credit_card_info 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea183a3706d5644c767cc675c43494222e601f523975cdff250dc73efbfd5932
4
- data.tar.gz: bd3464aff05576d853128c4eeb45ca16447a989a6464d9f1e77d80ff8ac2cfd8
3
+ metadata.gz: 0c5b27792599dcd41289e4cf9c84192966a0e28ede54e65f51be7d76107cf63f
4
+ data.tar.gz: 9c9adf2bd3dd39b9d3babb30470763a7fd197cda88f757690d5db16c59e0c772
5
5
  SHA512:
6
- metadata.gz: 664b458e7dfbb66d6d9cf51d166bbadb900c800334f198984325f5bf44e92397d54e850ca080ccddf8c42a4c64cfd6378050917312d0431c806f9266ea559c0f
7
- data.tar.gz: 677f140934f8e2d24f8ee8b465aa917211c0e9c1362c0cc247bafea04e7525e5e3fff4f74b8786e1150484ccf5e89d59bd97ec40a3cf82f36ccb606d30aa958d
6
+ metadata.gz: a7bc0e117d93e55f71e680fdcdbd332db798674a78b3ac8653e394db92833cd75344ef3c5630e751674f5e7fafef2d4799ed417ddad3c53e25d74a41cc87e8ca
7
+ data.tar.gz: 6876226c9b86b6bd10108060d49742cba999b2cea38ba2771a0febd519f4a12f8010a88ba17118df9b132bbc48682d4b8a777722d6bbab0f58a5a3b7258d1303
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- credit_card_info (0.1.0)
4
+ credit_card_info (0.2.0)
5
5
  credit_card_bins (~> 0.0.1)
6
6
  dry-configurable (>= 0.13)
7
7
 
data/README.md CHANGED
@@ -4,10 +4,10 @@ The Issuer Identification Number (IIN), previously known as Bank Identification
4
4
  These identify the institution that issued the card to the card holder and
5
5
  provide useful information about the card that can help make your payments flow smarter.
6
6
 
7
- Uses two data providers:
8
- * First bincodes.com API (require registration and api key).
9
- * Second 'credit_card_bins' gem data.
10
-
7
+ Uses 3 data providers:
8
+ * First binlist.net API.
9
+ * Second bincodes.com API (require registration and api key).
10
+ * Third 'credit_card_bins' gem data.
11
11
 
12
12
  ## Installation
13
13
 
@@ -30,6 +30,9 @@ Or install it yourself as:
30
30
 
31
31
  ```ruby
32
32
  CreditCardInfo::Config.configure do |config|
33
+ # Provider request order
34
+ config.data_providers = %w[Binlist Bincodes CreditCardBins]
35
+
33
36
  config.bincodes.api_key = "your_api_key"
34
37
  config.bincodes.api_url = "https://api.bincodes.com"
35
38
 
@@ -38,7 +41,12 @@ CreditCardInfo::Config.configure do |config|
38
41
  # set timeout for api request (10 by default)
39
42
  config.bincodes.timeout = 10
40
43
 
41
-
44
+ config.binlist.api_url = "https://lookup.binlist.net/"
45
+ # class for make HttpRequests
46
+ config.bincodes.http_klass = Net::HTTP
47
+ # set binlist for api request (10 by default)
48
+ config.bincobinlistdes.timeout = 10
49
+
42
50
  # set cache provider class, Rails.cache by default when using in Rails application
43
51
  config.cache.provider = Rails.cache
44
52
  # set cache keys lifeitime, default is 31 day
@@ -69,7 +77,9 @@ CreditCardInfo.fetch(bin)
69
77
  # }
70
78
  ```
71
79
 
72
- By default that call will be search firstly via bincodes.com, and if find nothing will search via `credit_card_bins` gem.
80
+ By default that call will be search firstly via binlist.net, and if find nothing will search via bincodes.com,
81
+ and if find nothing will search via `credit_card_bins` gem. You can change requests order, by `data_providers` section.
82
+
73
83
 
74
84
  ## Contributing
75
85
 
@@ -7,6 +7,8 @@ module CreditCardInfo
7
7
  class Config
8
8
  extend Dry::Configurable
9
9
 
10
+ setting :data_providers, default: %w[Binlist Bincodes CreditCardBins]
11
+
10
12
  setting :bincodes do
11
13
  setting :api_key, default: ""
12
14
  setting :api_url, default: "https://api.bincodes.com"
@@ -14,6 +16,12 @@ module CreditCardInfo
14
16
  setting :http_klass, default: Net::HTTP
15
17
  end
16
18
 
19
+ setting :binlist do
20
+ setting :api_url, default: "https://lookup.binlist.net/"
21
+ setting :timeout, default: 10
22
+ setting :http_klass, default: Net::HTTP
23
+ end
24
+
17
25
  setting :cache do
18
26
  setting :provider, default: defined?(Rails) ? Rails.cache : nil
19
27
  setting :ttl, default: 2678400 # 31 days
@@ -33,8 +33,9 @@ module CreditCardInfo
33
33
  CreditCardInfo::Response.new(data: data).tap do |res|
34
34
  res.error = { code: data[:error], message: data[:message] } if data[:error]
35
35
  end
36
- rescue JSONError => e
37
- CreditCardInfo::Response.new(error: { code: e.class, message: "Invalid data from bincodes api: #{body}" })
36
+ rescue StandardError => e
37
+ CreditCardInfo::Response.new(error: { code: e.class,
38
+ message: "Invalid data from bincodes api: #{response.body}" })
38
39
  end
39
40
 
40
41
  def self.config
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+
7
+ # {
8
+ # "number": {
9
+ # "length": 16,
10
+ # "luhn": true
11
+ # },
12
+ # "scheme": "visa",
13
+ # "type": "debit",
14
+ # "brand": "Visa/Dankort",
15
+ # "prepaid": false,
16
+ # "country": {
17
+ # "numeric": "208",
18
+ # "alpha2": "DK",
19
+ # "name": "Denmark",
20
+ # "emoji": "🇩🇰",
21
+ # "currency": "DKK",
22
+ # "latitude": 56,
23
+ # "longitude": 10
24
+ # },
25
+ # "bank": {
26
+ # "name": "Jyske Bank",
27
+ # "url": "www.jyskebank.dk",
28
+ # "phone": "+4589893300",
29
+ # "city": "Hjørring"
30
+ # }
31
+ # }
32
+ module CreditCardInfo
33
+ module Providers
34
+ class Binlist
35
+ API_VERSION = "3"
36
+
37
+ def self.fetch(code)
38
+ uri = URI.join(config.api_url, "/#{code}")
39
+
40
+ http = config.http_klass.new(uri.host, uri.port)
41
+ http.use_ssl = true
42
+ http.read_timeout = config.timeout
43
+
44
+ request = Net::HTTP::Get.new(uri, { "Accept-Version" => API_VERSION })
45
+ response = http.start { |h| h.request(request) }
46
+
47
+ parse_response(code, response)
48
+ rescue StandardError => e
49
+ CreditCardInfo::Response.new(error: { code: e.class, message: e.message })
50
+ end
51
+
52
+ def self.parse_response(code, response)
53
+ unless response.is_a?(Net::HTTPSuccess)
54
+ return CreditCardInfo::Response.new(error: { code: response.code,
55
+ message: "Unexpected error from binlist api" })
56
+ end
57
+
58
+ data = JSON.parse(response.body, symbolize_names: true)
59
+
60
+ CreditCardInfo::Response.new(data: transform_keys(code, data))
61
+ rescue StandardError => e
62
+ CreditCardInfo::Response.new(error: { code: e.class,
63
+ message: "Invalid data from binlist api: #{response.body}" })
64
+ end
65
+
66
+ def self.transform_keys(code, data)
67
+ {
68
+ bin: code,
69
+ bank: data.dig(:bank, :name),
70
+ card: data[:scheme],
71
+ type: data[:type],
72
+ level: data[:brand],
73
+ country: data.dig(:country, :name),
74
+ countrycode: data.dig(:country, :alpha2)
75
+ }
76
+ end
77
+
78
+ def self.config
79
+ CreditCardInfo.config.binlist
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,23 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "providers/bincodes"
4
+ require_relative "providers/binlist"
4
5
  require_relative "providers/credit_card_bins"
5
6
 
6
7
  module CreditCardInfo
7
8
  module Proxy
8
9
  # @return [Hash, NilClass] bin description
9
10
  def self.fetch(code)
10
- bincode_response = Providers::Bincodes.fetch(code)
11
- return bincode_response.data if bincode_response.valid?
11
+ providers.each do |provider|
12
+ response = provider.fetch(code)
13
+ return response.data if response.valid?
12
14
 
13
- bincode_response.log_error
14
-
15
- card_bins_response = Providers::CreditCardBins.fetch(code)
16
- return card_bins_response.data if card_bins_response.valid?
17
-
18
- card_bins_response.log_error
15
+ response.log_error
16
+ end
19
17
 
20
18
  nil
21
19
  end
20
+
21
+ def self.providers
22
+ @providers ||=
23
+ CreditCardInfo.config.data_providers.filter_map do |name|
24
+ Object.const_get "CreditCardInfo::Providers::#{name}"
25
+ rescue NameError
26
+ nil
27
+ end
28
+ end
22
29
  end
23
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CreditCardInfo
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credit_card_info
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Zhuravlev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-14 00:00:00.000000000 Z
11
+ date: 2022-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: credit_card_bins
@@ -54,12 +54,12 @@ files:
54
54
  - README.md
55
55
  - Rakefile
56
56
  - bin/console
57
- - credit_card_info-0.1.0.gem
58
57
  - credit_card_info.gemspec
59
58
  - lib/credit_card_info.rb
60
59
  - lib/credit_card_info/cache.rb
61
60
  - lib/credit_card_info/config.rb
62
61
  - lib/credit_card_info/providers/bincodes.rb
62
+ - lib/credit_card_info/providers/binlist.rb
63
63
  - lib/credit_card_info/providers/credit_card_bins.rb
64
64
  - lib/credit_card_info/proxy.rb
65
65
  - lib/credit_card_info/response.rb
Binary file