Exception: Pipl::Client::APIError

Inherits:
Exception
  • Object
show all
Defined in:
lib/pipl/errors.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status_code, params = {}) ⇒ APIError

Returns a new instance of APIError.



12
13
14
15
16
17
18
19
20
# File 'lib/pipl/errors.rb', line 12

def initialize(message, status_code, params={})
  super message
  @status_code = status_code
  @qps_allotted = params[:qps_allotted]
  @qps_current = params[:qps_current]
  @quota_allotted = params[:quota_allotted]
  @quota_current = params[:quota_current]
  @quota_reset = params[:quota_reset]
end

Instance Attribute Details

#qps_allottedObject (readonly)

Returns the value of attribute qps_allotted.



10
11
12
# File 'lib/pipl/errors.rb', line 10

def qps_allotted
  @qps_allotted
end

#qps_currentObject (readonly)

Returns the value of attribute qps_current.



10
11
12
# File 'lib/pipl/errors.rb', line 10

def qps_current
  @qps_current
end

#quota_allottedObject (readonly)

Returns the value of attribute quota_allotted.



10
11
12
# File 'lib/pipl/errors.rb', line 10

def quota_allotted
  @quota_allotted
end

#quota_currentObject (readonly)

Returns the value of attribute quota_current.



10
11
12
# File 'lib/pipl/errors.rb', line 10

def quota_current
  @quota_current
end

#quota_resetObject (readonly)

Returns the value of attribute quota_reset.



10
11
12
# File 'lib/pipl/errors.rb', line 10

def quota_reset
  @quota_reset
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



9
10
11
# File 'lib/pipl/errors.rb', line 9

def status_code
  @status_code
end

Class Method Details

.deserialize(json_str, headers = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pipl/errors.rb', line 30

def self.deserialize(json_str, headers={})
  h = JSON.parse(json_str, symbolize_names: true)

  # Quota and Throttle headers
  params = {}
  params[:qps_allotted] = headers['X-APIKey-QPS-Allotted'].to_i if headers.key? 'X-APIKey-QPS-Allotted'
  params[:qps_current] = headers['X-APIKey-QPS-Current'].to_i if headers.key? 'X-APIKey-QPS-Current'
  params[:quota_allotted] = headers['X-APIKey-Quota-Allotted'].to_i if headers.key? 'X-APIKey-Quota-Allotted'
  params[:quota_current] = headers['X-APIKey-Quota-Current'].to_i if headers.key? 'X-APIKey-Quota-Current'
  params[:quota_reset] = DateTime.strptime(headers['X-Quota-Reset'], '%A, %B %d, %Y %I:%M:%S %p %Z') if headers.key? 'X-Quota-Reset'

  self.new(h[:error], h[:@http_status_code], params)
end

.from_http_response(resp) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/pipl/errors.rb', line 44

def self.from_http_response(resp)
  begin
    self.deserialize(resp.body, resp)
  rescue
    Pipl::Client::APIError.new resp.message, resp.code
  end
end

.from_json(json_str) ⇒ Object

Here for backward compatibility



53
54
55
# File 'lib/pipl/errors.rb', line 53

def self.from_json(json_str)
  self.deserialize(json_str)
end

Instance Method Details

#is_pipl_error?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/pipl/errors.rb', line 26

def is_pipl_error?
  not is_user_error?
end

#is_user_error?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/pipl/errors.rb', line 22

def is_user_error?
  (400..499).member?(@status_code)
end