Class: CreditCardInfo::Providers::Binlist

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

Constant Summary collapse

API_VERSION =
"3"

Class Method Summary collapse

Class Method Details

.configObject



78
79
80
# File 'lib/credit_card_info/providers/binlist.rb', line 78

def self.config
  CreditCardInfo.config.binlist
end

.fetch(code) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/credit_card_info/providers/binlist.rb', line 37

def self.fetch(code)
  uri = URI.join(config.api_url, "/#{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, { "Accept-Version" => API_VERSION })
  response = http.start { |h| h.request(request) }

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

.parse_response(code, response) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/credit_card_info/providers/binlist.rb', line 52

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

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

  CreditCardInfo::Response.new(data: transform_keys(code, data))
rescue StandardError => e
  CreditCardInfo::Response.new(error: { code:    e.class,
                                        message: "Invalid data from binlist api: #{response.body}" })
end

.transform_keys(code, data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/credit_card_info/providers/binlist.rb', line 66

def self.transform_keys(code, data)
  {
    bin:         code,
    bank:        data.dig(:bank, :name),
    card:        data[:scheme],
    type:        data[:type],
    level:       data[:brand],
    country:     data.dig(:country, :name),
    countrycode: data.dig(:country, :alpha2)
  }
end