open-uri

How to fix a deadlock caused by open

本小妞迷上赌 提交于 2019-11-29 18:03:55
I have is a deadlock, but I am not using any threads in my program. Plus, the error only happens about once every 1000 to 1500 function calls, making it very difficult to pinpoint and correct. Here is the complete error message when the issue occurs: /usr/lib/ruby/2.3.0/timeout.rb:95:in `join': No live threads left. Deadlock? (fatal) from /usr/lib/ruby/2.3.0/timeout.rb:95:in `ensure in block in timeout' from /usr/lib/ruby/2.3.0/timeout.rb:95:in `block in timeout' from /usr/lib/ruby/2.3.0/timeout.rb:101:in `timeout' from /usr/lib/ruby/2.3.0/net/http.rb:878:in `connect' from /usr/lib/ruby/2.3.0

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

痞子三分冷 提交于 2019-11-29 14:17:05
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. 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, different behavior in 1.9.3 Ruby 2.0.0 require 'net/http' uri = URI('http://github.com/ruby') http_request =

How to get HTTP headers before downloading with Ruby's OpenUri

我是研究僧i 提交于 2019-11-29 06:44:42
I am currently using OpenURI to download a file in Ruby. Unfortunately, it seems impossible to get the HTTP headers without downloading the full file: open(base_url, :content_length_proc => lambda {|t| if t && 0 < t pbar = ProgressBar.create(:total => t) end }, :progress_proc => lambda {|s| pbar.progress = s if pbar }) {|io| puts io.size puts io.meta['content-disposition'] } Running the code above shows that it first downloads the full file and only then prints the header I need. Is there a way to get the headers before the full file is downloaded, so I can cancel the download if the headers

Rails 3.2.17 Runtime Error Redirection Forbidden facebook

两盒软妹~` 提交于 2019-11-29 06:13:54
问题 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

What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems?

久未见 提交于 2019-11-29 03:07:01
It seems like both of these gems perform very similar tasks. Can anyone give examples of where one gem would be more useful than the other? I don't have specific code that I'm referring to, I'm more wondering about general use cases for each gem. I know this is a short question, I will fill in the blanks upon request. Thanks. The reason they look like they perform similar tasks is OpenURI is a wrapper for Net::HTTP, Net::HTTPS, and Net::FTP. Usually, unless you feel you need a lower level interface, using OpenURI is better as you can get by with less code. Using OpenURI you can open a URL/URI

Ruby open-uri redirect forbidden

蓝咒 提交于 2019-11-29 00:58:55
I have this simple html parser(for learning purposes) that I have been working on.: require 'open-uri' puts "Enter URL to parse HTML: " url = gets.chomp puts "Enter tag to parse from: " tag = gets.chomp response = open(url).read title1 = response.index(tag) title2 = response.index(tag.insert(1,'/')) -1 result = response[(title1 + tag.length - 1)..title2] print result and when I input http://twitter.com , I get this error message: ERROR: `open_loop': redirection forbidden: http://twitter.com -> https://twitter.com/ (RuntimeError) from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb

Retrieve contents of URL as string

╄→гoц情女王★ 提交于 2019-11-29 00:08:35
问题 For tedious reasons to do with Hpricot, I need to write a function that is passed a URL, and returns the whole contents of the page as a single string. I'm close. I know I need to use OpenURI, and it should look something like this: require 'open-uri' open(url) { # do something mysterious here to get page_string } puts page_string Can anyone suggest what I need to add? 回答1: The open method passes an IO representation of the resource to your block when it yields. You can read from it using the

Fast way to get remote image dimensions

时光怂恿深爱的人放手 提交于 2019-11-28 18:45:42
I'm using the imagesize gem to check the sizes of remote images and then only push images that are big enough into an array. require 'open-uri' require 'image_size' data = Nokogiri::HTML(open(url)) images = [] forcenocache = Time.now.to_i # No cache because jquery load event doesn't fire for cached images data.css("img").each do |image| image_path = URI.join(site, URI.encode(image[:src])) open(image_path, "rb") do |fh| image_size = ImageSize.new(fh.read).get_size() unless image_size[0] < 200 || image_size[1] < 100 image_element = "<img src=\"#{image_path}?#{forcenocache}\">" images.push(image

How to fix a deadlock caused by open

柔情痞子 提交于 2019-11-28 13:14:06
问题 I have is a deadlock, but I am not using any threads in my program. Plus, the error only happens about once every 1000 to 1500 function calls, making it very difficult to pinpoint and correct. Here is the complete error message when the issue occurs: /usr/lib/ruby/2.3.0/timeout.rb:95:in `join': No live threads left. Deadlock? (fatal) from /usr/lib/ruby/2.3.0/timeout.rb:95:in `ensure in block in timeout' from /usr/lib/ruby/2.3.0/timeout.rb:95:in `block in timeout' from /usr/lib/ruby/2.3.0

How do I make a POST request with open-uri?

早过忘川 提交于 2019-11-28 05:50:00
Is it possible to make a POST request from Ruby with open-uri? Unfortunately open-uri only supports the GET verb. You can either drop down a level and use net/http , or use rest-open-uri , which was designed to support POST and other verbs. You can do gem install rest-open-uri to install it. Venkat D. require 'open-uri' require 'net/http' params = {'param1' => 'value1', 'param2' => 'value2'} url = URI.parse('http://thewebsite.com/thepath') resp, data = Net::HTTP.post_form(url, params) puts resp.inspect puts data.inspect It worked for me :) tomtaylor I'd also really recommend rest-client . It's