class Fakturoid::Response

Attributes

body[R]
caller[R]
env[R]
request_method[R]
response[R]

Public Class Methods

new(faraday_response, caller, request_method) click to toggle source
# File lib/fakturoid/response.rb, line 5
def initialize(faraday_response, caller, request_method)
  @response = faraday_response
  @caller = caller
  @env = faraday_response.env
  @request_method = request_method.to_sym

  if !(env.body.nil? || env.body.empty? || (json? && env.body =~ /\A\s+\z/))
    @body = json? ? MultiJson.load(env.body) : env.body
  end
  handle_response
end

Public Instance Methods

headers() click to toggle source
# File lib/fakturoid/response.rb, line 25
def headers
  env.response_headers
end
inspect() click to toggle source
# File lib/fakturoid/response.rb, line 29
def inspect
  "#<#{self.class.name}:#{object_id} @body=\"#{body}\" @status_code=\"#{status_code}\">"
end
json?() click to toggle source
# File lib/fakturoid/response.rb, line 21
def json?
  env.request_headers['Content-Type'] == 'application/json'
end
status_code() click to toggle source
# File lib/fakturoid/response.rb, line 17
def status_code
  env['status']
end

Private Instance Methods

body_has_key?(key) click to toggle source
# File lib/fakturoid/response.rb, line 74
def body_has_key?(key)
  body && body.is_a?(Hash) && body.key?(key.to_s)
end
error(klass, message = nil) click to toggle source
# File lib/fakturoid/response.rb, line 58
def error(klass, message = nil)
  klass.new message, status_code, body
end
handle_response() click to toggle source
# File lib/fakturoid/response.rb, line 35
def handle_response
  case status_code
    when 400
      raise error(UserAgentError,  'User-Agent header missing') if env.request_headers['User-Agent'].nil? || env.request_headers['User-Agent'].empty?
      raise error(PaginationError, 'Page does not exist')
    when 401 then raise error(AuthenticationError, 'Authentification failed')
    when 402 then raise error(BlockedAccountError, 'Account is blocked')
    when 403 then
      raise error(DestroySubjectError, 'Cannot destroy subject with invoices')          if caller == Client::Subject && request_method == :delete
      raise error(SubjectLimitError,   'Subject limit for account reached')             if caller == Client::Subject && request_method == :post
      raise error(GeneratorLimitError, 'Recurring generator limit for account reached') if caller == Client::Generator
      raise error(UnsupportedFeatureError, 'Feature unavailable for account plan')
    when 404 then raise error(RecordNotFoundError, 'Record not found')
    when 415 then raise error(ContentTypeError,    'Unsupported Content-Type')
    when 422 then raise error(InvalidRecordError,  'Invalid record')
    when 429 then raise error(RateLimitError,      'Rate limit reached')
    when 503 then raise error(ReadOnlySiteError,   'Fakturoid is in read only state')
    else
      raise error(ServerError, 'Server error') if status_code >= 500
      raise error(ClientError, 'Client error') if status_code >= 400
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/fakturoid/response.rb, line 62
def method_missing(method, *args, &block)
  if body_has_key?(method)
    body[method.to_s]
  else
    super
  end
end
respond_to_missing?(method, _include_all) click to toggle source
Calls superclass method
# File lib/fakturoid/response.rb, line 70
def respond_to_missing?(method, _include_all)
  body_has_key?(method) || super
end