Exception: Hashblue::API::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/hashblue/api/error.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error

Returns a new instance of Error.



20
21
22
23
24
25
26
27
# File 'lib/hashblue/api/error.rb', line 20

def initialize(response)
  if response.respond_to?(:body)
    @body = response.body
    @headers = response.headers
  else
    @response = response
  end
end

Class Method Details

.from_response(response) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/hashblue/api/error.rb', line 10

def self.from_response(response)
  if response.json? && response.keys == ["error"]
    response["error"].keys.first.constantize.new(response["error"].values.first)
  elsif response.code == 401
    Hashblue::API::AccessDeniedError.new(response)
  else
    Hashblue::API::BadResponseError.new(response)
  end
end

.status(return_status) ⇒ Object



6
7
8
# File 'lib/hashblue/api/error.rb', line 6

def self.status(return_status)
  define_method(:status) { return_status }
end

Instance Method Details

#statusObject



29
30
31
# File 'lib/hashblue/api/error.rb', line 29

def status
  :not_found
end

#to_jsonObject



42
43
44
# File 'lib/hashblue/api/error.rb', line 42

def to_json
  {self.class.name => self.message}
end

#to_sObject



33
34
35
36
37
38
39
40
# File 'lib/hashblue/api/error.rb', line 33

def to_s
  str = "<##{self.class.name}:#{self.object_id}"
  if @body && @headers
    str + "@headers=#{@headers.inspect} @body=#{@body.inspect}>"
  else
    str + "@response=#{@response.inspect}>"
  end
end