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.



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

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
        RecursiveOpenStruct.new(elem, recurse_over_arrays: true)
      else
        elem
      end}
  elsif @body_object.class == Hash
    @body = RecursiveOpenStruct.new(@body_object, recurse_over_arrays: true)
  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.



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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



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

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



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

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

#[]=(key, val) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
    @body_object.empty?
end

#keysObject



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

def keys
  @body_object.keys
end

#map(&block) ⇒ Object



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

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

#to_sObject



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

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