Skip to content

Add incentives examples in Ruby#542

Closed
dorasun wants to merge 1 commit intomainfrom
add-incentives-examples-12559570931346782113
Closed

Add incentives examples in Ruby#542
dorasun wants to merge 1 commit intomainfrom
add-incentives-examples-12559570931346782113

Conversation

@dorasun
Copy link
Contributor

@dorasun dorasun commented Mar 24, 2026

Translates the fetch_incentives.py and apply_incentive.py examples into Ruby under the examples/incentives directory. They feature standard boilerplate and argument parsing seen in other repository examples.


PR created automatically by Jules for task 12559570931346782113 started by @dorasun

This commit translates the Python examples for applying and fetching incentives into Ruby examples under `examples/incentives`. These match the existing coding styles of other examples.

Co-authored-by: dorasun <10905385+dorasun@users.noreply.github.com>
@dorasun dorasun requested a review from a team as a code owner March 24, 2026 17:37
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

$stderr.printf("\tType: %s\n\tCode: %s\n", k, v)
end
end
raise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment to explain why the error is re-raised or apply the comment from fetch_incentives.rb.

require "google/ads/google_ads"

# [START fetch_incentives]
def fetch_incentives(email_address, language_code, country_code)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependency Injection: Instead of instantiating the client inside the method, pass it as an argument. This allows you to share one authenticated session across multiple API calls, reducing overhead.

# incentives in the response. At the time this example was written, all
# incentive offers are CYO incentive offers.
cyo_incentives = response.incentive_offer.cyo_incentives
puts cyo_incentives.low_offer.inspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Human-Readable Output: Replace .inspect with a helper method to print specific attributes like offer_id or expiration_date. Raw object inspection is difficult for end-users to parse."

# code.
#
# Running the example with -h will print the command line usage.
options[:email_address] = "INSERT_EMAIL_ADDRESS_HERE"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placeholder Check: Add a check to ensure the user actually replaced INSERT_EMAIL_ADDRESS_HERE. You could raise an error immediately if the string still contains 'INSERT' to prevent an unnecessary API round-trip.

$stderr.printf("\tType: %s\n\tCode: %s\n", k, v)
end
end
raise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error Bubbling: Instead of re-raising the raw GoogleAdsError, consider wrapping it in a domain-specific error (e.g., IncentiveFetchError). This makes it easier for higher-level services to catch and handle logic errors separately from network errors.


response = client.service.incentive.apply_incentive(request_params)

puts "Applied incentive."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional Feedback: The message 'Applied incentive' might be misleading if the user already had one (as per the header comments). Change this to 'Incentive details retrieved/applied' for better accuracy.


puts "Applied incentive."
puts "Coupon Code: #{response.coupon_code}"
puts "Creation Time: #{response.creation_time}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date Formatting: The raw creation time might be a timestamp object. Formatting this (e.g., .strftime('%Y-%m-%d %H:%M:%S')) makes the console output much more readable.

# code.
#
# Running the example with -h will print the command line usage.
options[:customer_id] = "INSERT_CUSTOMER_ID_HERE"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safety Guard: Implement a check to ensure INSERT_ placeholders are replaced. Running this with dummy strings results in a wasted API call and a 400-range error.


begin
apply_incentive(
options.fetch(:customer_id).tr("-", ""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input Sanitation: Moving the .tr("-", "") logic into a helper method or the apply_incentive method itself ensures consistency if you ever call this function from a different part of the app.

$stderr.printf("\tOn field: %s\n", field_path_element.field_name)
end
error.error_code.to_h.each do |k, v|
next if v == :UNSPECIFIED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detailed Logging: "In a production environment, you should log these errors to a persistent file (e.g., ads_api.log) rather than just printing to $stderr, which is lost after the terminal closes."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants