Class: LMC::LMCResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/lmc/Response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ LMCResponse

Returns a new instance of LMCResponse.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lmc/Response.rb', line 8

def initialize(response)
  @body_object = {}
  if response.bytesize > 0
    @body_object = JSON.parse response.body
  end
  if @body_object.class == Array
    @body = @body_object.map { |elem|
      if elem.is_a? Hash then
        OpenStruct.new(elem)
      else
        elem
      end}
  elsif @body_object.class == Hash
    @body = OpenStruct.new(@body_object)
  elsif @body_object.class == TrueClass || @body_object.class == FalseClass
    @body = @body_object
  else
    raise "Unknown json parse result: #{@body_object.class}"
  end
  @code = response.code
  @headers = response.headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/lmc/Response.rb', line 6

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/lmc/Response.rb', line 6

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/lmc/Response.rb', line 6

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ Object

body_object and these methods allow to use LMCResponse objects in place of the old response which was just the body parsed to a hash or array



33
34
35
# File 'lib/lmc/Response.rb', line 33

def [](key)
  @body_object[key]
end

#[]=(key, val) ⇒ Object



37
38
39
# File 'lib/lmc/Response.rb', line 37

def []=(key, val)
  @body_object[key] = val
end

#each(&block) ⇒ Object



49
50
51
# File 'lib/lmc/Response.rb', line 49

def each(&block)
  @body_object.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lmc/Response.rb', line 57

def empty?
    @body_object.empty?
end

#keysObject



41
42
43
# File 'lib/lmc/Response.rb', line 41

def keys
  @body_object.keys
end

#map(&block) ⇒ Object



45
46
47
# File 'lib/lmc/Response.rb', line 45

def map(&block)
  @body_object.map(&block)
end

#to_sObject



53
54
55
# File 'lib/lmc/Response.rb', line 53

def to_s
  "Response: Code: #{@code}, Body: #{@body_object.to_s}"
end