Sha256: a4d5a4cc2e95e01bae63c798b729d99ce08b472cb30c5a4a9663829f6e2e0716

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

require 'numverify/errors/numverify_client_error'
require 'numverify/errors/api_error'
require 'numverify/errors/https_access_restriction_error'
require 'numverify/errors/inactive_user_error'
require 'numverify/errors/invalid_access_key_error'
require 'numverify/errors/invalid_api_function_error'
require 'numverify/errors/invalid_country_code_error'
require 'numverify/errors/no_phone_number_error'
require 'numverify/errors/non_numeric_phone_number_error'
require 'numverify/errors/not_found_error'
require 'numverify/errors/usage_limit_error'
require 'numverify/result'
require 'uri'

module NumverifyClient
  class Request
    def initialize(query = {})
      @base_uri   = 'http://apilayer.net'
      @query      = query
      @path       = '/api/validate'
      @connection = Excon.new(@base_uri)
    end

    attr_reader :connection, :path, :query

    def perform(**args)
      response = connection.request(args.merge(path: path, query: query))
      parsed_response = JSON.parse(response.body)
      if parsed_response.key?('error')
        handle_error(parsed_response['error'])
      else
        NumverifyClient::Result.new(parsed_response)
      end
    end

    private

    def handle_error(error)
      case error['code']
      when 404
        raise NotFoundError, error['info']
      when 101
        raise InvalidAccessKeyError, error['info']
      when 103
        raise InvalidApiFunctionError, error['info']
      when 210
        raise NoPhoneNumberError, error['info']
      when 211
        raise NonNumericPhoneNumberError, error['info']
      when 310
        raise InvalidCountryCodeError, error['info']
      when 104
        raise UsageLimitError, error['info']
      when 105
        raise HttpsAccessRestrictionError, error['info']
      when 102
        raise InactiveUserError, error['info']
      else
        raise APIError, error['info']
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
numverify-0.1.1 lib/numverify/request.rb
numverify-0.1.0 lib/numverify/request.rb