Class: Sinew::Request

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

Constant Summary collapse

HTML_ENTITIES =
HTMLEntities.new
VALID_METHODS =
%w[get post patch put delete head options].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sinew, method, url, options = {}) ⇒ Request

Options are largely compatible with HTTParty, except for :method.



19
20
21
22
23
24
25
# File 'lib/sinew/request.rb', line 19

def initialize(sinew, method, url, options = {})
  @sinew = sinew
  @method = method
  @options = options.dup
  @uri = parse_url(url)
  @cache_key = calculate_cache_key
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



16
17
18
# File 'lib/sinew/request.rb', line 16

def cache_key
  @cache_key
end

#methodObject (readonly)

Returns the value of attribute method.



16
17
18
# File 'lib/sinew/request.rb', line 16

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/sinew/request.rb', line 16

def options
  @options
end

#sinewObject (readonly)

Returns the value of attribute sinew.



16
17
18
# File 'lib/sinew/request.rb', line 16

def sinew
  @sinew
end

#uriObject (readonly)

Returns the value of attribute uri.



16
17
18
# File 'lib/sinew/request.rb', line 16

def uri
  @uri
end

Instance Method Details

#performObject

run the request, return the result



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sinew/request.rb', line 28

def perform
  validate!

  # merge optons
  options = self.options.merge(sinew.runtime_options.httparty_options)

  # merge headers
  headers = sinew.runtime_options.headers
  headers = headers.merge(options[:headers]) if options[:headers]
  options[:headers] = headers

  party_response = HTTParty.send(method, uri, options)
  Response.from_network(self, party_response)
end