Module: Amfetamine::RestHelpers

Included in:
Base
Defined in:
lib/amfetamine/rest_helpers.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

RESPONSE_STATUSES =
{ 422 => :errors, 404 => :notfound, 200 => :success, 201 => :created, 500 => :server_error, 406 => :not_acceptable }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/amfetamine/rest_helpers.rb', line 13

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#handle_response(response) ⇒ Object

This method handles the save response TODO: Needs refactoring, now just want to make the test pass =) Making assumption here that when response is nil, it should have possitive result. Needs refactor when slept more



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/amfetamine/rest_helpers.rb', line 24

def handle_response(response)
  case response[:status]
  when :success, :created
    self.instance_variable_set('@notsaved', false)
    true
  when :errors
    Amfetamine.logger.warn "Errors from response\n #{response[:body]}"
    response[:body].each do |attr, error_messages|
      error_messages.each do |msg|
        errors.add(attr.to_sym, msg)
      end
    end
    false
  when :server_error
    Amfetamine.logger.warn "Something went wrong at the remote end."
    false
  end
end

#rest_path(args = {}) ⇒ Object



9
10
11
# File 'lib/amfetamine/rest_helpers.rb', line 9

def rest_path(args={})
  self.class.rest_path(args)
end

#singular_path(args = {}) ⇒ Object



17
18
19
# File 'lib/amfetamine/rest_helpers.rb', line 17

def singular_path(args={})
  self.class.find_path(self.id, args)
end