Class: Linguin::BaseResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/linguin/base_response.rb

Overview

Linguin::BaseResponse

Base class for Linguin::Detection && Linguin::Status

Constant Summary collapse

CODE_MAP =
{
  400 => InputError,
  401 => AuthenticationError,
  404 => NotFoundError,
  422 => InputError,
  429 => RateLimitError,
  500 => InternalError,
  503 => InternalError
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ BaseResponse

Returns a new instance of BaseResponse.

Yields:

  • (_self)

Yield Parameters:



19
20
21
# File 'lib/linguin/base_response.rb', line 19

def initialize
  yield self
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



17
18
19
# File 'lib/linguin/base_response.rb', line 17

def error
  @error
end

Class Method Details

.from_httparty(response:) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/linguin/base_response.rb', line 24

def from_httparty(response:)
  if response.code == 200
    response = JSON.parse response, symbolize_names: true
    success(response)
  else
    error(response.code, response.body)
  end
end

Instance Method Details

#raise_on_error!Object

Raises:

  • (error_klass)


34
35
36
37
38
39
40
# File 'lib/linguin/base_response.rb', line 34

def raise_on_error!
  return self unless error

  error_klass = CODE_MAP[error[:code]] || Error
  error_message = error[:message].empty? ? "unknown" : error[:message]
  raise error_klass, "#{error[:code]} / #{error_message}"
end