bckbn 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc0cdd6e820ae998dccec0a53eefd66d6f21d948d3424d6b82ac0c21dcbf1501
4
- data.tar.gz: a44e6ae7d880b59528dab60f4143aff235f64f37cd7e7e4ea4be19d687c0cbc7
3
+ metadata.gz: ce377473db9fe443d1c56a5fddee4a3a0c054f7116ddec01c72a557a42444fc5
4
+ data.tar.gz: 658740e19e499bb366b79c35be52dc679248c03bd07b752062b29be8b8ca4ca4
5
5
  SHA512:
6
- metadata.gz: 95858139e1c9805a895784df36083c30756ab7fdccf4c5e5611619539c76b9d82d5fe07e6d6343f4fa1b2d40ba22da624c33888823308e21deb5f42e2efcc1f7
7
- data.tar.gz: 1a6783055c484191f7b8fdd1bd592da750be62504a8c19649aa51e304868f9efd81edc5c67c26312d6ed75a49896889026d0752b31cbe834b117908b2c69f33a
6
+ metadata.gz: 737a5f3f12a45a1324bf5da6808db9675dae182ec2c18161679711c94b32597aa3d6a978b38cc3244d89a6dd0fffd3187d069e916dff56dc419b930b8b18a121
7
+ data.tar.gz: 1ffaf775d1abb53073328e3b11118bfba10bc5fa3065cbdb40cf00efb24cecec6cf90c806852c34307857a02664a8bd1c5129e109e92ba4726c7bc0d18d8b9cb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
- ## [Unreleased]
1
+ ## 0.2.0
2
2
 
3
- ## [0.1.0] - 2023-08-18
3
+ - Add support for `debug` and `error` log levels
4
4
 
5
- - Initial release
5
+ ## 1.0.0
6
+
7
+ - Update local config to override global config where both present
8
+ - Add idempotency key header
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bckbn (0.1.2)
4
+ bckbn (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,6 +9,7 @@ module Bckbn
9
9
  log_level
10
10
  merchant_id
11
11
  source_ip_address
12
+ idempotency_key
12
13
  ].freeze
13
14
 
14
15
  Configuration = Struct.new(*CONFIG_OPTIONS, keyword_init: true) do
@@ -18,6 +19,7 @@ module Bckbn
18
19
 
19
20
  def initialize(**opts)
20
21
  opts[:api_version] ||= API_VERSION
22
+ opts[:log_level] ||= :error
21
23
  super(**opts)
22
24
  end
23
25
  end
@@ -4,9 +4,18 @@ module Bckbn
4
4
  class Connection
5
5
  using Bckbn::CoreExt::StringExt
6
6
 
7
- HttpBadRequest = Class.new(StandardError)
8
- HttpInternalServerError = Class.new(StandardError)
9
- HttpServiceUnavailable = Class.new(StandardError)
7
+ class BaseHttpError < StandardError
8
+ attr_reader :logs
9
+
10
+ def initialize(message, logs = [])
11
+ super(message)
12
+ @logs = logs
13
+ end
14
+ end
15
+
16
+ HttpBadRequest = Class.new(BaseHttpError)
17
+ HttpInternalServerError = Class.new(BaseHttpError)
18
+ HttpServiceUnavailable = Class.new(BaseHttpError)
10
19
 
11
20
  ERRORS = {
12
21
  Net::HTTPBadRequest => HttpBadRequest,
@@ -14,32 +23,37 @@ module Bckbn
14
23
  Net::HTTPServiceUnavailable => HttpServiceUnavailable
15
24
  }.freeze
16
25
 
17
- def initialize(config)
18
- @config = config.empty? ? Bckbn.config : Bckbn::Configuration.new(**config)
26
+ def initialize(per_req_config)
27
+ global_config = Bckbn.config.to_h
28
+ config = global_config.merge(per_req_config)
29
+
30
+ @config = Bckbn::Configuration.new(**config)
31
+ @logs = []
19
32
  end
20
33
 
21
34
  def post_to_api(path, body, klass)
22
- headers = {
23
- "Content-Type" => "application/json",
24
- "Authorization" => "Bearer #{config.access_token}",
25
- "X-Api-Version" => config.api_version,
26
- "X-Merchant-Id" => config.merchant_id,
27
- "X-Source-Ip-Address" => config.source_ip_address
28
- }
35
+ log(:debug, "POST #{path}\n\nData: #{body.to_json}")
29
36
 
30
37
  url = URI.parse(config.api_base + path)
31
38
  request = Net::HTTP::Post.new(url.path)
32
- headers.reject! { |_, v| v.nil? || v == "" }
33
- headers.each { |k, v| request[k] = v }
39
+ bckbn_headers = headers(config)
40
+ bckbn_headers.reject! { |_, v| v.nil? || v == "" }
41
+ bckbn_headers.each { |k, v| request[k] = v }
34
42
  request.body = body.to_json
35
43
 
36
44
  response_handler(url, request) do |response, rbody|
37
45
  case response
38
46
  when Net::HTTPSuccess
39
47
  data = rbody.dig("data", klass.name.split("::").last.underscore)
40
- klass.new(data)
48
+ log(:debug, "\nResponse: #{data.to_json}")
49
+ klass.new(**data, logs: @logs)
41
50
  else
42
- raise ERRORS[response.class], rbody ? rbody["errors"] : nil
51
+ err_klass = ERRORS[response.class]
52
+ message = "Error: #{rbody ? rbody["errors"] : "Unknown"}"
53
+ log(:error, message)
54
+
55
+ err = err_klass.new(message, @logs)
56
+ raise err
43
57
  end
44
58
  end
45
59
  end
@@ -67,5 +81,29 @@ module Bckbn
67
81
  end
68
82
  end
69
83
  end
84
+
85
+ def headers(config)
86
+ {
87
+ "Content-Type" => "application/json",
88
+ "Authorization" => "Bearer #{config.access_token}",
89
+ "X-Api-Version" => config.api_version,
90
+ "X-Merchant-Id" => config.merchant_id,
91
+ "X-Source-Ip-Address" => config.source_ip_address,
92
+ "X-Idempotency-Key" => config.idempotency_key
93
+ }
94
+ end
95
+
96
+ def log(level, message)
97
+ return if LOG_LEVEL_RANKING[config.log_level] > LOG_LEVEL_RANKING[level]
98
+
99
+ entry = "[#{level.to_s.upcase}] #{message}"
100
+ @logs << entry
101
+ entry
102
+ end
103
+
104
+ LOG_LEVEL_RANKING = {
105
+ debug: 1,
106
+ error: 2
107
+ }.freeze
70
108
  end
71
109
  end
@@ -14,6 +14,7 @@ module Bckbn
14
14
  response
15
15
  response_time
16
16
  account_updater
17
+ logs
17
18
  ].freeze
18
19
 
19
20
  AUTHORIZATION_RESPONSE_MEMBERS = [
data/lib/bckbn/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bckbn
4
- VERSION = "0.1.2"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bckbn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nikkypx
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-04 00:00:00.000000000 Z
11
+ date: 2023-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker