open-uri

404 error with open-uri in a rake task… what's causing it?

纵然是瞬间 提交于 2019-12-04 00:17:21
I have a rake task that fetches JSON data from an API, parses it, and saves it to the database: task :embedly => :environment do require 'json' require 'uri' require 'open-uri' Video.all.each do |video| json_stream = open("http://api.embed.ly/1/oembed?key=08b652e6b3ea11e0ae3f4040d3dc5c07&url=#{video.video_url}&maxwidth=525") ruby_hash = JSON.parse(json_stream.read) thumbnail_url = ruby_hash['thumbnail_url'] embed_code = ruby_hash['html'] video.update_attributes(:thumbnail_url => thumbnail_url, :embed_code => embed_code) end end I get this error in the stack trace when I run the rake task and I

Why does Ruby open-uri's open return a StringIO in my unit test, but a FileIO in my controller?

一曲冷凌霜 提交于 2019-12-03 18:34:42
问题 I inherited a Rails 2.2.2 app that stores user-uploaded images on Amazon S3. The attachment_fu-based Photo model offers a rotate method that uses open-uri to retrieve the image from S3 and MiniMagick to perform the rotation. The rotate method contains this line to retrieve the image for use with MiniMagick: temp_image = MiniMagick::Image.from_file(open(self.public_filename).path) self.public_filename returns something like http://s3.amazonaws.com/bucketname/photos/98/photo.jpg Retrieving the

Is there a workaround to open URLs containing underscores in Ruby?

大憨熊 提交于 2019-12-03 08:14:38
问题 I'm using open-uri to open URLs. resp = open("http://sub_domain.domain.com") If it contains underscore I get an error: URI::InvalidURIError: the scheme http does not accept registry part: sub_domain.domain.com (or bad hostname?) I understand that this is because according to RFC URLs can contain only letters and numbers. Is there any workaround? 回答1: This looks like a bug in URI, and uri-open, HTTParty and many other gems make use of URI.parse. Here's a workaround: require 'net/http' require

Rails - How to send an image from a controller

[亡魂溺海] 提交于 2019-12-03 04:22:59
问题 in my rails app, I need to pass back a image. I have a 1x1.gif tracking pixel in my route as follows: match "/_ctrack.gif" => "email_tracking_pixels#my_method" In the controller: def my_method send_data open('https://www.xxxxxx.com/images/1x1_transparent.gif') {|f| f.read }, :filename => '1x1_transparent.gif', :type => 'image/gif' end The problem is that for some reason sometimes this times outs. with the following error: 2011-03-07T20:08:36-08:00 heroku[router]: Error H12 (Request timeout) -

Rails - How to send an image from a controller

不想你离开。 提交于 2019-12-02 17:37:31
in my rails app, I need to pass back a image. I have a 1x1.gif tracking pixel in my route as follows: match "/_ctrack.gif" => "email_tracking_pixels#my_method" In the controller: def my_method send_data open('https://www.xxxxxx.com/images/1x1_transparent.gif') {|f| f.read }, :filename => '1x1_transparent.gif', :type => 'image/gif' end The problem is that for some reason sometimes this times outs. with the following error: 2011-03-07T20:08:36-08:00 heroku[router]: Error H12 (Request timeout) -> GET www.xxxxxxx.com/images/1x1_transparent.gif dyno=web.1 queue=0 wait=0ms service=0ms bytes=0 2011

open-uri returning ASCII-8BIT from webpage encoded in iso-8859

大憨熊 提交于 2019-12-01 04:43:07
I am using open-uri to read a webpage which claims to be encoded in iso-8859-1. When I read the contents of the page, open-uri returns a string encoded in ASCII-8BIT. open("http://www.nigella.com/recipes/view/DEVILS-FOOD-CAKE-5310") {|f| p f.content_type, f.charset, f.read.encoding } => ["text/html", "iso-8859-1", #<Encoding:ASCII-8BIT>] I am guessing this is because the webpage has the byte (or character) \x92 which is not a valid iso-8859 character. http://en.wikipedia.org/wiki/ISO/IEC_8859-1 . I need to store webpages as utf-8 encoded files. Any ideas on how to deal with webpage where the

open-uri returning ASCII-8BIT from webpage encoded in iso-8859

ぐ巨炮叔叔 提交于 2019-12-01 03:01:29
问题 I am using open-uri to read a webpage which claims to be encoded in iso-8859-1. When I read the contents of the page, open-uri returns a string encoded in ASCII-8BIT. open("http://www.nigella.com/recipes/view/DEVILS-FOOD-CAKE-5310") {|f| p f.content_type, f.charset, f.read.encoding } => ["text/html", "iso-8859-1", #<Encoding:ASCII-8BIT>] I am guessing this is because the webpage has the byte (or character) \x92 which is not a valid iso-8859 character. http://en.wikipedia.org/wiki/ISO/IEC_8859

Getting Headers from a Ruby Net::HTTP Request before making the request

杀马特。学长 韩版系。学妹 提交于 2019-11-30 09:16:15
问题 In Ruby, how can I get hold of the HTTP Request Headers that will be sent by a net/http(s) or open-uri request BEFORE it actually makes the request. In some cases, headers are used when creating a signed string in a URI. Surely there is some way to acquire the request headers that will be sent. These should include the "Host:" header for example. 回答1: see http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html#label-Setting+Headers Works well in ruby 2.0.0 - but you are correct,

Rails 3.2.17 Runtime Error Redirection Forbidden facebook

巧了我就是萌 提交于 2019-11-30 06:47:44
I have this code I use to get avatars from Facebook... if auth.info.image.present? user.update_attribute(:avatar, URI.parse(auth.info.image)) end When I try to load the code now I get this error: A RuntimeError occurred in authentications#create: redirection forbidden: http://graph.facebook.com/672086173/picture?type=square -> https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t5.0-1/1086349_672086173_156380036_q.jpg /home/ubuntu/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/open-uri.rb:223:in `open_loop' I understand that this is a problem with Open-URI not allowing HTTP to HTTPS redirections...

Why does Ruby open-uri's open return a StringIO in my unit test, but a FileIO in my controller?

北城余情 提交于 2019-11-29 22:56:48
I inherited a Rails 2.2.2 app that stores user-uploaded images on Amazon S3. The attachment_fu-based Photo model offers a rotate method that uses open-uri to retrieve the image from S3 and MiniMagick to perform the rotation. The rotate method contains this line to retrieve the image for use with MiniMagick: temp_image = MiniMagick::Image.from_file(open(self.public_filename).path) self.public_filename returns something like http://s3.amazonaws.com/bucketname/photos/98/photo.jpg Retrieving the image and rotating it work just fine in the running application in production and development. However,