class NumverifyClient::Request
Attributes
connection[R]
path[R]
query[R]
Public Class Methods
new(query: {}, https: false)
click to toggle source
# File lib/numverify/request.rb, line 19 def initialize(query: {}, https: false) @base_uri = "#{protocol(https)}://apilayer.net" @query = query @path = '/api/validate' @connection = Excon.new(@base_uri) end
Public Instance Methods
perform(**args)
click to toggle source
# File lib/numverify/request.rb, line 28 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 Instance Methods
handle_error(error)
click to toggle source
# File lib/numverify/request.rb, line 44 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
protocol(use_https)
click to toggle source
# File lib/numverify/request.rb, line 40 def protocol(use_https) use_https ? 'https' : 'http' end