sea_shanty 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: 1e1c54c6748c4dffae4228506da66710eb949c6fc92dccefa5c655d0a1d22efb
4
- data.tar.gz: 666c4702a8665c1da5c667d8b64e2bd044fd60ea166e658feacb835ec67a53b8
3
+ metadata.gz: 513c49610891e98faecb14e58daf7a675f865facece11474daa7142bf359beaf
4
+ data.tar.gz: b2e9f9018c06a2c33aaa72bf8be87487118f1782d01406c28ce60ff6b087ddce
5
5
  SHA512:
6
- metadata.gz: a9bd23109117bf23895dd6b6119249b00440a2a5ecca97ff07667e183b52dfe0b180014dbaa6d1f1ad4204d5ea5abb13138f99ffec2ae0dca2f7d826ebcc9985
7
- data.tar.gz: ca8d9adec42adf5ec95a3940bf54b1da295e0d2366a501616420b5b97ceb82ef188cf038b6e4e42f249ca86d6aaa54aab07248dc2ed01fb08d1c8f407628ead1
6
+ metadata.gz: 83d95071d3a20a5c574dcc2a6d0df123fd90bedd7216099e9c71451b3c984ba2b660c539092ab3912213d75c2a9f27db273a50c39e303863245736f18b09fc7c
7
+ data.tar.gz: e091a14764d18ada33fb273139db862a7cf715ad081c2233be05991af6b991207f48e1f8d76de441714da0601f19f157c4e670bb2b50cc1272881ed4a4cafbc9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-03-16
4
+
5
+ - Add a bit of logging ([#3])
6
+ - Update CI to run against MRI 3.2.1 instead of 3.2.0
7
+
3
8
  ## [0.1.1] - 2023-01-05
4
9
 
5
10
  - Permit `Time` and `DateTime` when loading an interaction from yaml ([#2])
data/Gemfile.lock CHANGED
@@ -1,48 +1,48 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sea_shanty (0.1.1)
4
+ sea_shanty (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
- faraday (2.7.2)
10
+ faraday (2.7.4)
11
11
  faraday-net_http (>= 2.0, < 3.1)
12
12
  ruby2_keywords (>= 0.0.4)
13
13
  faraday-net_http (3.0.2)
14
14
  json (2.6.3)
15
- language_server-protocol (3.17.0.2)
16
- minitest (5.16.3)
15
+ language_server-protocol (3.17.0.3)
16
+ minitest (5.18.0)
17
17
  parallel (1.22.1)
18
- parser (3.1.3.0)
18
+ parser (3.2.1.0)
19
19
  ast (~> 2.4.1)
20
20
  rainbow (3.1.1)
21
21
  rake (13.0.6)
22
- regexp_parser (2.6.1)
22
+ regexp_parser (2.7.0)
23
23
  rexml (3.2.5)
24
- rubocop (1.40.0)
24
+ rubocop (1.44.1)
25
25
  json (~> 2.3)
26
26
  parallel (~> 1.10)
27
- parser (>= 3.1.2.1)
27
+ parser (>= 3.2.0.0)
28
28
  rainbow (>= 2.2.2, < 4.0)
29
29
  regexp_parser (>= 1.8, < 3.0)
30
30
  rexml (>= 3.2.5, < 4.0)
31
- rubocop-ast (>= 1.23.0, < 2.0)
31
+ rubocop-ast (>= 1.24.1, < 2.0)
32
32
  ruby-progressbar (~> 1.7)
33
- unicode-display_width (>= 1.4.0, < 3.0)
34
- rubocop-ast (1.24.0)
35
- parser (>= 3.1.1.0)
36
- rubocop-performance (1.15.1)
33
+ unicode-display_width (>= 2.4.0, < 3.0)
34
+ rubocop-ast (1.26.0)
35
+ parser (>= 3.2.1.0)
36
+ rubocop-performance (1.15.2)
37
37
  rubocop (>= 1.7.0, < 2.0)
38
38
  rubocop-ast (>= 0.4.0)
39
39
  ruby-progressbar (1.11.0)
40
40
  ruby2_keywords (0.0.5)
41
- standard (1.20.0)
41
+ standard (1.24.3)
42
42
  language_server-protocol (~> 3.17.0.2)
43
- rubocop (= 1.40.0)
44
- rubocop-performance (= 1.15.1)
45
- unicode-display_width (2.3.0)
43
+ rubocop (= 1.44.1)
44
+ rubocop-performance (= 1.15.2)
45
+ unicode-display_width (2.4.2)
46
46
 
47
47
  PLATFORMS
48
48
  x86_64-darwin-21
data/README.md CHANGED
@@ -87,6 +87,9 @@ Any HTTP requests made before the call to `SeaShanty.intercept` will not be hand
87
87
  * A callable object with an arity of 2
88
88
  * Is called once with each header name and value
89
89
  * Must return the filtered value
90
+ * `log`
91
+ * Is the destination, where SeaShanty should log to
92
+ * Must be either an object that responds to `write` or a path to a file to write log entries to
90
93
 
91
94
  ### Creating your own interceptor
92
95
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "sea_shanty/errors"
4
+ require "sea_shanty/util/logger"
4
5
 
5
6
  module SeaShanty
6
7
  class Configuration
@@ -8,6 +9,14 @@ module SeaShanty
8
9
  alias_method :bypass?, :bypass
9
10
  alias_method :readonly?, :readonly
10
11
 
12
+ def log=(destination)
13
+ @logger = Logger.new(destination)
14
+ end
15
+
16
+ def logger
17
+ @logger ||= Logger::NullLogger.new
18
+ end
19
+
11
20
  def generic_responses=(responses)
12
21
  unless Hash === responses
13
22
  raise(
@@ -18,50 +18,49 @@ module SeaShanty
18
18
  if configuration.bypass?
19
19
  raise ConfigurationError, "Bypass and readonly are both true - please set only one of them." if configuration.readonly?
20
20
  yield
21
- elsif configuration.readonly?
22
- load_response(request)
23
- elsif has_response_for?(request)
24
- load_response(request)
25
21
  else
26
- response = yield
27
- store(request, response) unless configuration.bypass?
28
- response
22
+ path = request_file_path(request)
23
+ if configuration.readonly? || path.exist?
24
+ load_response(path, request)
25
+ else
26
+ response = yield
27
+ store(path, request, response) unless configuration.bypass?
28
+ response
29
+ end
29
30
  end
30
31
  end
31
32
 
32
- def has_response_for?(request)
33
- request_file_path(request).exist?
34
- end
35
-
36
- def load_response(request)
37
- raise UnknownRequest, "SeaShanty: Unknown request #{request.method.to_s.upcase} to #{request.url}" unless has_response_for?(request)
38
- contents = YAML.safe_load(request_file_path(request).read, permitted_classes: [Symbol, Time, DateTime])
33
+ def load_response(path, request)
34
+ raise UnknownRequest, "SeaShanty: Unknown request #{request.method.to_s.upcase} to #{request.url}" unless path.exist?
35
+ log("Loading response for #{request.url} from #{path}")
36
+ contents = YAML.safe_load(Pathname(path).read, permitted_classes: [Symbol, Time, DateTime])
39
37
  Response.from_h(contents.fetch(:response))
40
38
  end
41
39
 
42
- def store(request, response)
43
- file_path = request_file_path(request)
44
- file_path.dirname.mkpath
45
- file_path.open("w+") do |file|
40
+ def store(path, request, response)
41
+ log("Storing response for #{request.url} in #{path}")
42
+ path.dirname.mkpath
43
+ path.open("w+") do |file|
46
44
  file.write(YAML.dump(serialize(request, response)))
47
45
  end
48
46
  end
49
47
 
50
- private
51
-
52
- attr_reader :configuration, :generic_responses, :storage_dir, :request_serializer
53
-
54
48
  def request_file_path(request)
55
49
  _, generic_file_path = generic_responses.find { |matcher, path| matcher.match?(request.url.to_s) }
56
50
  file_path = if generic_file_path.nil?
57
- request_serializer.file_path(request)
51
+ request_serializer.file_path(request).tap { |path| log("Generated #{path} for request to #{request.url}") }
58
52
  else
53
+ log("Found a generic response in #{generic_file_path} for request to #{request.url}")
59
54
  Pathname.new(generic_file_path.to_s)
60
55
  end
61
56
 
62
57
  storage_dir.join(file_path)
63
58
  end
64
59
 
60
+ private
61
+
62
+ attr_reader :configuration, :generic_responses, :storage_dir, :request_serializer
63
+
65
64
  def serialize(request, response)
66
65
  {
67
66
  request: request_serializer.serialize(request),
@@ -69,5 +68,9 @@ module SeaShanty
69
68
  stored_at: DateTime.now.to_s
70
69
  }
71
70
  end
71
+
72
+ def log(message)
73
+ configuration.logger.log(message)
74
+ end
72
75
  end
73
76
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SeaShanty
4
+ class Logger
5
+ attr_reader :destination
6
+
7
+ def initialize(destination)
8
+ @destination = if destination.respond_to? :printf
9
+ destination
10
+ else
11
+ File.open(destination.to_s, "a+")
12
+ end
13
+ end
14
+
15
+ def log(message)
16
+ destination.write(message)
17
+ destination.write("\n") unless message.end_with?("\n")
18
+ end
19
+
20
+ class NullLogger
21
+ def log(*)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SeaShanty
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/sea_shanty.rb CHANGED
@@ -56,7 +56,12 @@ module SeaShanty
56
56
  end
57
57
 
58
58
  def request_store
59
- @request_store ||= RequestStore.new(configuration)
59
+ RequestStore.new(configuration)
60
+ end
61
+
62
+ def reset!
63
+ @configuration = Configuration.new
64
+ configure {}
60
65
  end
61
66
 
62
67
  def configuration_overwrite(env_var, value)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sea_shanty
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
  - Rasmus Bang Grouleff
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-05 00:00:00.000000000 Z
11
+ date: 2023-03-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: SeaShanty is a minimalistic library for recording HTTP requests and responses
14
14
  and replaying responses for already seen requests in an unobtrusive and test framework
@@ -40,6 +40,7 @@ files:
40
40
  - lib/sea_shanty/request_serializer.rb
41
41
  - lib/sea_shanty/request_store.rb
42
42
  - lib/sea_shanty/response.rb
43
+ - lib/sea_shanty/util/logger.rb
43
44
  - lib/sea_shanty/version.rb
44
45
  - sea_shanty.gemspec
45
46
  - sig/sea_shanty.rbs