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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sinew/request.rb', line 36

def perform
  validate!

  party_options = options.dup

  # merge proxy
  if proxy = self.proxy
    addr, port = proxy.split(':')
    party_options[:http_proxyaddr] = addr
    party_options[:http_proxyport] = port || 80
  end

  # now merge runtime_options
  party_options = party_options.merge(sinew.runtime_options.httparty_options)

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

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

#proxyObject



27
28
29
30
31
32
33
# File 'lib/sinew/request.rb', line 27

def proxy
  @proxy ||= begin
    if proxies = sinew.options[:proxy]
      proxies.split(',').sample
    end
  end
end