YouTube Data Api and Rails - ruby-on-rails

I've been able to set up a toy app with the following code taken from YouTube's sample ruby projects. I'm looking to use YouTube to search for artists channels, playlists, videos. No issues with the sample toy app but can't get the same code working in Rails.
After successfully searching YouTube's API and manipulating the response, I've tried importing some of this code into an existing rails app.
I initially put the following code into a new initializer in my config directory but when trying to load rails console to play with the query, I'm receiving the following error:
"NameError: uninitialized constant Google"
require 'google/api_client
client = Google::APIClient.new(
:key => "XXXXXXXXXXX",
:authorization => nil,
:application_name => $PROGRAM_NAME,
:application_version => '1.0.0'
)
Gemfile:
gem 'google-api-client', '0.9'
I know there's been some updates to the API but having trouble tracking down exactly what I need to do.

Have you added require 'google/api_client' to the file where you're instantiating that client variable?

I had a the same error using Ruby 2.6.5 in a Rails 6 app. I found this similar issue on Github and downgrading the google-api-client gem solved it for me.
I used
gem 'google-api-client', '~> 0.7.1'

Related

Get Google's authentication_token in the Ruby on Rails application

I want to use the Google API in my Rails application. For this, I need to authenticate to a Google Dev account to get the authentication_token.
The best way to do it, in my opinion, is to use oauth2 library and examples provided in the documentation.
As you see, I have tried to use Google::APIClient in there, although I am stuck with the following issue below
In the console I get:
Google::APIClient::KeyUtils.load_from_pkcs12('file.p12', 'notasecret')
NameError: uninitialized constant Google
I'm using following gems:
gem 'oauth2'
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'google-api-client'
Any suggestions?
You just need to require a file
require 'google/api_client/auth/key_utils'
Only then you would be allowed calling a Google::APIClient::KeyUtils.
You can notice in the documentation they require each class/module separately.
It seems like gem does not load all of its dependencies itself, because of their ideology and just not to overload the application with redundant libraries that won't be used.
Take a note, it is quite possible you will need to require some other libraries.

Rails 4.2 - Grape - IOError: Not opened for reading

I'm building an API with Grape on Rails 4.2. Here's a link to the repo on GitHub.
On the frontend, I have a JavaScript application built with EmberJS. Here's the link to the repo on GitHub as well.
I have updated the following gems so that I can format the responses of my API following JSON API standard, which appears to be required to update to Ember 1.13, then to Ember 2.0.
gem "active_model_serializers", '0.10.0.rc2'
gem "grape"
gem "grape-active_model_serializers", :git => 'https://github.com/jrhe/grape-active_model_serializers.git'
After that, I've got the following error when I call the API: IOError: Not opened for reading
According to the information I gathered here and there, I suspect this is a conflict with how I format JSON.
In default.rb, the file that inherit all API controllers, when I comment this line:
formatter :json, Grape::Formatter::ActiveModelSerializers
I don't have any error anymore, but obviously the response isn't serialized.
My questions are:
Do you have any idea how I can solve this?
Do you think it's relevant to use Grape to build the API in my Rails app? With Rails 5 coming, isn't that better to user rails-api and rewrite the whole app? I mean, I'm starting, it's maybe the right time to do that...
Let me know if you need more information.
Thanks in advance for your help.
I ended up rewriting the whole API using jsonapi-resources.

PayPal payment error

I've got an app that has the possibility to do shopping, making the money transaction through paypal. I'm trying now to integrate the paypal stuff into my rails project and as I'm trying to learn how that's done I've downloaded this sample project that appears to show a good example of how this stuff is done.
I think I've got everything covered when talking about configuration. That app is now all configured with my paypal credentials.
My problem is that after making any payment, when making the callback, the app throws this error:
undefined method `recipientName=' for #<PayPal::SDK::REST::DataTypes::Address:0x007feb13a30ee8>
Even with the default configuration I'm having this issue.
Does anyone know what is that about? I guess is something about the gems included in the project that have to do with paypal:
# PayPal
gem 'paypal-sdk-core', :git => 'https://github.com/paypal/sdk-core-ruby.git'
gem 'paypal-sdk-rest', :git => 'https://github.com/paypal/rest-api-sdk-ruby.git'
But have any clue about how to fix it. Maybe someone does.
Thanks!
I found the solution. Definitely was something about the gem versions. Just updated them and problem solved.
Making this:
# PayPal
gem 'paypal-sdk-core', :git => 'https://github.com/paypal/sdk-core-ruby.git'
gem 'paypal-sdk-rest', :git => 'https://github.com/paypal/rest-api-sdk-ruby.git'
Didn't installed the latest version of both gems. I've just took out those git specification and after a bundle update the problem was solved.

Building a new strategy for Omniauth, but Omniauth can't find it

I'm new to Rails and to Omniauth, so sorry in advance for the dumb
question.
I'm trying to do an Omniauth strategy for the Deezer website.
http://www.deezer.com/en/developers/simpleapi/oauth
First, I set up a basic rails application to test the Facebook strategy.
Here is my code in Github :
https://github.com/geoffroymontel/omniauth-test
And it works fine. Good.
Then I added those files in my app lib directory
lib/omniauth-deezer.rb
lib/omniauth/deezer.rb
lib/omniauth/deezer/version.rb
lib/omniauth/deezer/strategies/deezer.rb
and added
provider :deezer, ENV['DEEZER_APP_ID'],
ENV['DEEZER_APP_SECRET'], :perms => 'basic_access,email'
in
config/initializers/omniauth.rb
But when I start the app with
rails s
I get the following error message
/home/geoffroy/.rvm/gems/ruby-1.9.2-p290#rails3tutorial2ndEd/gems/omniauth-1.0.2/lib/omniauth/builder.rb:33:in `rescue in provider': Could not find matching strategy for :deezer. You may need to install an additional gem (such as omniauth-deezer). (LoadError) from /home/geoffroy/.rvm/gems/ruby-1.9.2-p290#rails3tutorial2ndEd/gems/omniauth-1.0.2/lib/omniauth/builder.rb:30:in `provider'
Thanks for your help
Best
Geoffroy
I added
require 'omniauth-deezer'
in initializers/omniauth.rb and it worked.
I don't really understand why I need it and Facebook doesn't.
In the Upgrading to 1.0 doc it is mentioned that Omniauth needs a gem for every provider now, so you'll have to move your provider to a gem eventually.
For future reference, if you want to add your own omniauth strategy, add this to your gemfile
gem 'omniauth-mystrategy', :path => '~/full-path-to-the-omniauth-folder/'
You´ll be using the local repository feauture of the gemfiles.
In the official bundler page you´ll see how to use remote repos also.
http://gembundler.com/v1.3/gemfile.html

Adding Google Analytics to an app

I want to implement this new method of Google Analytics, I want to conditinally insert this code in the head section conditionally in production mode only, any suggestion to how to do this?
http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html
Google Analytics and Rails in 5 EASY Steps:
If you are in Rails 3, I just found a great solution for doing Google Analytics in Rails apps.
(1) In your Gemfile:
group :production do
gem 'rack-google_analytics', :require => "rack/google_analytics"
end
(2) Bundle Install
(3) In your config/application.rb (put this in the class definition section - careful not to drop it in a module. I put mine right under "class Application"):
if Rails.env == "production"
config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1")
end
(4) Initiate your Google Analytics account
(5) Copy and paste that funky web_property_id from Google's supplied code into the code from (3), replacing 'UA-000000-1'
That's it!
I originally found this solution here:
David Bock Article
I know this is already answered, but I spent quite a bit of time based on an answer above which seems to be outdated. So I am posting what I did to get Google Analytics working with my Rails 3.0 app.
1) Add the following in your Gemfile
group :production do
gem 'rack-google-analytics', :require => 'rack/google-analytics'
end
Note that it is rack-google-analytics and NOT rack-google_analytics. Ditto with the require part
2) In your config/environments/production.rb file, add the following snippet (replacing the dummy tracker with the google analytics tracker for your website).
config.gem 'rack-google-analytics', :lib => 'rack/google-analytics'
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-XXXXXXXX-X'
3) Run bundle install and start the rails server
4) Sit back and see the analytics flowing in!
Check out https://github.com/leehambley/rack-google-analytics/blob/master/README.md for instructions on how to make it work with sinatra, padrino etc.
Also, the latest copy of the gem uses :async option to use asynchronous tracker. The default is true, so you won't need to use this option unless you want to suppress async for some strange reason!
Are you using Rails 2.3 ? You could wrap the snip in an if statement,
<% if Rails.env.production? %>
<!-- my analytics code -->
<% end %>
simple enough.
For people coming across this article, be aware this gem supports the traditional google analytics (as opposed to the "newer" aysnc version). Google Analytics Async
I didn't really see any switches to convert between the 2 types.

Resources