Ruby on Rails asking to put require and enable? - ruby-on-rails

I'm not sure where to put require and enable?
I'm trying to use the instagram api, but it's telling me to put
require "sinatra"
require "instagram"
enable :sessions
in the sample application : https://github.com/Instagram/instagram-ruby-gem
But I'm new to learning rails, so I'm just trying figure some bits out,
Thanks

Thats for a sintatra app, not a rails app, for rails all I think you need to do is add it to your gemfile like so.
gem 'instagram'
If you still can't use it try adding :require, though I don't think you should have to.
gem 'instagram', :require => 'instagram'

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.

What does `require:` mean in a Rails gemfile?

I'm doing a tutorial on authentication and came across the following line for gemfile. What is the use of require here?
gem 'google-api-client', require: 'google/api_client'
Tutorial: http://willschenk.com/setting-up-devise-with-twitter-and-facebook-and-other-omniauth-schemes-without-email-addresses/
I understand require in Javascript, but in Rails I thought the gemfile is for installing gems, and once they are installed they can be used in the application, and thats all there is to it... so I'm not sure why I would use require.
I'm particularely interested because after adding this line and starting the server, I ran into an error.
Error:
/usr/local/rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in
`require': cannot load such file -- google/api_client (LoadError)
Temporary solution: I've commented out the require: part and the error is prevented. But maybe this is not ideal.
So understanding the use of require would help very much in troubleshooting this.
I read other articles on SO, but they discuss specifics like require => nil and require => false, which I think is a little different from my question.
Bunder: What does :require => nil in Gemfile mean?
Bundler: What does :require => false in a Gemfile mean?
Can anyone share some incite?
UPDATE
I later found this which explains it well: When do you need a require in a rails Gemfile?
If you omit the :require option, by default Bundler will attempt to
require the gem by using the standard name-to-file conversion rule:
This works well if the gem author has followed the standard
conventions. But in some cases, for a variety of reasons, this doesn't
happen.
When the gem itself doesn't require any of its lib, you need to do that either in your Gemfile (the way you wrote) or in some file in your project.
Eg.: imagine a gem which has more than one solution for any particular problem. However, you don't want to load all of those solutions (files), you only need one. Then you'd need to specify which file you want to load by using require: some_lib.

how to require different gems with the same name?

I am interested in making an application that requires me to require a gmail gem for ruby.
Right now there are 2 gems:
https://github.com/dcparker/ruby-gmail
https://github.com/nu7hatch/gmail
Both gems have the same require name, ie: gmail
The second one is much clear but there is a problem with one of its method. This method works well in the first gem (link). So I was thinking maybe I could require the first one for just that method. Is it possible to do so, how?
As others answers and comments have said, you cannot simply require both gems as they are.
However, given that both are hosted on GitHub, you could fork one of them and rename the offending classes. So long as your renaming is consistent within the gem you could use your fork within your Gemfile
Of course, you wouldn't be easily able to rebase changes onto your fork easily but if you really must use both gems this might be a compromise that you are happy with.
You could add the following to your Gemfile:
gem 'gmail', :git => "git://github.com/dcparker/ruby-gmail", :branch => "master"
gem 'gmail', :git => "git://github.com/nu7hatch/gmail", :branch => "master"

Unitinialized constant. Integrating Google API in Rails

I am having a trouble integrating Google API client into my Rails app. I am fairly new to Rails.
I have included this in my Gemfile
gem 'google-api-client'
Then I have ran bundle in my console, in project directory
Lastly, I have created a controller, defined a route, and added
client = Google::APIClient.new(:key => "MyApiKey", :authorization => nil)
to my controller (I intend to use this with a public API key).
And when I try it in the browser, I get this:
uninitialized constant MyController::Google
Object creation without params creates same result. I have searched for a solution and tried to add require 'google/api_client' to my boot.rb, but it made no difference.
Could anyone tell me how I should approach this?
As per the official documentation you need to require the classes you are going to use, e.g.:
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
You should be adding this line to your controller
require 'google/api_client'
It should resolve the error.

Installing and using acts-as-taggable-on

This is going to be a really dumb question, I just know it, but I'm going to ask anyways because it's driving me crazy.
How do I get acts-as-taggable-on to work?
I installed it as a gem with gem install acts-as-taggable-on because I can't ever seem to get installing plugins to work, but that's a whole other batch of questions that are all probably really dumb. Anyways, no problems there, it installed correctly.
I did ruby script/generate acts_as_taggable_on_migration and rake db:migrate, again no problems.
I added acts_as_taggable to the model I want to use tags with, started up the server and then loaded the index for the model just to see if what I've got so far is working and got the following error: undefined local variable or method `acts_as_taggable' for #.
I figure that just means I need to do something like require 'acts-as-taggable-on' to my model's file because that's typically what's necessary for gems. So I did that hit refresh and got uninitialized constant ActiveRecord::VERSION. I'm not even going to pretend to begin to know what that means went wrong.
Did I go wrong somewhere or there something else I need to do. The installation instructions seem to me like they just assume you generally know what you're doing and don't even begin to explain what to do when things go wrong.
Did you try to define your gem dependencies in config/environment.rb (Rails 2.3):
Rails::Initializer.run do |config|
#...
config.gem 'acts-as-taggable-on'
#...
end
Or in Gemfile for Rails 3 or if you use already Bundler with rails 2.3:
gem 'acts-as-taggable-on'
This should make the require 'acts-as-taggable-on' unnecessary
Maybe following the installation here can help.
For example you don't need to:
require 'acts-as-taggable-on'
but:
class User < ActiveRecord::Base
acts_as_taggable
end
Otherwise you need to post more details about the error.
I installed acts-as-taggable-on for my app through github. If you want to try that method instead of the gem, you can read my this post that explains my experience: http://blog.mediummassage.com/2010/04/27/creating-categories-in-the-store-with-tags/

Resources