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::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
    elsif value.is_a?(Array)
      value.map {|v| "#{key}=#{URI.encode(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}
    else
      {key => value}.to_params
    end
  end.flatten.sort.join('&')
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



28
29
30
31
32
33
34
35
36
37
# File 'lib/httparty/request.rb', line 28

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

Instance Attribute Details

#http_methodObject

Returns the value of attribute http_method.



26
27
28
# File 'lib/httparty/request.rb', line 26

def http_method
  @http_method
end

#last_responseObject

Returns the value of attribute last_response.



26
27
28
# File 'lib/httparty/request.rb', line 26

def last_response
  @last_response
end

#optionsObject

Returns the value of attribute options.



26
27
28
# File 'lib/httparty/request.rb', line 26

def options
  @options
end

#pathObject

Returns the value of attribute path.



26
27
28
# File 'lib/httparty/request.rb', line 26

def path
  @path
end

#redirectObject

Returns the value of attribute redirect.



26
27
28
# File 'lib/httparty/request.rb', line 26

def redirect
  @redirect
end

Instance Method Details

#formatObject



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

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

#parserObject



62
63
64
# File 'lib/httparty/request.rb', line 62

def parser
  options[:parser]
end

#performObject



66
67
68
69
70
71
72
# File 'lib/httparty/request.rb', line 66

def perform
  validate
  setup_raw_request
  self.last_response = http.request(@raw_request)
  handle_deflation
  handle_response
end

#uriObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/httparty/request.rb', line 43

def uri
  new_uri = path.relative? ? URI.parse("#{options[: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

  new_uri
end