How to configure a GEM in a Ruby on Rails app - ruby-on-rails

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

Related

Rails google-ads-ruby setting up

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.

Uninitialized constant Google::Cloud raised by Google::Cloud::Translate.new in production environment

I have a GoogleTranslate service that translates text in my app. The feature works on localhost but in production it raises
uninitialized constant Google::Cloud in my app/helpers/google_translations_helper.rb:4:in `initialize'...
Here is the Gemfile related part :
# use of google API
gem 'google-api-client', '~> 0.11', :require =>
'google/apis/analyticsreporting_v4'
gem 'omniauth-google-oauth2'
gem "google-cloud-translate"
And here is the helper/service initializer:
module GoogleTranslationsHelper
class GoogleTranslate
def initialize
#translation_service = Google::Cloud::Translate.new
end
def translate(text)
#translation_service.translate text.to_s, from: "fr", to: "en"
end
end
end
I wonder if I'm not missing something about gem version or something like that..
Does someone had the problem already?
I assume you are running Rails. Make sure you include:
require "google/cloud/translate"
somewhere reasonable, either at the top of the file that creates the client object (app/helpers/google_translations_helper.rb in your case), or in a global initialization file such as config/application.rb. (The google-cloud-translate library, like most libraries, needs you to require it before you can use it. See the snippets in the documentation for examples.)
It's not completely clear to me why this is working differently between your development and production environments, but there are usually a lot of differences in the initialization procedure between the two environments so it's not too surprising. Just make sure you're in the habit of requiring any library before using it.
As a side note, I would also recommend updating your Gemfile to call for more recent versions of the Google client libraries. Or at least make sure you've done a recent bundle update. As of this writing, google-api-client 0.11 is more than 2 years old; the newest is 0.30.8. And google-cloud-translate is at 1.3.0. It is always possible there are issues if you're on old versions.

How to do ActiveModel out of box?

Ok, I was poking around github's rails activemodel stuff for the heck of it.
Link: https://github.com/rails/rails/tree/master/activemodel
When I saw this code included in their README.rdoc
class Person
include ActiveModel::AttributeMethods
attribute_method_prefix 'clear_'
define_attribute_methods :name, :age
attr_accessor :name, :age
def clear_attribute(attr)
send("#{attr}=", nil)
end
end
person = Person.new
person.clear_name
person.clear_age
Thought that above code was pretty cool as I wasn't familiar with this. Only have seen something like this with dynamic find method, sort of.
So wanted to run it, but I don't know how to run it outside of the context of ROR framework.
How do I go about doing that?
Thanks.
If this question is dumb, let me know? (nicely, please).
Asking because I've heard that people run ORM outside of ROR framework, so, that's why I'm asking this question in the first place. Let me know if I misunderstood this.
You could always run it in an IRB as it's a ruby method not specific to Rails.
http://ruby-doc.org/core-2.1.1/Object.html#method-i-sendirb
For doing Active Record Like Models you could use this gem http://rubygems.org/gems/activemodel, or this gem https://github.com/solnic/virtus.
First create one dummy application in Rails,then open rails console using this command rails c and you past the code then you could verify.
or
Inside application lib directory create one file with .rb extension now open console and you can verify.
More info click here

what to set in Capybara.app for a Middleman project?

In the env.rb to use Capybara you should setup Capybara.app = something
Middleman is based on sinatra so I was thinking to use Sinatra::Application but gives an error
Anyone know what should be put to set up Capybara in the proper way?
Although I've recently stated this answer in the (recently closed) GitHub issue that #bhollis gave, I should fill in the answer here as well in keeping with the spirit of StackOverflow.
In a spec_helper.rb file added to a spec folder in the root of your Middleman project, the assignment I've used is Capybara.app = Middleman::Application.server.inst - however, I configure it a little bit too like so:
Capybara.app = Middleman::Application.server.inst do
set :root, File.expand_path(File.join(File.dirname(__FILE__), '..'))
set :environment, :development
set :show_exceptions, false
end
A full example of this using RSpec can be found here.
The answer will eventually be at https://github.com/middleman/middleman/issues/895

Rails I18n Backend with MongoDB/Mongoid

Does anyone have an idea on how to implement this (http://railscasts.com/episodes/256-i18n-backends) with MongoDB/Mongoid? My question is primarily about the initializer.rb file.
The docs of Mongo-I18n on github suggests the following using its 'MongoI18n::Store.new' method:
collection = Mongo::Connection.new['my_app_related_db'].collection('i18n')
I18n.backend = I18n::Backend::KeyValue.new(MongoI18n::Store.new(collection)
But how to do this if you don't want to use their plugin? Is there something like a Mongo::Store method?
I just did this exact same thing, except that I had trouble installing Mongo-I18n, because it has a dependency on a very old version of MongoDB.
To get around this, I copied the code from here into lib/mongo_i18n.rb.
You were on the right track with your initializer though, if you're using Mongoid - the best way forward is to do this:
require 'mongo_i18n'
collection = Mongoid.database.collection('i18n')
I18n.backend = I18n::Backend::KeyValue.new(MongoI18n::Store.new(collection))
Which tells the I18n backend to use a new collection (called i18n), but in the same database as the rest of your application.
Make sure you delete the Mongo_I18n gem out of your gemfile and run bundle before starting your server again.
You can access your store directly using:
I18n.backend.store
But to make it a little cleaner, I added this method to my I18n library:
# mongo_i18n.rb
def self.store
collection = Mongoid.database.collection('i18n')
MongoI18n::Store.new
end
So that I can access the store directly with:
MongoI18n.store
I did exactly like theTRON said, except that instead of require 'mongo_i18n' I added whole class MongoI18n::Store definition from Mongo_i18n gem directly to mongo initializer. It not such a big deal, because whole MongoI18n::Store is 41 lines long. Look here, why make dependancy from 41 lines gem ?

Resources