Ruby on Rails: How do I use a Gem? (Vacuum Amazon API) - ruby-on-rails

I found an Amazon Gem that I want to use https://github.com/hakanensari/vacuum/.
I've only ever used gems with in depth tutorials or following a RailsCast. I'm looking for tips on using gems I find online. I'll explain the steps I'm doing, and hopefully someone can give me some ideas on what else I should be doing when using a new gem. Also, if you have a good tutorial or explanation on gems, that would be great too.
I started off examining the Gem on Github, I'll point out the things I took notice of. Let me know if there are things I'm missing I should notice.
Examining Gem on Github
Go to the examples and look at "examples/product_advertising/basic_lookup.rb"
Follow the required file, and checkout "examples/product_advertising/shared.rb"
Notice, I need to install "pry"
Notice, the examples expand on the "lib" folder
Check out "credentials.yml"
Notice a "#req" is instantiated as a new Vacuum object.
Then back in basic_lookup.rb, it looks like it's assigning the lookup value, then binding the response to some kind of "pry" view.
Next, I'll try implementing these examples in my own project. Here's where I'm not sure what to do as far, as files are concerned.
Attempt Implementing Example
Install vacuum gem
gem install vacuum
Install pry gem
gem install pry
Added "shared.rb" and "credentials.yml" to my "app/controllers" directory
Replaced the information in "credentials.yml" with my information
Attempt to copy the information from "basic_lookup.rb" into an existing controller
def amazon
require File.expand_path('../shared.rb', __FILE__)
res = #req.look_up '0816614024'
items = res.find 'Item'
binding.pry
end
Create a route
match '/test' => 'products#amazon'
Go to test page and receive the following error
undefined method 'look_up' for nil:NilClass
I would like to point out, at this point I haven't added the lib folder.
Questions
I like the credentials.yml is separated out, when I want to add this to my project, where should I save that file?
I like the shared.rb file, should I just put that in the controller folder?
Why is it referencing the "lib" folder in "shared.rb"? Do I need to copy that directory into my project?
I appreciate you sticking around and reading all of this. I'm still trying to get a handle on using a gems, so any help or tips are great. Really, I'm just trying to figure out, how do I find any gem and start using it appropriately.
Thanks for any help you can give me!

I like the shared.rb file, should I just put that in the controller folder?
Answer = yes, you just put that file in controller folder.

Related

Where can I look at the Rails controller high-level superclasses?

I am trying to understand some of the higher level functioning of Rails, using the Rails console. I run controller.class.superclass.superclass which gives ActionController::Base, controller.class.superclass.superclass.superclass which gives ActionController::Metal and controller.class.superclass.superclass.superclass.superclass gives AbstractController::Base.
I have found these in the API documentation.
http://api.rubyonrails.org/classes/AbstractController/Base.html
http://api.rubyonrails.org/classes/ActionController/Metal.html
[can only post two links]
I can add to these simply by declaring the classes again in the console, but is there a way to find the original Ruby code for these and to inspect and edit it in its original file(s)? Just in case I need to know the full contents of these for future.
You can also do:
bundle show <gem>
and that will show you where the gem is on your system. Editing in those files is not advised unless you know how to re-install gems.
You can see the Rails source code on Github:
https://github.com/rails/rails/tree/master/actionpack/lib/action_controller

Is it possible to create a ruby gem that when added to a rails project simply appends code to its initializers?

I've got some helper code (monkey patches) I tend to carry around from rails project to rails project via simply copying files or pasting code into the initializers folder.
I think a clean way to deploy different categories of my initalizer code would be to simply add gems to my project Gemfile that would do the equivalent of this for me. I'm not very skilled at creating gems or the deeper side of ruby on rails engineering so I just wanted to know if this were possible before I got started with it.
Yes, this is definitely possible. It should be probably enough to make a simple gem, put your monkey-patches there and require them in the gem entry point (e.g., if the gem is called foobar, then bundler will require lib/foobar.rb by default).
There is a guide on creating gems available in Bundler docs. In case you need to interact with Rails application configuration, you can take a look at Rails Engines.

Ruby on Rails - In what file do I add the following: require 'forecast_io'?

Thanks for your help,
I believe this is very simple but I can't figure it out. Following the instructions at https://github.com/darkskyapp/forecast-ruby - it tells me to not forget to add require 'forecast_io' - what file do I put this in?
I've run a scaffold to set up a simple lat and lng, following this guide:
https://campushippo.com/lessons/an-easy-way-to-implement-weather-forecasting-in-rails-9d10403 but keep on getting method errors. So I think we are using different versions of Ruby & Rails, and/or I'm placing this code in the wrong file, or wrong place. My question isn't about method errors, but just where to place this.
I'm unsure whether or not to place it in /config/application.rb; create and place the code in /config/forecast_io.rb; or to create and place it in /config/initializers/forecast_io.rb; or if it's supposed to go somewhere else entirely.
I've looked at the api docs, the ruby wrapper read me, and also have read other tutorials (they won't let me post more links, otherwise I would list them) - one is from hackpsu.westarate that is kind of different with using sinatra, went through the Treehouse tutorial on Rails scaffolding, and began their tutorial on creating an API to better understand REST, and have looked at other rails projects to see how they incorporate external API's, so I'm in the thick of it, and am banging my head against the wall because it seems so simple, but I'm not seeing it yet.
Thanks for your help!
David
In Rails, placing a gem in your Gemfile already requires the gem by default, unless explicitly stated to ignore.
If you're still bothered about it though, you could add the require option to your gem as such:
gem 'forecast_io', require: 'forecast_io'
Now, for the rest of the configurations mentioned, you can create a file in your initializers and the following:
#config/initializers/forecast_io.rb
ForecastIO.configure do |configuration|
configuration.api_key = 'this-is-your-api-key'
end

How to convert a large gem to standalone rails app

I'm building a social network and have been using the gem community_engine but have been having trouble implementing the large amount of customization that I need for my app. I figure this will make it easier for me to override and add methods, as well as help me to better understand and learn from the code since I will be able to actually see all of it in action.
So far in my attempt I downloaded the source code, added the default bin directory and config files that were missing, as well ass all the gem dependencies.
What else do I need to do to get the app to work? I realize that there may still be a lot and that it might not be easy to explain, but at the very least is there any sort of documentation out there that might help me understand how to convert the gem to a Rails application?
Heres the community_engine repo: https://github.com/bborn/communityengine
Because this idea may draw some criticism, I'll add that I was originally building the app without any huge plugins accept for devise however I'm running out of time to finish this.
More stuff I've tried:
Moving files to a new rails app, got server to run but encountered many seemingly random errors, fixed a few but more just seem to pop up that I cant figure out:
I also took a look at http://guides.rubyonrails.org/plugins.html but this gem seems to go beyond that.
I would suggest that you clone the gem and begin copying files from the gem into your a new Rails application.
The engine gem probably has a similar structure to a Rails application, so you should be able to move the files from the corresponding folder to the same folder in your Rails root folder.
You may need to move gem files out of modules, change namespaces etc. Relevant folders to look at files you'll want to include might include app/ config/ db/, any gem dependencies in Gemfile or the gemspec file, as well as spec/ or test/.
Beyond that I think there's no silver bullet answer to your question, you're just going to have to work through problems until you have this up and running, and perhaps ask subsequent questions if you hit on an obstacle that you don't get beyond.
I think what you're looking for is a way to hook your Rails Engine into a rails app. The Hooking Into an Application section of the Getting Started with Rails Engines guide should be exactly what you're looking for.
Here are two additional resources on Rails Engines.
A Guide to Rails Engines in the Wild
Rails::Engine - Ruby on Rails API

Where is the sSwfPath path when using jquery-datatables-rails gem

I have set up Data Tables in my rails 4.1 project using the rweng/jquery-datatables-rails gem.
Everything works great except the Table Tools extension. The buttons show but they do not work.
I finally figured out that the reason the buttons don't work id because the path:
sSwfPath": "/swf/copy_csv_xls_pdf.swf"
Does not exist.
The path is obviously within the gem file somewhere, but I've got no idea how to find it.
I finally got it to work by setting the path to:
sSwfPath": "http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"
But I'm not sure if this is the correct way to go. Would rather use a local copy.
Many thanks

Resources