Rails google-ads-ruby setting up - ruby-on-rails

I am trying to use the google-ads-ruby library to allow our ruby on rails application users to connect our app with Google Ads and pull some stats from their account for them.
I installed the gem and managed to authenticate a user and get the refresh_token.
Now I'm trying to start collecting data from Google.
The first thing that fails is their instructions to require the gem in my code with require 'google/ads/google_ads'
I tried adding it to my controller and got cannot load such file -- google/ads/google_ads
Then, according to their instructions, I should be able to run this:
client = Google::Ads::GoogleAds::GoogleAdsClient.new do |config|
config.client_id = Rails.application.secrets.google_oauth_client_id
config.client_secret = Rails.application.secrets.google_oauth_client_secret
config.developer_token = Rails.application.secrets.google_developer_token
config.refresh_token = #user.google_ads.refresh_token
end
accessible_customers = client.service.customer.list_accessible_customers().resource_names
accessible_customers.each do |resource_name|
puts "Customer resource name: #{resource_name}"
end
and then list, for example, the user's accounts, as described here.
However, I am getting uninitialized constant Google::Ads::GoogleAds
Does anyone know what is going on?

Have you tried?
client = ::Google::Ads::GoogleAds::GoogleAdsClient.new do |config|
config.client_id = Rails.application.secrets.google_oauth_client_id
config.client_secret = Rails.application.secrets.google_oauth_client_secret
config.developer_token = Rails.application.secrets.google_developer_token
config.refresh_token = #user.google_ads.refresh_token
end

This is not really an answer to my question. I was unable to find a solution, but doing some more digging, I found the AdsWords on Rails example app Google added to that same gem and the documentation
The app is slightly outdated and you will probably hate getting it to work. Also, it's written in a very cryptic way and includes so many functions just to use their API... but I was able to make it work. To be honest, someone should write a tutorial.
Hope this may give some clues to someone who's lost at some point.

Related

How to configure a GEM in a Ruby on Rails app

Sorry, I'm back with another noob/dumb question. However, I have been working on this for a while now and can't seem to find a definitive answer/solution.
I found this answer from SObut it's really specific to the gem in question... I also found another answer or 2 but it really wasn't clear enough.
So I'm trying to use this gem for my app: https://github.com/shideneyu/kraken_client#configuration and there's a whole section about configuration that I would like to use:
KrakenClient.configure do |config|
config.api_key = ENV['KRAKEN_API_KEY']
config.api_secret = ENV['KRAKEN_API_SECRET']
config.base_uri = 'https://api.kraken.com'
config.api_version = 0
config.limiter = true
config.tier = 2
end
Note that they do not precise any 'require' in this code so I'm guessing that I won't be needing it (unlike the 'phaxio' gem from the answer I found on SO)?
I've tried to create a kraken_client.rb file in /config/initializers/ but when prompting the rails console it won't use it.
My objective is to first test the gem through the console to be able to build the rails app on that afterwards.
I could use the other option cited in the gem repo:
KrakenClient.load({base_uri: 'https://api.kraken.com', tier: 3}).config.tier
But I'm really not sure how to use that either (sorry... I'm really, really new). So my question is how can I configure this gem and generally how are gems configured once installed in a rails project?
Thanks in advance for your help!
in config/initializers/load_lib.rb(create if does not exist) write: require 'kraken_client' and save
after it come to rails c and call it

Ruby currency exchange gems that work?

This is based on an earlier question that was resolved. I need to load sale prices for my ruby-based app in different currencies. I was recently using the gem google_currency to convert the prices based on the Google API. At some point recently it stopped working and I have no idea why. I have tried testing in various ways but can't work out what the problem is.
I am now trying to use the 'exchange' gem which has good documentation however the method I am using is not producing anything in the view files when running.
According to the exchange gem the simple conversion should be something like:
def exchange4
puts 10.in(:eur).to(:usd)
end
However it is not loading anything in the html view. Any suggestions including other working gems welcome!
Currently this code seems like it would pass however now Action Controller is telling me it doesn't know the conversion rates:
def exchange4(goods)
require 'money'
require 'money-rails'
exr = Money.new(1, goods.currency).exchange_to(buyer.currency)
puts exr
end
The error Action Controller is giving is:
No conversion rate known for 'GBP' -> 'EUR'
Very strange..
RubyMoney organization has a very good options to deal with currencies, money and exchange. I use money and it really works. For Rails integration they have money-rails.
Examples of exchange:
Money.us_dollar(100).exchange_to('EUR')
Money.new(100, 'USD').exchange_to('EUR')
You can use eu_central_bank gem (compatible with money) to extract all exchange rates. Example usage (in rails console):
>> bank = EuCentralBank.new
>> bank.update_rates # if bank.last_updated.blank? || bank.last_updated < 1.day.ago
>> Money.default_bank = bank
Then:
>> Money.new(1, 'GBP').exchange_to('EUR')
=> #<Money fractional:1 currency:EUR>

Where do I put ruby code that I want to run when users access certain pages?

I have a bit of code that I need to run when a user loads a page. What it does specifically is update my database based on an xml file using a gem. So far I've found answers that tell me I should put it in everything from a rake task to the lib folder itself to the model. It's all a little confusing.
Here's the code in question:
require 'rubygems'
require 'eaal'
EAAL.cache = EAAL::Cache::FileCache.new
api = EAAL::API.new("id", "vcode", "char")
result = api.MarketOrders("characterID" => "id")
result.orders.each do |order|
#found = MarketItem.find_by_typeid(order.typeID.to_i)
MarketItem.update(#found.id, :remaining => order.volRemaining.to_i)
end
I'm sorry if this is an obvious question and I'm sure my code is horrible. I'm really new to rails and the only way I seem to be able to learn new languages is the bull-in-a-china-shop method.
There is a route to that page that hits a function first
for example. show_user_path will hit the function show. You can put your code at the beginning of the function that renders the page.

How can I fix this undefined method error?

I am integrating Postmark's transactional email service into my web application so that when a user signs up, my application sends the user a welcome email. When I try to signup as a user, I get the following error: undefined method `postmark_settings=' for ActionMailer::Base:Class
In my config/application.rb file, I have the following code:
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => "my_api_key" }
I'm not sure what is causing the error or how I can go about fixing it. If I haven't provided enough useful information to solve the problem, just let me know and I'll add whatever code might be useful. Thanks so much!
Have you included the line below in your Gemfile?
gem 'postmark-rails', '0.4.0'
There are similar issues where people haven't included this line on the Gemfile...
Also, are you using Rails 3? The configuration in different between Rails 2 and 3. Please note details here.

Rails vanity gem custom metric error: private method `to_time' called

Hi I am trying to setup Vanity gem into a rail 3 application.
I am creating a custom metric to experiment a bit.
So I created it following the suggestions on their website:
metric "Signups" do
description "Signups completed"
def values(from, to)
(from..to).map { |i| 24 }
end
end
The file is located at the right place and is loaded, vanity picks it up but somehow it looks like there is an internal error in the way vanity works.
Of course: those values are only there for a testing purpose, they will be replaced by real ones later.
I get the following error when running this experiment
Thank you!
There seems to be an error in the gem:
in this file
.rvm/gems/ruby-1.9.2-p290/gems/vanity-1.7.1/lib/vanity/templates/_metric.erb
where we can read the following line
js = data.map { |date,value| "['#{**date.to_time.httpdate**}',#{value}]" }.join(",")
vanity_html_safe(%{<div class="chart"></div>
if we change date.to_time.httpdate to date.to_s.to_time.httpdate it works.

Resources