Module: Requests::Utils
- Defined in:
- lib/requests/utils.rb
Class Method Summary collapse
- .default_headers ⇒ Object
- .default_user_agent ⇒ Object
- .encoding_from_headers(headers) ⇒ Object
- .env_proxies(url) ⇒ Object
- .guess_filename(obj, fallback) ⇒ Object
- .looks_like_html?(str) ⇒ Boolean
- .parse_header_links(value) ⇒ Object
- .truncate(str, length = 200) ⇒ Object
Class Method Details
.default_headers ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/requests/utils.rb', line 8 def default_headers CIHash.new( 'User-Agent' => default_user_agent, 'Accept' => '*/*', 'Accept-Encoding' => 'gzip, deflate' ) end |
.default_user_agent ⇒ Object
16 17 18 |
# File 'lib/requests/utils.rb', line 16 def default_user_agent "requests_ruby/#{Requests::VERSION}" end |
.encoding_from_headers(headers) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/requests/utils.rb', line 42 def encoding_from_headers(headers) ct = headers['content-type'] return 'UTF-8' unless ct m = ct.match(/charset=([\w\-]+)/i) m ? m[1] : 'UTF-8' end |
.env_proxies(url) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/requests/utils.rb', line 56 def env_proxies(url) host = URI.parse(url).host.to_s no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] if no_proxy list = no_proxy.split(',').map { |s| s.strip.downcase }.reject(&:empty?) return {} if list.include?('*') || list.any? { |p| host.downcase == p || host.downcase.end_with?(".#{p.sub(/\A\./, '')}") } end out = {} http = ENV['HTTP_PROXY'] || ENV['http_proxy'] https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] out['http'] = http if http && !http.empty? out['https'] = https if https && !https.empty? out rescue URI::InvalidURIError {} end |
.guess_filename(obj, fallback) ⇒ Object
38 39 40 41 |
# File 'lib/requests/utils.rb', line 38 def guess_filename(obj, fallback) return File.basename(obj.path) if obj.respond_to?(:path) && obj.path fallback.to_s end |
.looks_like_html?(str) ⇒ Boolean
53 54 55 |
# File 'lib/requests/utils.rb', line 53 def looks_like_html?(str) !!(str.to_s.lstrip =~ /\A(<!DOCTYPE html|<html)/i) end |
.parse_header_links(value) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/requests/utils.rb', line 19 def parse_header_links(value) return {} if value.nil? || value.empty? out = {} value.split(',').each do |part| seg = part.split(';') url = seg.shift.to_s.strip.gsub(/[<>]/, '') attrs = {} rel = nil seg.each do |a| k, v = a.strip.split('=', 2) next unless k v = v.to_s.gsub('"', '') attrs[k] = v rel = v if k == 'rel' end out[rel] = attrs.merge('url' => url) end out end |
.truncate(str, length = 200) ⇒ Object
48 49 50 51 52 |
# File 'lib/requests/utils.rb', line 48 def truncate(str, length = 200) s = str.to_s return s if s.length <= length "#{s[0, length]}... (#{s.length} chars total)" end |