open-uri

How to specify “http request header” in OpenURI

纵然是瞬间 提交于 2021-02-06 06:58:53
问题 I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header. Any idea how to do this? 回答1: According to the documentation, you can pass a hash of http headers as the second argument to open : open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... } 来源: https://stackoverflow.com/questions/7478841/how-to-specify-http-request

How to specify “http request header” in OpenURI

若如初见. 提交于 2021-02-06 06:58:12
问题 I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header. Any idea how to do this? 回答1: According to the documentation, you can pass a hash of http headers as the second argument to open : open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... } 来源: https://stackoverflow.com/questions/7478841/how-to-specify-http-request

How to specify “http request header” in OpenURI

不问归期 提交于 2021-02-06 06:58:10
问题 I am trying to call a URL using Ruby's OpenURI gem, however it needs me to pass certain values inside its HTTP request header. Any idea how to do this? 回答1: According to the documentation, you can pass a hash of http headers as the second argument to open : open("http://www.ruby-lang.org/en/", "User-Agent" => "Ruby/#{RUBY_VERSION}", "From" => "foo@bar.invalid", "Referer" => "http://www.ruby-lang.org/") {|f| # ... } 来源: https://stackoverflow.com/questions/7478841/how-to-specify-http-request

Want to save facebook image into my rails app

佐手、 提交于 2021-01-01 07:16:40
问题 I am using ominauth to signup users in my rails app.i also have a basic signup option with email address also. So for both users i have to save upload user picture in my file system I have regular file upload for my email users but for FB users i want their pics to save in my disc so i can use same code and not use FB graph link while displaying. FB sends images in this format with graph API http://graph.facebook.com/100007619644580/picture?type=large How can i save that in my public folder

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

北战南征 提交于 2020-01-01 08:04:09
问题 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

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

情到浓时终转凉″ 提交于 2020-01-01 08:04:09
问题 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

In Ruby/Rails, how can I encode/escape special characters in URLs?

旧街凉风 提交于 2019-12-30 01:55:10
问题 How do I encode or 'escape' the URL before I use OpenURI to open(url) ? We're using OpenURI to open a remote url and return the xml: getresult = open(url).read The problem is the URL contains some user-input text that contains spaces and other characters, including "+", "&", "?", etc. potentially, so we need to safely escape the URL. I saw lots of examples when using Net::HTTP, but have not found any for OpenURI. We also need to be able to un-escape a similar string we receive in a session

In Ruby/Rails, how can I encode/escape special characters in URLs?

泄露秘密 提交于 2019-12-30 01:55:02
问题 How do I encode or 'escape' the URL before I use OpenURI to open(url) ? We're using OpenURI to open a remote url and return the xml: getresult = open(url).read The problem is the URL contains some user-input text that contains spaces and other characters, including "+", "&", "?", etc. potentially, so we need to safely escape the URL. I saw lots of examples when using Net::HTTP, but have not found any for OpenURI. We also need to be able to un-escape a similar string we receive in a session

Prevent timeout when opening large files from URL

时光毁灭记忆、已成空白 提交于 2019-12-24 08:05:42
问题 I am writing a Ruby 1.8.7 script which has to request really large XML files(1 - 5MB) from server which is quite slow(1min30sec for 1MB). The requested file is written to disk. I set the timeout in my script to some ridiculous amount of seconds, since I really want to get the file, not just move on if it takes too long. Still with the high amount of seconds I keep getting timeouts. Is there a best practice for this? right now I use open(DIR + "" + number + "" + ".xml", 'wb') do |file| begin

Rails open-uri breaking on path

放肆的年华 提交于 2019-12-23 12:29:43
问题 I'm trying to get a url using open-uri. my code is outrageously simple url = open("http://localhost:3000/descriptions") return render :text => url.to_json When I run this code, I get a timeout error. When I request just "http://localhost:3000", I get my home page (which is correct). So it seems this is breaking on the path somewhere. I've tried using net-http with uri (not sure if that would make a difference) and I still have the same issue. I want to get a path and a query, but I can't seem