Where do I require the songkickr gem file? - ruby-on-rails

I am attempting to use the songkickr gem in a rails app. I have installed the gem using the
gem install songkickr
The next step of the instruction is to:
require 'songkickr'
remote = Songkickr::Remote.new API_KEY
Where do I do this? In what file?
The github page for the gem is https://github.com/jrmehle/songkickr
and my github for this is on https://github.com/jeremybelcher/travel
Sorry for the beginner question, trying to lean al this. Any help is very much appreciated.

In your gemfile at your application root you need to add the line
gem 'songkickr'
Then run
bundle

There are several places where you can put ruby code in a Ruby on Rails app and you should just choose one of them, depending which makes the most sense for you application. For a start, how about you just try putting that code in one of your controllers?
In your Gemfile, add:
gem 'songkickr'
Then run the following command: bundle
At the top of the controller put:
require 'songkickr'
API_KEY = ""  # edit this line
Then in some action you can do this to test it out:
remote = Songkickr::Remote.new API_KEY
Later you might decide to make the "remote" object persist between requests and then you would need to do something more complicated.

Add require 'songkickr' to your application controller
Example:
class ApplicationController < ActionController::Base
protect_from_forgery
require 'songkickr'
end

Related

Rails: How to use initializer code inside gem

I'm using the okcomputer gem for a Rails app (All you need to do to use this gem is to place some code in an initializer).
I'd like to wrap this gem inside a custom gem that can be used without an initializer. I hope to be able to use this gem in several dockerized microservices just by installing it.
I read that it's possible to put initializer code in an init.rb file at the root of the gem. In my case, that doesn't seem to be working (the routes generated by okcomputer are not found), but I'm not sure where the issue lies.
In general, can I expect code in init.rb to behave like code in an initializer?
Your best approach is probably to create a Railties and run your code in an after_initialize hook.
module Gemname
class MyCoolRailtie < ::Rails::Railtie
config.after_initialize do
OKComputer::Registry.register "resque_critical",
OKComputer::ResqueBackedUpCheck.new("critical", 10)
end
end
end
https://api.rubyonrails.org/classes/Rails/Railtie.html

Using a gem in Ruby on Rails does not seem to work

I have installed the gem ruby-imagespec
The code looks like the following:
class TestController < ApplicationController
def ratio(fid)
file = File.new("#{Rails.public_path}/test/#{fid}.swf", "rb")
dimensions = ImageSpec.new(file)
return dimensions
end
helper_method :ratio
end
When I try to run this I get an error:
"uninitialized constant TestController::ImageSpec" on line 7 The
gem is listed in the gemfile and explicitly requiring it does not
work.
Where is the error? The gem is installed correctly and this code is just like the code from the gem's README
Looking at the ruby-imagespec repository, the file you need to require appears to be lib/image_spec.rb. Try require 'image_spec'.
ruby-imagespec is an unsupported gem, please try fastimage:
add gem 'fastimage' in your Gemfile
here the documentation https://github.com/sdsykes/fastimage

Rails bootstrap gem monkeypatching method not working

I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end

Why uninitialized constant HomeController::HideMyAss?

Cant figure out what I'm doing wrong.
Using hidemyass gem for proxy.
Using github example code.
class HomeController < ApplicationController
require 'hidemyass'
def index
HideMyAss.options[:max_concurrency] = 3
response = HideMyAss.get("www.google.com", timeout: 2)
end
end
Whey I try to use them in my rails controller, I get the Uninitialized constant error.
uninitialized constant HomeController::HideMyAss
Tried looking at the source to figure out with no luck. Maybe it's the problem with my code. Gemfile is good and tried looking at all the things causing the problem.
There is no need to write require 'hidemyass'
You just need to add gem 'hidemyass' to Gemfile and do bundle install. Check installation with gem list hidemyass command. I have successfully checked it and used github example. It works fine and smoothly.

Creating an install generator for a Ruby gem

I'm pretty new to building Ruby gems and am taking a stab at my first one. I'm in the middle of writing a generator for my gem that will generate a migration in my Rails app. I'm looking to simply include the gem in the Rails application, run "rails g mygem:install" to have it create the migrations, then run "rake db:migrate" to finalize everything.
I've found several different ways to accomplish similar tasks, but so far nothing has worked. I cannot seem to get the Rails application to find the generator. The latest guide I've tried is located here: http://www.railsdispatch.com/posts/how-rails-3-enables-more-choices-part-1.
Here is what my current gem structure looks like:
-lib/
-generators/
-templates/
-some_migration.rb
-install_generator.rb
-gemname/
-rails/
-railtie.rb
-engine.rb
-tasks/
-gemname.rake
-gemname.rb
-spec/
-gemname.gemspec
Here is what my install_generator.rb file looks like:
require 'rails'
module Gemname
class InstallGenerator < ::Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
desc "add the migrations"
def self.next_migration_number(path)
unless #prev_migration_nr
#prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
else
#prev_migration_nr += 1
end
#prev_migration_nr.to_s
end
def copy_migrations
migration_template "some_migration.rb", "db/migrate/some_migration.rb"
end
end
end
I'm not sure if there's something I'm missing. I'm testing with a Rails 3.2 application that has my gem listed in its Gemfile and the gem is installed. Is there anything wrong with gem folder structure that might be preventing my generator from showing up? Do I need to require something somewhere?
Any help is appreciated.
Rails no longer automatically loads everything in lib by default, like it used to. For a few methods of requiring your files, check out the answers to this question - Best way to load module/class from lib folder in Rails 3?.
Well, I'm not sure which of the changes I made fixed my problem, but here is what I did:
Changed the name of my generator to gemname_generator.rb and the directory structure of my generator to this:
- lib/
-generators/
-templates/
-gemname_generator.rb
Added this line to my generator:
namespace 'gemname'
I'm now able to run "rails g gemname" in my Rails application and have it call my generator.

Resources