Class: HTTParty::Request

Inherits:
Object
  • Object
show all
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,
  Net::HTTP::Lock,
  Net::HTTP::Unlock,
]
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, path, o = {}) ⇒ Request

Returns a new instance of Request.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/httparty/request.rb', line 61

def initialize(http_method, path, o = {})
  @changed_hosts = false
  @credentials_sent = false

  self.http_method = http_method
  self.options = {
    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_methodObject

Returns the value of attribute http_method.



58
59
60
# File 'lib/httparty/request.rb', line 58

def http_method
  @http_method
end

#last_responseObject

Returns the value of attribute last_response.



58
59
60
# File 'lib/httparty/request.rb', line 58

def last_response
  @last_response
end

#last_uriObject

Returns the value of attribute last_uri.



58
59
60
# File 'lib/httparty/request.rb', line 58

def last_uri
  @last_uri
end

#optionsObject

Returns the value of attribute options.



58
59
60
# File 'lib/httparty/request.rb', line 58

def options
  @options
end

#pathObject

Returns the value of attribute path.



59
60
61
# File 'lib/httparty/request.rb', line 59

def path
  @path
end

#redirectObject

Returns the value of attribute redirect.



58
59
60
# File 'lib/httparty/request.rb', line 58

def redirect
  @redirect
end

Class Method Details

._load(data) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/httparty/request.rb', line 49

def self._load(data)
  http_method, path, options, last_response, last_uri, raw_request = Marshal.load(data)
  instance = new(http_method, path, options)
  instance.last_response = last_response
  instance.last_uri = last_uri
  instance.instance_variable_set("@raw_request", raw_request)
  instance
end

Instance Method Details

#_dump(_level) ⇒ Object



187
188
189
190
191
192
# File 'lib/httparty/request.rb', line 187

def _dump(_level)
  opts = options.dup
  opts.delete(:logger)
  opts.delete(:parser) if opts[:parser] && opts[:parser].is_a?(Proc)
  Marshal.dump([http_method, path, opts, last_response, @last_uri, @raw_request])
end

#base_uriObject



128
129
130
131
132
133
134
135
136
# File 'lib/httparty/request.rb', line 128

def base_uri
  if redirect
    base_uri = "#{@last_uri.scheme}://#{@last_uri.host}"
    base_uri = "#{base_uri}:#{@last_uri.port}" if @last_uri.port != 80
    base_uri
  else
    options[:base_uri] && HTTParty.normalize_base_uri(options[:base_uri])
  end
end

#connection_adapterObject



146
147
148
# File 'lib/httparty/request.rb', line 146

def connection_adapter
  options[:connection_adapter]
end

#formatObject



138
139
140
# File 'lib/httparty/request.rb', line 138

def format
  options[:format] || (format_from_mimetype(last_response['content-type']) if last_response)
end

#handle_unauthorized(&block) ⇒ Object



176
177
178
179
180
181
# File 'lib/httparty/request.rb', line 176

def handle_unauthorized(&block)
  return unless digest_auth? && response_unauthorized? && response_has_digest_auth_challenge?
  return if @credentials_sent
  @credentials_sent = true
  perform(&block)
end

#parserObject



142
143
144
# File 'lib/httparty/request.rb', line 142

def parser
  options[:parser]
end

#perform(&block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/httparty/request.rb', line 150

def perform(&block)
  validate
  setup_raw_request
  chunked_body = nil
  current_http = http

  self.last_response = current_http.request(@raw_request) do |http_response|
    if block
      chunks = []

      http_response.read_body do |fragment|
        encoded_fragment = encode_text(fragment, http_response['content-type'])
        chunks << encoded_fragment if !options[:stream_body]
        block.call ResponseFragment.new(encoded_fragment, http_response, current_http)
      end

      chunked_body = chunks.join
    end
  end

  handle_host_redirection if response_redirects?
  result = handle_unauthorized
  result ||= handle_response(chunked_body, &block)
  result
end

#raw_bodyObject



183
184
185
# File 'lib/httparty/request.rb', line 183

def raw_body
  @raw_request.body
end

#request_uri(uri) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/httparty/request.rb', line 92

def request_uri(uri)
  if uri.respond_to? :request_uri
    uri.request_uri
  else
    uri.path
  end
end

#uriObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/httparty/request.rb', line 100

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 = options[:uri_adapter].parse("#{@last_uri.scheme}:#{path}").normalize
  elsif path.relative?
    new_uri = options[:uri_adapter].parse("#{base_uri}#{path}").normalize
  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