Class: WebClient::Request

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

Constant Summary collapse

TYPES =
[:get, :post, :put, :delete]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Request

Returns a new instance of Request.



11
12
13
14
15
16
17
18
# File 'lib/web_client/request.rb', line 11

def initialize(options={}, &block)
  @type = options[:type] || :get
  @url = options[:url]
  @body = options[:body]
  @headers = options[:headers] || {}

  block.call(self) if block_given?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



8
9
10
# File 'lib/web_client/request.rb', line 8

def body
  @body
end

#headersObject

Returns the value of attribute headers.



9
10
11
# File 'lib/web_client/request.rb', line 9

def headers
  @headers
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/web_client/request.rb', line 6

def type
  @type
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/web_client/request.rb', line 7

def url
  @url
end

Instance Method Details

#to_httpObject



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

def to_http
  klass = eval("Net::HTTP::#{type.to_s.capitalize}")
  request = klass.new url
  request.set_form_data(body) if body.is_a? Hash
  request.body = body if body.is_a? String
  headers.each { |k, v| request.send("#{k}=", v) }
  request
end