Class: HTTParty::Request
- Inherits:
-
Object
- Object
- HTTParty::Request
- Defined in:
- lib/httparty/request.rb
Overview
:nodoc:
Constant Summary collapse
- SupportedHTTPMethods =
[ Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Patch, Net::HTTP::Put, Net::HTTP::Delete, Net::HTTP::Head, Net::HTTP::Options ]
- SupportedURISchemes =
[URI::HTTP, URI::HTTPS]
- NON_RAILS_QUERY_STRING_NORMALIZER =
Proc.new do |query| Array(query).map do |key, value| if value.nil? key.to_s elsif value.is_a?(Array) value.map {|v| "#{key}=#{URI.encode(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"} else HashConversions.to_params(key => value) end end.flatten.sort.join('&') end
Instance Attribute Summary collapse
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#last_response ⇒ Object
Returns the value of attribute last_response.
-
#last_uri ⇒ Object
Returns the value of attribute last_uri.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#redirect ⇒ Object
Returns the value of attribute redirect.
Instance Method Summary collapse
- #base_uri ⇒ Object
- #connection_adapter ⇒ Object
- #format ⇒ Object
-
#initialize(http_method, path, o = {}) ⇒ Request
constructor
A new instance of Request.
- #parser ⇒ Object
- #perform(&block) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(http_method, path, o = {}) ⇒ Request
Returns a new instance of Request.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/httparty/request.rb', line 29 def initialize(http_method, path, o={}) self.http_method = http_method self.path = path self. = { :limit => o.delete(:no_follow) ? 1 : 5, :default_params => {}, :follow_redirects => true, :parser => Parser, :connection_adapter => ConnectionAdapter }.merge(o) end |
Instance Attribute Details
#http_method ⇒ Object
Returns the value of attribute http_method.
27 28 29 |
# File 'lib/httparty/request.rb', line 27 def http_method @http_method end |
#last_response ⇒ Object
Returns the value of attribute last_response.
27 28 29 |
# File 'lib/httparty/request.rb', line 27 def last_response @last_response end |
#last_uri ⇒ Object
Returns the value of attribute last_uri.
27 28 29 |
# File 'lib/httparty/request.rb', line 27 def last_uri @last_uri end |
#options ⇒ Object
Returns the value of attribute options.
27 28 29 |
# File 'lib/httparty/request.rb', line 27 def @options end |
#path ⇒ Object
Returns the value of attribute path.
27 28 29 |
# File 'lib/httparty/request.rb', line 27 def path @path end |
#redirect ⇒ Object
Returns the value of attribute redirect.
27 28 29 |
# File 'lib/httparty/request.rb', line 27 def redirect @redirect end |
Instance Method Details
#base_uri ⇒ Object
64 65 66 |
# File 'lib/httparty/request.rb', line 64 def base_uri redirect ? "#{@last_uri.scheme}://#{@last_uri.host}" : [:base_uri] end |
#connection_adapter ⇒ Object
76 77 78 |
# File 'lib/httparty/request.rb', line 76 def connection_adapter [:connection_adapter] end |
#format ⇒ Object
68 69 70 |
# File 'lib/httparty/request.rb', line 68 def format [:format] || (format_from_mimetype(last_response['content-type']) if last_response) end |
#parser ⇒ Object
72 73 74 |
# File 'lib/httparty/request.rb', line 72 def parser [:parser] end |
#perform(&block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/httparty/request.rb', line 80 def perform(&block) validate setup_raw_request chunked_body = nil self.last_response = http.request(@raw_request) do |http_response| if block chunks = [] http_response.read_body do |fragment| chunks << fragment block.call(fragment) end chunked_body = chunks.join end end handle_deflation handle_response(chunked_body) end |
#uri ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/httparty/request.rb', line 49 def uri new_uri = path.relative? ? URI.parse("#{base_uri}#{path}") : path # avoid double query string on redirects [#12] unless redirect new_uri.query = query_string(new_uri) end unless SupportedURISchemes.include? new_uri.class raise UnsupportedURIScheme, "'#{new_uri}' Must be HTTP or HTTPS" end @last_uri = new_uri end |