Class: Aitch::Request
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
-
#data ⇒ Object
Returns the value of attribute data.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
-
#redirects ⇒ Object
Returns the value of attribute redirects.
-
#request_method ⇒ Object
Returns the value of attribute request_method.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #client ⇒ Object
- #content_type ⇒ Object
- #content_type=(content_type) ⇒ Object
- #http_method_class ⇒ Object
-
#initialize(options) ⇒ Request
constructor
A new instance of Request.
- #perform ⇒ Object
- #request ⇒ Object
- #timeout_exception ⇒ Object
- #uri ⇒ Object
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() self.headers = {} self. = {} self.redirects = [] @_original_options = .dup.freeze .each do |name, value| public_send("#{name}=", value) end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/aitch/request.rb', line 5 def data @data end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/aitch/request.rb', line 5 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/aitch/request.rb', line 5 def @options end |
#redirects ⇒ Object
Returns the value of attribute redirects.
5 6 7 |
# File 'lib/aitch/request.rb', line 5 def redirects @redirects end |
#request_method ⇒ Object
Returns the value of attribute request_method.
5 6 7 |
# File 'lib/aitch/request.rb', line 5 def request_method @request_method end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/aitch/request.rb', line 5 def url @url end |
Instance Method Details
#client ⇒ Object
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_type ⇒ Object
44 45 46 47 |
# File 'lib/aitch/request.rb', line 44 def content_type headers[CONTENT_TYPE] || .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_class ⇒ Object
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 |
#perform ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/aitch/request.rb', line 30 def perform response = Response.new(, client.request(request)) response.url = url response = follow_redirect(response) validate_response! response response rescue timeout_exception raise RequestTimeoutError end |
#request ⇒ Object
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_exception ⇒ Object
139 140 141 |
# File 'lib/aitch/request.rb', line 139 def timeout_exception defined?(Net::ReadTimeout) ? Net::ReadTimeout : Timeout::Error end |