Class: Agms::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response, op) ⇒ Response

A class representing AGMS Response objects.



5
6
7
8
9
# File 'lib/agms/response/response.rb', line 5

def initialize(response, op)
  @response = response
  @op = op
  @mapping = nil
end

Instance Method Details

#doMap(arr) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/agms/response/response.rb', line 24

def doMap(arr)
  response = Hash.new
  mapping = @mapping
  if mapping
    # We only map the end of the array containing data
    # If this element is an array, then we map its individual sub-arrays
    # Otherwise, we map
    arr.each do |key, value|

      if value.class == Hash
        response << doMap(value)
      else

        if not mapping.has_key?(key)
          raise UnexpectedError, "Unmapped field #{key} in response."
        else
          response[mapping[key]] = value
        end
      end
    end
  end
  return response
end

#mapResponse(arr) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/agms/response/response.rb', line 15

def mapResponse(arr)
  if @mapping
    response = doMap(arr)
    return response
  else
    raise UnexpectedError, 'Response mapping not defined for this API.'
  end
end

#toArrayObject



11
12
13
# File 'lib/agms/response/response.rb', line 11

def toArray
  return mapResponse(@response)
end