Class: HTTParty::Request

Inherits:
Object
  • Object
show all
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,
  Net::HTTP::Move,
  Net::HTTP::Copy
]
SupportedURISchemes =
[URI::HTTP, URI::HTTPS, URI::Generic]
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/httparty/request.rb', line 32

def initialize(http_method, path, o = {})
  self.http_method = http_method
  self.path = path
  self.options = {
    limit: o.delete(:no_follow) ? 1 : 5,
    assume_utf16_is_big_endian: true,
    default_params: {},
    follow_redirects: true,
    parser: Parser,
    connection_adapter: ConnectionAdapter
  }.merge(o)
  set_basic_auth_from_uri
end

Instance Attribute Details

#http_methodObject

Returns the value of attribute http_method.



29
30
31
# File 'lib/httparty/request.rb', line 29

def http_method
  @http_method
end

#last_responseObject

Returns the value of attribute last_response.



29
30
31
# File 'lib/httparty/request.rb', line 29

def last_response
  @last_response
end

#last_uriObject

Returns the value of attribute last_uri.



29
30
31
# File 'lib/httparty/request.rb', line 29

def last_uri
  @last_uri
end

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/httparty/request.rb', line 29

def options
  @options
end

#pathObject

Returns the value of attribute path.



30
31
32
# File 'lib/httparty/request.rb', line 30

def path
  @path
end

#redirectObject

Returns the value of attribute redirect.



29
30
31
# File 'lib/httparty/request.rb', line 29

def redirect
  @redirect
end

Instance Method Details

#base_uriObject



80
81
82
# File 'lib/httparty/request.rb', line 80

def base_uri
  redirect ? "#{@last_uri.scheme}://#{@last_uri.host}" : options[:base_uri]
end

#connection_adapterObject



92
93
94
# File 'lib/httparty/request.rb', line 92

def connection_adapter
  options[:connection_adapter]
end

#formatObject



84
85
86
# File 'lib/httparty/request.rb', line 84

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

#parserObject



88
89
90
# File 'lib/httparty/request.rb', line 88

def parser
  options[:parser]
end

#perform(&block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/httparty/request.rb', line 96

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 options[:stream_body]
        block.call(fragment)
      end

      chunked_body = chunks.join
    end
  end

  handle_deflation unless http_method == Net::HTTP::Head
  handle_response(chunked_body, &block)
end

#raw_bodyObject



118
119
120
# File 'lib/httparty/request.rb', line 118

def raw_body
  @raw_request.body
end

#request_uri(uri) ⇒ Object



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

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

#uriObject



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

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

  new_uri = path.relative? ? URI.parse("#{base_uri}#{path}") : path.clone

  # 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, HTTPS or Generic"
  end

  @last_uri = new_uri
end