Class: PerfectAudit::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/perfect_audit/response_parser.rb

Class Method Summary collapse

Class Method Details

.parse(response) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/perfect_audit/response_parser.rb', line 7

def self.parse(response)
  struct = OpenStruct.new(JSON.parse(response))

  raise PerfectAudit::Error.new(struct.message, struct.code) if struct.status != 200

  case struct.response
  when Hash
    struct.response.each_with_object({}) { |(k, v), memo|
      memo[k.to_sym] = v
    }
  when Array
    struct.response.map do |item|
      item.each_with_object({}) { |(k, v), memo|
        memo[k.to_sym] = v
      }
    end
  else
    struct.response
  end
end