Where do I put an extension to open_uri - ruby-on-rails

I'm trying to fetch an image from Twitter:
open("http://api.twitter.com/1/users/profile_image/barackobama.png?size=bigger")
But I get:
RuntimeError: redirection forbidden: http://... -> https://...
There is an open issue and it seems that I can use an extension to open_uri but I don't know how it works. For example, if I place it in lib/ or if I paste the module in the console, it still doesn't work. Any idea?

I think the proper place to put such a patch is in a file inside config/initializers, i.e. config/initializers/open_uri_allow_unsafe_redirects_patch.rb. You have to require 'open-uri' before reopening the OpenURI module:
require 'open-uri'
module OpenURI
# the rest of the file here...
end
Then you have to call open passing the option allow_unsafe_redirects set to true:
open('http://api.twitter.com/1/users/profile_image/barackobama.png?size=bigger',
allow_unsafe_redirects: true)
You can find more informations about initializer files on the Ruby on Rails guide

Related

How import a helper file that isn't a controller helper?

Suppose I have a random file in a rails app: myapp/app/random_folder/random_helper.rb.
How can I load / require this file in the rails console?
Example
I have the file myapp/spec/spec_helper.rb and I want to require/load those helpers in the rails console.
What I've tried
I tried
require 'spec_helper'
require_relative 'spec/spec_helper.rb'
require_relative 'spec/spec_helper'
require_relative 'spec_helper'
but none of these approaches work.
It was a bug between the keyboard and chair. This does work:
require_relative 'path/to/some_helper'
Read the error message carefully because after loading the helper, an error may be produced, which is very different to a failure to load the helper file at all.
In my case, I saw NoMethodError: undefined method 'configure' for RSpec:Module and mistook it for an inability to load the helper file, but it was in fact loading the helper file but an error resulted from something in the helper file.
I will not accept this answer because it may not be the best way to load a random helper file, but hopefully it may be useful to someone else.

Rails adding external libs to path

I'm working on a rails app. I've extracted some common code out into a library under
APP_ROOT/lib/my_lib/my_lib.rb
APP_ROOT/lib/my_lib/version.rb
Currently my controllers can access the lib, but if I write a small standalone script in
APP_ROOT/lib/my_lib/test.rb
that looks like this:
require 'my_lib'
libtest = MyLib.new
I get a error:
/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- my_lib (LoadError)
I've tried modifying LOAD_PATH, RUBYLIB to include this, but I can't seem to get it to find the library.
For Rails 3/4 you simply need to update config/application.rb
config.autoload_paths << "#{config.root}/lib/my_lib"
Try this:
require './lib/my_lib/my_lib.rb'
It's your custom lib i.e.
my_lib.rb
and it's in a folder called my_lib. When you try to require a lib like:
require 'name_of _the_lib.rb'
the server looks for the file in the lib folder which is in APP_ROOT. So, now you have to mention your my_lib folder too like this:
require 'my_lib/my_lib.rb'
Hope it will work.

Rails 4.2 error require 'open_uri'

I am using Nokogiri with open_uri to crawl web data in a Rails app. I have a NokogiriCrawler class:
class NokogiriCrawler
require 'open_uri'
def initialize
end
end
but always have this error: cannot load such file -- open_uri
Try this instead:
require 'open-uri'
P.S. This is unrelated to your actual problem, but requires should almost always go at the top of your file, not inside your class declaration.

How to require some lib files from anywhere

I'll explain my situation.
Here is my file tree in my rails application :
lib/my_module.rb
require 'my_module/my_file'
module My_module
end
lib/my_module/my_file.rb
class Tweetag::Collector
(...)
end
I've made a ruby script that I've put in config/jobs/
I really don't understand how I am supposed to require the file my_file.rb in this file.
require '../../my_module/my_file.rb'
It gives me `require': cannot load such file
Same error with just require 'my_module' which is what I do in my controllers...
Someone here to explain to me ? Thanks a lot
You can autoinclude everything under the lib folderand avoid these problems:
Type this your file in config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
If you want to require only a specific file then,
do something relative to Rails root like this
for example: --
lib/plan.rb
module Plan
...some code...
end
and if you want to require it only in some model, say app/models/user.rb
do in user model
require "#{Rails.root}/lib/plan"
class User < ActiveRecord::Base
include Plan
end
if you want it to be available everywhere
one solution is given by #VecchiaSpugna
or you can create a ruby file in config/initializers folder
and require all file over there one by one
OR
try this
require '../../my_module/my_file'
instead of
require '../../my_module/my_file.rb'
You don't need to specify extension for a file in require.
I think there are two solutions.
1) Add the lib path to the search path.
In ruby:
$:.unshift('../../my_module/lib')
Then you can require 'my_module.rb'
I think Vecchia Spugna answer is the rails-version of my ruby-answer. (I'm not familiar with rails).
2) Another solution:
In your lib/my_module.rb you require my_file. This file is located relative to your my_module.rb? Then use require_relative:
require_relative './my_module/my_file'
Just chiming in because it took me forever to figure this out because very little solutions worked.
• I had to use plain old require. I put it in the config/application.rb file.
patching_file_path = File.expand_path("./lib", Dir.pwd)
Dir[patching_file_path+'/*.rb'].each {|file| require file }
• I also put a temporary puts "I'm Working! in the file I'm trying to require so I can check the console to see if it's actually loading.
• Also, if you're using spring loader, before you start your console you should do bin/spring stop in your terminal before you start your rails console. Otherwise, it won't load new files.
Similar to require_relative,
# inside lib/my_module.rb
require File.expand_path('./my_module/my_file', File.dirname(__FILE__))
This expands the path of current file directory and add the relative file path to be required.

How to open URLs in rails?

I'm trying to read in the html of a certain website.
Trying #something = open("http://www.google.com/") fails with the following error:
Errno::ENOENT in testController#show
No such file or directory - http://www.google.com/
Going to http://www.google.com/, I obviously see the site. What am I doing wrong?
Thanks!
You need to require 'open-uri' first to be able to open() remote paths.
See the docs for more info.
You should use a utility like Nokogiri to parse the returned content like so:
(From the Nokogiri site front page # http://nokogiri.org/)
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we’re interested in...
doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
# Do funky things with it using Nokogiri::XML::Node methods...
# Search for nodes by css
doc.css('h3.r a.l').each do |link|
puts link.content
end
will print to the screen:
Some Link

Resources