Class: Restfully::HTTP::Request

Inherits:
Object
  • Object
show all
Includes:
Headers, Parsing
Defined in:
lib/restfully/http/request.rb

Constant Summary

Constants included from Parsing

Parsing::PARSERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Headers

#sanitize_http_headers

Methods included from Parsing

#select_parser_for, #serialize, #unserialize

Constructor Details

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

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
# File 'lib/restfully/http/request.rb', line 10

def initialize(url, options = {})
  options = options.symbolize_keys
  @uri = url.kind_of?(URI) ? url : URI.parse(url)
  @headers = sanitize_http_headers(options.delete(:headers) || {})
  if query = options.delete(:query)
    @uri.query = [@uri.query, query.to_params].compact.join("&")
  end
  @body = options.delete(:body)
  @retries = 0
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#retriesObject

Returns the value of attribute retries.



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

def retries
  @retries
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#add_headers(headers = {}) ⇒ Object



37
38
39
# File 'lib/restfully/http/request.rb', line 37

def add_headers(headers = {})
  @headers.merge!(sanitize_http_headers(headers || {}))
end

#bodyObject



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

def body
  if @body.kind_of?(String)
    @unserialized_body ||= unserialize(@body, :content_type => @headers['Content-Type'])
  else
    @body
  end
end

#raw_bodyObject



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

def raw_body
  if @body.kind_of?(String)
    @body
  else
    @serialized_body ||= serialize(@body, :content_type => @headers['Content-Type'])
  end
end