Class: Aitch::Request

Inherits:
Object show all
Defined in:
lib/aitch/request.rb

Constant Summary collapse

CONTENT_TYPE =
"Content-Type"
USER_AGENT =
"User-Agent"
ACCEPT_ENCODING =
"Accept-Encoding"
GZIP_DEFLATE =
"gzip,deflate"
HTTPS =
"https"
HEADER_SEPARATOR_RE =
/[-_]/
JSON_RE =
/\bjson\b/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Request

Returns a new instance of Request.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aitch/request.rb', line 18

def initialize(options)
  self.headers = {}
  self.options = {}
  self.redirects = []

  @_original_options = options.dup.freeze

  options.each do |name, value|
    public_send("#{name}=", value)
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/aitch/request.rb', line 5

def data
  @data
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/aitch/request.rb', line 5

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/aitch/request.rb', line 5

def options
  @options
end

#redirectsObject

Returns the value of attribute redirects.



5
6
7
# File 'lib/aitch/request.rb', line 5

def redirects
  @redirects
end

#request_methodObject

Returns the value of attribute request_method.



5
6
7
# File 'lib/aitch/request.rb', line 5

def request_method
  @request_method
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/aitch/request.rb', line 5

def url
  @url
end

Instance Method Details

#clientObject



59
60
61
62
63
64
65
# File 'lib/aitch/request.rb', line 59

def client
  @client ||= Net::HTTP.new(uri.host, uri.port).tap do |client|
    set_https(client)
    set_timeout(client)
    set_logger(client)
  end
end

#content_typeObject



44
45
46
47
# File 'lib/aitch/request.rb', line 44

def content_type
  headers[CONTENT_TYPE] ||
    options.fetch(:default_headers, {})[CONTENT_TYPE]
end

#content_type=(content_type) ⇒ Object



40
41
42
# File 'lib/aitch/request.rb', line 40

def content_type=(content_type)
  headers[CONTENT_TYPE] = content_type
end

#http_method_classObject



79
80
81
82
83
84
# File 'lib/aitch/request.rb', line 79

def http_method_class
  Net::HTTP.const_get(request_method.to_s.capitalize)
rescue NameError
  raise InvalidHTTPMethodError,
        "unexpected HTTP verb: #{request_method.inspect}"
end

#performObject



30
31
32
33
34
35
36
37
38
# File 'lib/aitch/request.rb', line 30

def perform
  response = Response.new(options, client.request(request))
  response.url = url
  response = follow_redirect(response)
  validate_response! response
  response
rescue timeout_exception
  raise RequestTimeoutError
end

#requestObject



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

def request
  @request ||= http_method_class.new(uri.request_uri).tap do |request|
    set_body(request) if request.request_body_permitted?
    set_user_agent(request)
    set_gzip(request)
    set_headers(request)
    set_credentials(request)
  end
end

#timeout_exceptionObject



139
140
141
# File 'lib/aitch/request.rb', line 139

def timeout_exception
  defined?(Net::ReadTimeout) ? Net::ReadTimeout : Timeout::Error
end

#uriObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aitch/request.rb', line 67

def uri
  @uri ||= begin
    normalized_url = if url.start_with?("/") && options[:base_url]
                       File.join(options[:base_url], url)
                     else
                       url
                     end

    URI.new(normalized_url, data, http_method_class::REQUEST_HAS_BODY)
  end
end