Module: Plaza::BaseAdapter

Included in:
RestfulAdapter
Defined in:
lib/plaza/adapters/base_adapter.rb

Instance Method Summary collapse

Instance Method Details

#handle_error(response) ⇒ Object

Raises:

  • (error)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/plaza/adapters/base_adapter.rb', line 16

def handle_error(response)
  response_hash = response_to_hash(response)
  error_hash = response_hash.has_key?(:error) ? response_hash[:error] : response_hash["error"]
  unless error_hash
    error_hash = response_hash.has_key?(:errors) ? response_hash[:errors] : response_hash["errors"]
  end
  #test for both singular error and plural errors
  logger.warn("Response returned an error code #{response.status} - #{response_hash}")
  case response.status.to_i
    when 422
      error = Plaza::ResourceInvalid.new(response, "#{error_hash}", error_hash)
  else
    error = Plaza::Error.new(response, "#{error_hash}")
  end
  raise(error,error.to_s )
end

#handle_response(response) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/plaza/adapters/base_adapter.rb', line 3

def handle_response(response)
  begin
    if response.success?
      return response_to_hash(response)
    else
      handle_error(response)
    end
  rescue JSON::ParserError=> jsonError
    error = Plaza::Error.new(jsonError, jsonError.message)
    raise(error,error.to_s )
  end
end

#response_to_hash(response) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/plaza/adapters/base_adapter.rb', line 33

def response_to_hash(response)
  if response.body.kind_of? Hash
    response.body
  else
    JSON.parse(response)
  end
end