Class: Facile::Response::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/facile/response/wrapper.rb

Defined Under Namespace

Classes: Json, Mash, Raw

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, headers = {}) ⇒ Wrapper

Returns a new instance of Wrapper.



12
13
14
# File 'lib/facile/response/wrapper.rb', line 12

def initialize(body, headers = {})
  @body, @headers = body, headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



39
40
41
# File 'lib/facile/response/wrapper.rb', line 39

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



39
40
41
# File 'lib/facile/response/wrapper.rb', line 39

def headers
  @headers
end

Class Method Details

.parse(body, options = {}) ⇒ Object



6
7
8
9
10
# File 'lib/facile/response/wrapper.rb', line 6

def self.parse(body, options = {})
  wrapper = new(body, options)

  wrapper.parse
end

Instance Method Details

#content_type_to_class(content_type) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/facile/response/wrapper.rb', line 30

def content_type_to_class(content_type)
  case content_type
  when /application\/json/
    'Json'
  else
    'Raw'
  end
end

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/facile/response/wrapper.rb', line 16

def parse
  wrapper = "Raw"

  if headers.has_key?('content-type')
    wrapper = content_type_to_class(headers['content-type'])
  end

  klass = "Facile::Response::Wrapper::#{wrapper}".split('::').inject(Object) do |o, c|
    o.const_get(c)
  end

  klass.new(@body).parsed_body
end