Class: CreditCardInfo::Providers::Bincodes

Inherits:
Object
  • Object
show all
Defined in:
lib/credit_card_info/providers/bincodes.rb

Class Method Summary collapse

Class Method Details

.configObject



41
42
43
# File 'lib/credit_card_info/providers/bincodes.rb', line 41

def self.config
  CreditCardInfo.config.bincodes
end

.fetch(code) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/credit_card_info/providers/bincodes.rb', line 10

def self.fetch(code)
  uri = URI("#{config.api_url}/bin/json/#{config.api_key}/#{code}/")

  http = config.http_klass.new(uri.host, uri.port)
  http.use_ssl = true
  http.read_timeout = config.timeout

  request = Net::HTTP::Get.new(uri)
  response = http.start { |h| h.request(request) }

  parse_response(response)
rescue StandardError => e
  CreditCardInfo::Response.new(error: { code: e.class, message: e.message })
end

.parse_response(response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/credit_card_info/providers/bincodes.rb', line 25

def self.parse_response(response)
  unless response.is_a?(Net::HTTPSuccess)
    return CreditCardInfo::Response.new(error: { code:    response.code,
                                                 message: "Unexpected error from bincodes api" })
  end

  data = JSON.parse(response.body, symbolize_names: true)

  CreditCardInfo::Response.new(data: data).tap do |res|
    res.error = { code: data[:error], message: data[:message] } if data[:error]
  end
rescue StandardError => e
  CreditCardInfo::Response.new(error: { code:    e.class,
                                        message: "Invalid data from bincodes api: #{response.body}" })
end