http_store 0.2.1 → 0.3.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: 56d5372d4f8a5d499a4556320d98ff1af22a2bb9ed04b8ff76033dc1e5fb94d0
4
- data.tar.gz: 48fe733db93cf5ac6fcd0f671e34e576b2ab936f10fd3e8f266cce1843d42f1c
3
+ metadata.gz: d19ed690d1cf9d1fb09ea54be7077ba76d99de22adef71981d8ec223ef9233f2
4
+ data.tar.gz: 67e08a8941b383daf5ddf8c682848b1436d74a1aa18029b49137f6c17265cf90
5
5
  SHA512:
6
- metadata.gz: 0c7bfebf305491f6ae57bcb0a938605579eec3490611737533a090172d15f7f2f4813dec758b68d64373bd756dd0055db23be15f04af0d98f996575474a031a7
7
- data.tar.gz: 151159953eb67f376fb0a6181eadadeb67d447894a9e482b275c50beddf8da6d1cac42d48ef17bddf61de4c60731f01acb33a8c6a56d508764b3ef96a0b81bc6
6
+ metadata.gz: e5ea3799b181497e6b2a323743a440d598992837ccc421982c4e6177615ed13a0e30ad01b535348d9dbf5f0afd8207ddeaec5f1d9c1f42ce52d7797f9e0ec431
7
+ data.tar.gz: 248bacb945b9f30c1c878f925e9c1607786c94fbea26b6c37841f88e984c9349dd592c051c8067c0bd74d5542b90fe28e0eb7acaeca5de798547d03d3c25dca3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- http_store (0.2.1)
4
+ http_store (0.3.0)
5
5
  activerecord (>= 5.0, < 6.1)
6
6
  hashie (~> 3.0)
7
7
  rails (>= 5, < 7)
@@ -24,7 +24,7 @@ class CreateHttpLogs < ActiveRecord::Migration[5.2]
24
24
  t.text :response_data, comment: '格式化后的 response'
25
25
 
26
26
  # relation
27
- t.string :type, comment: '请求类型'
27
+ t.string :client_type, comment: '请求类型'
28
28
  t.string :requestable_id, comment: '外键 ID'
29
29
  t.string :requestable_type, comment: '外键 类型'
30
30
 
@@ -0,0 +1,12 @@
1
+ module HttpStore
2
+ class InitializerGenerator < Rails::Generators::Base
3
+ def create_initializer_file
4
+ create_file "config/initializers/http_store.rb", <<FILE
5
+ # HttpStore Config
6
+ HttpStore.configure do
7
+ end
8
+ FILE
9
+
10
+ end
11
+ end
12
+ end
@@ -1,9 +1,5 @@
1
1
  module HttpStore
2
2
  class Client
3
- META_KEYS = %w[http_method url data_type headers query_params data other_params request_valid
4
- status_code response response_headers response_valid response_data
5
- request_digest type requestable requestable_id requestable_type response_obj]
6
-
7
3
  attr_accessor :meta
8
4
 
9
5
  include HttpStore::Helpers::Requestable
@@ -11,18 +7,19 @@ module HttpStore
11
7
  include HttpStore::Helpers::Storable
12
8
 
13
9
  def self.execute(requestable, other_params = {})
14
- new(requestable: requestable,
15
- type: to_s,
16
- other_params: other_params)
10
+ new(requestable: requestable, other_params: other_params)
17
11
  end
18
12
 
19
13
  def initialize(args)
20
- @meta = Hashie::Mash.new(args)
21
- @meta.request_digest = gen_request_digest(@meta.to_json)
22
-
14
+ @meta = Hashie::Mash.new(args)
23
15
  build_request
24
16
 
25
- execute if request_valid # send request
17
+ return unless request_valid?
18
+
19
+ # exist request, so return
20
+ return if !@meta.other_params.force && storeable_record.present?
21
+
22
+ execute # send request
26
23
  raise HttpStore::RequestError, 'response_obj is nil' if response_obj.nil?
27
24
 
28
25
  build_response
@@ -30,11 +27,7 @@ module HttpStore
30
27
  store_request
31
28
  end
32
29
 
33
- def gen_request_digest(str)
34
- Digest::SHA1.hexdigest(str)
35
- end
36
-
37
- META_KEYS.each do |meta_key|
30
+ HttpStore::ALL_KEYS.each do |meta_key|
38
31
  define_method meta_key do
39
32
  @meta.send(meta_key)
40
33
  end
@@ -0,0 +1,13 @@
1
+ module HttpStore
2
+ class Configuration
3
+ attr_writer :store_enable, :store_class
4
+
5
+ def store_enable
6
+ @store_enable || true
7
+ end
8
+
9
+ def store_class
10
+ @store_class || HttpStore::HttpLog
11
+ end
12
+ end
13
+ end
@@ -36,8 +36,18 @@ module HttpStore
36
36
  @meta.query_params ||= {}
37
37
  @meta.data ||= {}
38
38
 
39
+ @meta.requestable_id = requestable.try(:id)
40
+ @meta.requestable_type = requestable.try(:class).try(:to_s)
41
+ @meta.client_type = self.class.to_s
42
+ @meta.request_digest = gen_request_digest
43
+
39
44
  @meta.request_valid = request_valid?
40
45
  end
46
+
47
+ def gen_request_digest
48
+ request_str = storable_hash(@meta.slice(*HttpStore::DIGEST_KEYS)).to_json
49
+ Digest::SHA1.hexdigest(request_str)
50
+ end
41
51
  end
42
52
  end
43
53
  end
@@ -6,14 +6,6 @@ module HttpStore
6
6
  status_code == 200
7
7
  end
8
8
 
9
- def response_error_handle
10
- 'error'
11
- end
12
-
13
- def response_success_handle
14
- 'success'
15
- end
16
-
17
9
  def json_response?
18
10
  response_headers_hash['content_type'].to_s =~ /json/
19
11
  end
@@ -24,7 +16,12 @@ module HttpStore
24
16
  @meta.response_headers = response_obj.headers
25
17
 
26
18
  @meta.response_valid = !!response_status_check
27
- @meta.response_data = response_valid ? response_success_handle : response_error_handle
19
+ @meta.response_data = build_response_data
20
+ raise HttpStore::RequestError, '三方请求异常, 请与管理员联系' if response_data.blank?
21
+ end
22
+
23
+ def build_response_data
24
+ response_valid ? 'success' : 'error'
28
25
  end
29
26
  end
30
27
  end
@@ -2,21 +2,28 @@ module HttpStore
2
2
  module Helpers
3
3
  module Storable
4
4
  STRING_LIMIT_SIZE = 30_000
5
- STORE_KEYS = HttpStore::Client::META_KEYS - %w[response_obj requestable]
5
+
6
+ def storeable_record
7
+ return unless HttpStore.config.store_enable
8
+
9
+ @storeable_model ||= HttpStore.config.store_class.find_by(request_digest: request_digest, response_valid: true)
10
+ end
6
11
 
7
12
  # you can rewrite this callback, to store the request
8
13
  def store_request
9
- return if other_params.store_class == false
14
+ return unless HttpStore.config.store_enable
10
15
 
11
- (other_params.store_class || HttpStore::HttpLog).new(storable_meta).save
16
+ storeable_class.new(storable_meta).save
12
17
  end
13
18
 
19
+ private
20
+
14
21
  def storable_meta
15
22
  @storable_meta ||= gen_storable_meta
16
23
  end
17
24
 
18
25
  def gen_storable_meta
19
- @meta.slice(*STORE_KEYS).map do |k, v|
26
+ @meta.slice(*HttpStore::STORE_KEYS).map do |k, v|
20
27
  [k, v.is_a?(Hash) ? storable_hash(v).to_json[0..STRING_LIMIT_SIZE] : v]
21
28
  end.to_h
22
29
  end
@@ -28,6 +35,8 @@ module HttpStore
28
35
  storable_hash(v)
29
36
  when String
30
37
  storable_string(v)
38
+ when Class
39
+ v.to_s
31
40
  else
32
41
  v
33
42
  end
@@ -36,7 +45,7 @@ module HttpStore
36
45
  end
37
46
 
38
47
  def storable_string(str)
39
- { digest: Digest::SHA1.hexdigest(str), origin: str[0..1000] } if str.length > STRING_LIMIT_SIZE
48
+ str.length > STRING_LIMIT_SIZE ? { digest: Digest::SHA1.hexdigest(str), origin: str[0..1000] } : str
40
49
  end
41
50
  end
42
51
  end
@@ -1,3 +1,3 @@
1
1
  module HttpStore
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/http_store.rb CHANGED
@@ -1,13 +1,24 @@
1
1
  require 'active_record'
2
2
  require 'rails/engine'
3
3
  require 'active_support/core_ext/module'
4
-
5
4
  require 'hashie'
6
5
  require 'rest-client'
6
+ require 'http_store/engine'
7
7
 
8
8
  module HttpStore
9
9
  extend ActiveSupport::Autoload
10
10
 
11
+ REQUEST_KEYS = %w[http_method url data_type headers query_params data other_params request_valid]
12
+ RESPONSE_KEYS = %w[status_code response response_headers response_data response_valid]
13
+ META_KEYS = %w[request_digest client_type requestable_id requestable_type]
14
+ TMP_KEYS = %w[requestable response_obj]
15
+
16
+ DIGEST_KEYS = REQUEST_KEYS + %w[requestable_id requestable_type]
17
+ ALL_KEYS = REQUEST_KEYS + RESPONSE_KEYS + META_KEYS + TMP_KEYS
18
+ STORE_KEYS = REQUEST_KEYS + RESPONSE_KEYS + META_KEYS
19
+
20
+ class RequestError < StandardError; end
21
+
11
22
  module Helpers
12
23
  extend ActiveSupport::Autoload
13
24
 
@@ -20,6 +31,15 @@ module HttpStore
20
31
  autoload :VERSION
21
32
  autoload :HttpLog
22
33
  autoload :Client
34
+ autoload :Configuration
23
35
 
24
- class RequestError < StandardError; end
36
+ class << self
37
+ def config
38
+ @config ||= Configuration.new
39
+ end
40
+
41
+ def configure(&block)
42
+ yield(config)
43
+ end
44
+ end
25
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - black
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-19 00:00:00.000000000 Z
11
+ date: 2019-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -153,8 +153,10 @@ files:
153
153
  - bin/setup
154
154
  - db/migrate/1_create_http_logs.rb
155
155
  - http_store.gemspec
156
+ - lib/generators/http_store/initializer_generator.rb
156
157
  - lib/http_store.rb
157
158
  - lib/http_store/client.rb
159
+ - lib/http_store/configuration.rb
158
160
  - lib/http_store/engine.rb
159
161
  - lib/http_store/helpers/requestable.rb
160
162
  - lib/http_store/helpers/responseable.rb