Class: Perry::Persistence::Response

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

Constant Summary collapse

ATTRIBUTES =
[:success, :status, :meta, :raw, :raw_format, :parsed]
@@parsers =
{ :json => ::JSON }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Response

@attrs: Sets any values listed in ATTRIBUTES



26
27
28
29
30
# File 'lib/perry/persistence/response.rb', line 26

def initialize(attrs={})
  ATTRIBUTES.each do |attr|
    self.send("#{attr}=", attrs[attr])
  end
end

Instance Attribute Details

#metaObject

Any adapter specific response metadata



14
15
16
# File 'lib/perry/persistence/response.rb', line 14

def meta
  @meta
end

#parsedObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/perry/persistence/response.rb', line 32

def parsed
  if parser = self.class.parsers[self.raw_format]
    begin
      @parsed ||= parser.parse(self.raw)
    rescue Exception => err
      Perry.logger.error("Failure parsing raw response #{err.inspect}")
      Perry.logger.error("Response: #{self.inspect}")
      nil
    end
  else
    @parsed
  end
end

#rawObject

Raw response – format specified by raw_format



17
18
19
# File 'lib/perry/persistence/response.rb', line 17

def raw
  @raw
end

#raw_formatObject

Format of the raw response



20
21
22
# File 'lib/perry/persistence/response.rb', line 20

def raw_format
  @raw_format
end

#statusObject

A more detailed report of the success/failure of the response



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

def status
  @status
end

#successObject

A boolean value reflecting whether or not the response was successful



8
9
10
# File 'lib/perry/persistence/response.rb', line 8

def success
  @success
end

Class Method Details

.parsersObject



46
47
48
# File 'lib/perry/persistence/response.rb', line 46

def self.parsers
  @@parsers
end

Instance Method Details

#array_attributesObject



55
56
57
58
59
60
61
# File 'lib/perry/persistence/response.rb', line 55

def array_attributes
  if parsed.is_a?(Array)
    parsed.collect { |item| item.is_a?(Hash) ? extract_attributes(item) : item }
  else
    []
  end
end

#errorsObject



63
64
65
# File 'lib/perry/persistence/response.rb', line 63

def errors
  parsed_hash.symbolize_keys
end

#model_attributesObject



50
51
52
53
# File 'lib/perry/persistence/response.rb', line 50

def model_attributes
  # return the inner hash if nested
  extract_attributes(parsed_hash)
end