Guest User

Untitled

a guest
May 20th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. # なぜか timeout error が起こるので wget で代用
  2. class Gem::RemoteFetcher
  3. alias open_uri_or_path_orig open_uri_or_path
  4. def open_uri_or_path(uri, last_modified = nil, head = false, depth = 0)
  5. raise "block is dead" if block_given?
  6.  
  7. # HEADメソッドならオリジナルに任せる
  8. return open_uri_or_path_orig(uri, last_modified, head, depth) if head
  9.  
  10. uri = URI.parse uri unless URI::Generic === uri
  11. unless ['http', 'https', 'file'].include?(uri.scheme)
  12. raise ArgumentError, 'uri scheme is invalid'
  13. end
  14.  
  15. # file://プロトコルならオリジナルに任せる
  16. return open_uri_or_path_orig(uri, last_modified, head, depth) if uri.scheme == 'file'
  17.  
  18. response = nil
  19.  
  20. require 'tmpdir'
  21. Dir.mktmpdir do |dir|
  22. cmd = "wget"
  23.  
  24. unless uri.nil? || uri.user.nil? || uri.user.empty? then
  25. cmd += " --user=#{uri.user}"
  26. cmd += " --password=#{uri.password}" if uri.password.to_s.size > 0
  27. end
  28.  
  29. fn = File.join("#{dir}", "#{rand}")
  30.  
  31. ua = "RubyGems/#{Gem::RubyGemsVersion} #{Gem::Platform.local}"
  32. ua << " Ruby/#{RUBY_VERSION} (#{RUBY_RELEASE_DATE}"
  33. ua << " patchlevel #{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
  34. ua << ")"
  35. cmd += " -U '#{ua}'"
  36. cmd += " -O '#{fn}'"
  37. cmd += " -q '#{uri}'"
  38. system(cmd)
  39.  
  40. response = File.open(fn, "rb"){ |f| f.read }
  41. end
  42. response
  43. end
  44. end
Add Comment
Please, Sign In to add comment