bckbn 0.1.1 → 0.2.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: 9afc29e67f20356439a1e7b4659391ea0f2c0b93352bc15b0f7fe2eff71b0c80
4
- data.tar.gz: 2ea53ee4b4de867149947e0b86cc88e2dd7a2686944e56901a7e18b8d655be33
3
+ metadata.gz: 18497553fb0efdfc5eabb1f3893f41cfd6d5bcb32e5d6f8b5fe0f29552ba1807
4
+ data.tar.gz: 6850ef4da956af91bb09b0208bf4023689e9ac4a88c493c460f2468d17446565
5
5
  SHA512:
6
- metadata.gz: 1e18d3f42a84af193b78e4177e04b20674433b582801cfaf2b99bd7b4a435990abe7756008686a63945dd84ad8e96b74ecda89f35fdd258378ce95af4df28ac8
7
- data.tar.gz: 2831e64739fc6e3fb02de6410fb72f321a748b94d5d184b1202b4ffef3b56d05fb99b236852cb49c4b67bf4c6506a500b9164d34b04d856b903221d1afdae9f6
6
+ metadata.gz: 657681ad26592ebdc309d65410843b6e0301140157a08a60da6a853850d917bf1f38222fe4ddd0b33841d3e3f9372b46e902e6cca9a3c02e8700cfdcb33c41a2
7
+ data.tar.gz: c989abe10cccc89833d898c5fa9e01e687071b9d43169034e0918b72cf172c78723a8788c642b51c3d0fe49e257b6b40ad9ca07e7e4d031c9be0c2a214ccba1e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bckbn (0.1.1)
4
+ bckbn (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -18,6 +18,7 @@ module Bckbn
18
18
 
19
19
  def initialize(**opts)
20
20
  opts[:api_version] ||= API_VERSION
21
+ opts[:log_level] ||= :error
21
22
  super(**opts)
22
23
  end
23
24
  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,
@@ -16,9 +25,12 @@ module Bckbn
16
25
 
17
26
  def initialize(config)
18
27
  @config = config.empty? ? Bckbn.config : Bckbn::Configuration.new(**config)
28
+ @logs = []
19
29
  end
20
30
 
21
31
  def post_to_api(path, body, klass)
32
+ log(:debug, "POST #{path}\n\nData: #{body.to_json}")
33
+
22
34
  headers = {
23
35
  "Content-Type" => "application/json",
24
36
  "Authorization" => "Bearer #{config.access_token}",
@@ -37,9 +49,15 @@ module Bckbn
37
49
  case response
38
50
  when Net::HTTPSuccess
39
51
  data = rbody.dig("data", klass.name.split("::").last.underscore)
40
- klass.new(data)
52
+ log(:debug, "\nResponse: #{data.to_json}")
53
+ klass.new(**data, logs: @logs)
41
54
  else
42
- raise ERRORS[response.class], rbody ? rbody["errors"] : nil
55
+ err_klass = ERRORS[response.class]
56
+ message = "Error: #{rbody ? rbody["errors"] : "Unknown"}"
57
+ log(:error, message)
58
+
59
+ err = err_klass.new(message, @logs)
60
+ raise err
43
61
  end
44
62
  end
45
63
  end
@@ -50,7 +68,7 @@ module Bckbn
50
68
 
51
69
  def response_handler(url, request)
52
70
  http = Net::HTTP.new(url.host, url.port)
53
- # http.use_ssl = true
71
+ http.use_ssl = true
54
72
 
55
73
  response, body = begin
56
74
  r = http.request(request)
@@ -67,5 +85,18 @@ module Bckbn
67
85
  end
68
86
  end
69
87
  end
88
+
89
+ def log(level, message)
90
+ return if LOG_LEVEL_RANKING[config.log_level] > LOG_LEVEL_RANKING[level]
91
+
92
+ entry = "[#{level.to_s.upcase}] #{message}"
93
+ @logs << entry
94
+ entry
95
+ end
96
+
97
+ LOG_LEVEL_RANKING = {
98
+ debug: 1,
99
+ error: 2
100
+ }.freeze
70
101
  end
71
102
  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.1"
4
+ VERSION = "0.2.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.1
4
+ version: 0.2.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-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker