Class: HTTParty::Request
- Inherits:
-
Object
- Object
- HTTParty::Request
- Defined in:
- lib/httparty/request.rb,
lib/httparty/request/body.rb,
lib/httparty/request/multipart_boundary.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Body, MultipartBoundary
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, Net::HTTP::Move, Net::HTTP::Copy, Net::HTTP::Mkcol, ]
- SupportedURISchemes =
['http', 'https', 'webcal', nil]
- NON_RAILS_QUERY_STRING_NORMALIZER =
proc do |query| Array(query).sort_by { |a| a[0].to_s }.map do |key, value| if value.nil? key.to_s elsif value.respond_to?(:to_ary) value.to_ary.map {|v| "#{key}=#{ERB::Util.url_encode(v.to_s)}"} else HashConversions.to_params(key => value) end end.flatten.join('&') end
- JSON_API_QUERY_STRING_NORMALIZER =
proc do |query| Array(query).sort_by { |a| a[0].to_s }.map do |key, value| if value.nil? key.to_s elsif value.respond_to?(:to_ary) values = value.to_ary.map{|v| ERB::Util.url_encode(v.to_s)} "#{key}=#{values.join(',')}" else HashConversions.to_params(key => value) end end.flatten.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
- #handle_unauthorized(&block) ⇒ Object
-
#initialize(http_method, path, o = {}) ⇒ Request
constructor
A new instance of Request.
- #parser ⇒ Object
- #perform(&block) ⇒ Object
- #raw_body ⇒ Object
- #request_uri(uri) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(http_method, path, o = {}) ⇒ Request
Returns a new instance of Request.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/httparty/request.rb', line 49 def initialize(http_method, path, o = {}) @changed_hosts = false @credentials_sent = false self.http_method = http_method self. = { limit: o.delete(:no_follow) ? 1 : 5, assume_utf16_is_big_endian: true, default_params: {}, follow_redirects: true, parser: Parser, uri_adapter: URI, connection_adapter: ConnectionAdapter }.merge(o) self.path = path set_basic_auth_from_uri end |
Instance Attribute Details
#http_method ⇒ Object
Returns the value of attribute http_method
46 47 48 |
# File 'lib/httparty/request.rb', line 46 def http_method @http_method end |
#last_response ⇒ Object
Returns the value of attribute last_response
46 47 48 |
# File 'lib/httparty/request.rb', line 46 def last_response @last_response end |
#last_uri ⇒ Object
Returns the value of attribute last_uri
46 47 48 |
# File 'lib/httparty/request.rb', line 46 def last_uri @last_uri end |
#options ⇒ Object
Returns the value of attribute options
46 47 48 |
# File 'lib/httparty/request.rb', line 46 def @options end |
#path ⇒ Object
Returns the value of attribute path
47 48 49 |
# File 'lib/httparty/request.rb', line 47 def path @path end |
#redirect ⇒ Object
Returns the value of attribute redirect
46 47 48 |
# File 'lib/httparty/request.rb', line 46 def redirect @redirect end |
Instance Method Details
#base_uri ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/httparty/request.rb', line 116 def base_uri if redirect base_uri = "#{@last_uri.scheme}://#{@last_uri.host}" base_uri += ":#{@last_uri.port}" if @last_uri.port != 80 base_uri else [:base_uri] && HTTParty.normalize_base_uri([:base_uri]) end end |
#connection_adapter ⇒ Object
134 135 136 |
# File 'lib/httparty/request.rb', line 134 def connection_adapter [:connection_adapter] end |
#format ⇒ Object
126 127 128 |
# File 'lib/httparty/request.rb', line 126 def format [:format] || (format_from_mimetype(last_response['content-type']) if last_response) end |
#handle_unauthorized(&block) ⇒ Object
163 164 165 166 167 168 |
# File 'lib/httparty/request.rb', line 163 def (&block) return unless digest_auth? && && response_has_digest_auth_challenge? return if @credentials_sent @credentials_sent = true perform(&block) end |
#parser ⇒ Object
130 131 132 |
# File 'lib/httparty/request.rb', line 130 def parser [:parser] end |
#perform(&block) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/httparty/request.rb', line 138 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 unless [:stream_body] block.call(fragment) end chunked_body = chunks.join end end handle_host_redirection if response_redirects? result = result ||= handle_response(chunked_body, &block) result end |
#raw_body ⇒ Object
170 171 172 |
# File 'lib/httparty/request.rb', line 170 def raw_body @raw_request.body end |
#request_uri(uri) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/httparty/request.rb', line 80 def request_uri(uri) if uri.respond_to? :request_uri uri.request_uri else uri.path end end |
#uri ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/httparty/request.rb', line 88 def uri if redirect && path.relative? && path.path[0] != "/" last_uri_host = @last_uri.path.gsub(/[^\/]+$/, "") path.path = "/#{path.path}" if last_uri_host[-1] != "/" path.path = last_uri_host + path.path end if path.relative? && path.host new_uri = [:uri_adapter].parse("#{@last_uri.scheme}:#{path}") elsif path.relative? new_uri = [:uri_adapter].parse("#{base_uri}#{path}") else new_uri = path.clone end # avoid double query string on redirects [#12] unless redirect new_uri.query = query_string(new_uri) end unless SupportedURISchemes.include? new_uri.scheme raise UnsupportedURIScheme, "'#{new_uri}' Must be HTTP, HTTPS or Generic" end @last_uri = new_uri end |