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
METHODS_WITH_BODY =
%w[patch post put].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Supported options:

body: Body of http post
headers: Hash of HTTP headers (combined with runtime_options.headers)
query: Hash of query parameters to add to url


22
23
24
25
26
27
# File 'lib/sinew/request.rb', line 22

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

Instance Attribute Details

#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

#perform(connection) ⇒ Object

run the request, return the result



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sinew/request.rb', line 38

def perform(connection)
  validate!

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

  body = options.delete(:body)

  fday_response = connection.send(method, uri, body, headers) do
    _1.options[:proxy] = proxy
  end

  Response.from_network(self, fday_response)
end

#proxyObject



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

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