http_store 0.3.2 → 0.3.3

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: 2c630613b04c77a91abcfd9de63daa0f77f8c0942775a45a07c108f91b2a2e33
4
- data.tar.gz: dadd6adda88db7852f73a467276d4ec61ce00e0889d7d865db21cc5d8f365ec2
3
+ metadata.gz: 7260e19010b3f1f15beb41344f4f724e93259b4f265331676392987f6f5f6117
4
+ data.tar.gz: 508395e3ccc1a935eec7d8b7b4cb7d17b37cae941ad74fd020cde4856fe411d9
5
5
  SHA512:
6
- metadata.gz: 72c9b03b19e5521d94ccde93bf3572fa59de2207783ec52ab9300a0a7761d4febbf3bde07f018f8835adab3a37208ff3938908f9fe8326c4f93b2c280dd967fd
7
- data.tar.gz: 4ef08840bf1668823f0305d5f5dda5238af5d9693a8cf375f9908680562e0ec2c7b893d4b7cb3f8342da14cefd97cac3b3ebbd13188857bd9329447843060c87
6
+ metadata.gz: 84c075dd32cfb36b0073cbde39fd604095d47a36bd45d46355b33d035f55b8f71ca949d413a9f0db04f853e7c332c89c6b9d5b16725d0c65d67af227c6e0054a
7
+ data.tar.gz: 696d1df0b628def8e953965ed4541ece093f96f2dae053b03388b8a247ea6206e96788da8983632213ccfe5879bd34eca445e2aaa61b3914ed7c7b5d6ba27e2c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- http_store (0.3.2)
4
+ http_store (0.3.3)
5
5
  activerecord (>= 5.0, < 6.1)
6
6
  hashie (~> 3.0)
7
7
  rails (>= 5, < 7)
data/README.md CHANGED
@@ -82,7 +82,7 @@ Default use activerecord, you can to rewrite it by setting
82
82
 
83
83
  ### File storable
84
84
 
85
- When request/response having a file(size limit 30_000), it will use `storable_hash` to format to a hash, `{digest: '', origin: data[0..1000]'}`
85
+ When request/response having a file(size limit 30_000), it will use `storable(data)` to format to a hash, `{digest: '', origin: data[0..1000]'}`
86
86
 
87
87
  ## Development
88
88
 
@@ -51,7 +51,7 @@ module HttpStore
51
51
  end
52
52
 
53
53
  def gen_request_digest
54
- Digest::SHA1.hexdigest(storable_hash(request_digest_hash).to_json)
54
+ Digest::SHA1.hexdigest(storable(request_digest_hash).to_json)
55
55
  end
56
56
  end
57
57
  end
@@ -16,32 +16,28 @@ module HttpStore
16
16
  HttpStore.config.store_class.new(storable_meta).save
17
17
  end
18
18
 
19
- private
20
-
21
19
  def storable_meta
22
20
  @storable_meta ||= gen_storable_meta
23
21
  end
24
22
 
25
23
  def gen_storable_meta
26
24
  @meta.slice(*HttpStore::STORE_KEYS).map do |k, v|
27
- [k, v.is_a?(Hash) ? storable_hash(v).to_json[0..STRING_LIMIT_SIZE] : v]
25
+ [k, v.is_a?(Hash) || v.is_a?(Array) ? storable(v).to_json[0..STRING_LIMIT_SIZE] : v]
28
26
  end.to_h
29
27
  end
30
28
 
31
- def storable_hash(hash)
32
- hash.map do |k, v|
33
- [k, case v
34
- when Hash
35
- storable_hash(v)
36
- when String
37
- storable_string(v)
38
- when Class
39
- v.to_s
40
- else
41
- v
42
- end
43
- ]
44
- end.to_h
29
+ def storable(value)
30
+ case value
31
+ when Hash
32
+ value.map { |k, v| [k, storable(v)] }.to_h
33
+ when Array
34
+ value.map { |v| storable(v) }
35
+ when String
36
+ json = JSON.parse(v) rescue nil
37
+ json ? storable(json) : storable_string(v)
38
+ else
39
+ v.try(:to_h) || v.try(:to_a) || v
40
+ end
45
41
  end
46
42
 
47
43
  def storable_string(str)
@@ -0,0 +1,44 @@
1
+ module HttpStore
2
+ module Middleware
3
+ class RequestLog
4
+ include HttpStore::Helpers::Storable
5
+
6
+ STRING_LIMIT_SIZE = 30_000
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ status, headers, body = @app.call(env)
14
+ [status, headers, body]
15
+ ensure
16
+ build_meta(env, status, headers, body)
17
+ store_request
18
+ end
19
+
20
+ def build_meta(env, status, headers, body)
21
+ request = ActionDispatch::Request.new(env)
22
+
23
+ @meta = format_req(request)
24
+ @meta.merge!(format_rsp(status, headers, body))
25
+ end
26
+
27
+ def format_req(request)
28
+ {
29
+ data: request.params,
30
+ headers: request.headers.select { |k, _v| k.start_with? 'HTTP_' }.to_h,
31
+ query_params: request.query_parameters
32
+ }
33
+ end
34
+
35
+ def format_rsp(status, headers, body)
36
+ {
37
+ status_code: status,
38
+ response_headers: headers,
39
+ response: body.try(:body) || body
40
+ }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpStore
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - black
@@ -162,6 +162,7 @@ files:
162
162
  - lib/http_store/helpers/responseable.rb
163
163
  - lib/http_store/helpers/storable.rb
164
164
  - lib/http_store/http_log.rb
165
+ - lib/http_store/middleware/request_log.rb
165
166
  - lib/http_store/version.rb
166
167
  homepage: https://github.com/308820773/http-store
167
168
  licenses: []