Object
This class is used internally by RestClient to send the request, but you can also access it internally if you’d like to use a method not directly supported by the main API. For example:
RestClient::Request.execute(:method => :head, :url => 'http://example.com')
backwards compatibility
# File lib/restclient/request.rb, line 11 11: def self.execute(args) 12: new(args).execute 13: end
# File lib/restclient/request.rb, line 15 15: def initialize(args) 16: @method = args[:method] or raise ArgumentError, "must pass :method" 17: @url = args[:url] or raise ArgumentError, "must pass :url" 18: @headers = args[:headers] || {} 19: @cookies = @headers.delete(:cookies) || args[:cookies] || {} 20: @payload = process_payload(args[:payload]) 21: @user = args[:user] 22: @password = args[:password] 23: @timeout = args[:timeout] 24: @open_timeout = args[:open_timeout] 25: end
# File lib/restclient/request.rb, line 148 148: def decode(content_encoding, body) 149: if content_encoding == 'gzip' and not body.empty? 150: Zlib::GzipReader.new(StringIO.new(body)).read 151: elsif content_encoding == 'deflate' 152: Zlib::Inflate.new.inflate(body) 153: else 154: body 155: end 156: end
# File lib/restclient/request.rb, line 182 182: def default_headers 183: { :accept => 'application/xml', :accept_encoding => 'gzip, deflate' } 184: end
# File lib/restclient/request.rb, line 170 170: def display_log(msg) 171: return unless log_to = RestClient.log 172: 173: if log_to == 'stdout' 174: STDOUT.puts msg 175: elsif log_to == 'stderr' 176: STDERR.puts msg 177: else 178: File.open(log_to, 'a') { |f| f.puts msg } 179: end 180: end
# File lib/restclient/request.rb, line 27 27: def execute 28: execute_inner 29: rescue Redirect => e 30: @url = e.url 31: execute 32: end
# File lib/restclient/request.rb, line 34 34: def execute_inner 35: uri = parse_url_with_auth(url) 36: transmit uri, net_http_request_class(method).new(uri.request_uri, make_headers(headers)), payload 37: end
# File lib/restclient/request.rb, line 39 39: def make_headers(user_headers) 40: unless @cookies.empty? 41: user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ') 42: end 43: 44: default_headers.merge(user_headers).inject({}) do |final, (key, value)| 45: final[key.to_s.gsub(/_/, '-').capitalize] = value.to_s 46: final 47: end 48: end
# File lib/restclient/request.rb, line 50 50: def net_http_class 51: if RestClient.proxy 52: proxy_uri = URI.parse(RestClient.proxy) 53: Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) 54: else 55: Net::HTTP 56: end 57: end
# File lib/restclient/request.rb, line 59 59: def net_http_request_class(method) 60: Net::HTTP.const_get(method.to_s.capitalize) 61: end
# File lib/restclient/request.rb, line 63 63: def parse_url(url) 64: url = "http://#{url}" unless url.match(/^http/) 65: URI.parse(url) 66: end
# File lib/restclient/request.rb, line 68 68: def parse_url_with_auth(url) 69: uri = parse_url(url) 70: @user = uri.user if uri.user 71: @password = uri.password if uri.password 72: uri 73: end
# File lib/restclient/request.rb, line 75 75: def process_payload(p=nil, parent_key=nil) 76: unless p.is_a?(Hash) 77: p 78: else 79: @headers[:content_type] ||= 'application/x-www-form-urlencoded' 80: p.keys.map do |k| 81: key = parent_key ? "#{parent_key}[#{k}]" : k 82: if p[k].is_a? Hash 83: process_payload(p[k], key) 84: else 85: value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) 86: "#{key}=#{value}" 87: end 88: end.join("&") 89: end 90: end
# File lib/restclient/request.rb, line 124 124: def process_result(res) 125: if res.code =~ /\A2\d{2}\z/ 126: decode res['content-encoding'], res.body if res.body 127: elsif %(301 302 303).include? res.code 128: url = res.header['Location'] 129: 130: if url !~ /^http/ 131: uri = URI.parse(@url) 132: uri.path = "/#{url}".squeeze('/') 133: url = uri.to_s 134: end 135: 136: raise Redirect, url 137: elsif res.code == "304" 138: raise NotModified, res 139: elsif res.code == "401" 140: raise Unauthorized, res 141: elsif res.code == "404" 142: raise ResourceNotFound, res 143: else 144: raise RequestFailed, res 145: end 146: end
# File lib/restclient/request.rb, line 158 158: def request_log 159: out = [] 160: out << "RestClient.#{method} #{url.inspect}" 161: out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload 162: out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty? 163: out.join(', ') 164: end
# File lib/restclient/request.rb, line 166 166: def response_log(res) 167: "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{(res.body) ? res.body.size : nil} bytes" 168: end
# File lib/restclient/request.rb, line 120 120: def setup_credentials(req) 121: req.basic_auth(user, password) if user 122: end
# File lib/restclient/request.rb, line 92 92: def transmit(uri, req, payload) 93: setup_credentials(req) 94: 95: net = net_http_class.new(uri.host, uri.port) 96: net.use_ssl = uri.is_a?(URI::HTTPS) 97: net.verify_mode = OpenSSL::SSL::VERIFY_NONE 98: net.read_timeout = @timeout if @timeout 99: net.open_timeout = @open_timeout if @open_timeout 100: 101: display_log request_log 102: 103: net.start do |http| 104: res = http.request(req, payload) 105: display_log response_log(res) 106: string = process_result(res) 107: 108: if string or @method == :head 109: Response.new(string, res) 110: else 111: nil 112: end 113: end 114: rescue EOFError 115: raise RestClient::ServerBrokeConnection 116: rescue Timeout::Error 117: raise RestClient::RequestTimeout 118: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.