class HerdstServicecall::Response

Attributes

body[RW]
code[RW]
event[RW]

Public Class Methods

new(event, code, body) click to toggle source
# File lib/herdst_servicecall/response.rb, line 8
def initialize(event, code, body)
    self.event = event
    self.code = code
    self.body = body ? 
        (body.respond_to?(:with_indifferent_access) ? body.with_indifferent_access : body) : 
        nil
end
parse(response) click to toggle source
# File lib/herdst_servicecall/response.rb, line 45
def self.parse(response)
    body = JSON.parse(response.body) rescue nil
    event = body && body["event"]
    data = body && body["data"]
    
    self.new(event, response.code.to_i, data)
end

Public Instance Methods

[](key) click to toggle source
# File lib/herdst_servicecall/response.rb, line 40
def [](key)
    self.body ? self.body[key] : nil
end
error(default = "Unknown Error") click to toggle source
# File lib/herdst_servicecall/response.rb, line 22
def error(default = "Unknown Error")
    if (self.was_successful? == false)
        return (self.body == nil) ? { :message => default } : self.body
    end
    
    return nil
end
to_json() click to toggle source
# File lib/herdst_servicecall/response.rb, line 31
def to_json
    {
        :event => self.event,
        :success => self.was_successful?,
        :data => self.body
    }.to_json
end
was_successful?() click to toggle source
# File lib/herdst_servicecall/response.rb, line 17
def was_successful?
    code && code >= 200 && code < 400
end