Class: RestPack::Service::Response

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

Defined Under Namespace

Classes: Status

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



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

def initialize
  @result = {}
  @errors = {}
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/restpack_service/response.rb', line 3

def errors
  @errors
end

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/restpack_service/response.rb', line 3

def result
  @result
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/restpack_service/response.rb', line 3

def status
  @status
end

Class Method Details

.from_rest(rest_response) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/restpack_service/response.rb', line 23

def self.from_rest(rest_response)
  response = new
  response.status = Status.from_code(rest_response.code)
  response.result = Yajl::Parser.parse(rest_response.body, :symbolize_keys => true)

  if response.result[:errors]
    response.result[:errors].each do |field, errors|
      response.errors[field.to_sym] = errors
    end

    response.result.delete :errors
  end

  response
end

Instance Method Details

#add_error(field, message) ⇒ Object



18
19
20
21
# File 'lib/restpack_service/response.rb', line 18

def add_error(field, message)
  @errors[field] ||= []
  @errors[field] << message
end

#codeObject



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

def code
  Status.from_status(status)
end

#success?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/restpack_service/response.rb', line 10

def success?
  @errors.empty? and @status == :ok
end