http_store 0.2.0 → 0.2.1

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: e46d9d716290cd38b9293d0d3ae10a6d387cb2ce02712e180fdd060a9da2bb2b
4
- data.tar.gz: a313bfb9874387e2f3a3a738d05c273b883824cccdd826b9eb85fba2008dfc54
3
+ metadata.gz: 56d5372d4f8a5d499a4556320d98ff1af22a2bb9ed04b8ff76033dc1e5fb94d0
4
+ data.tar.gz: 48fe733db93cf5ac6fcd0f671e34e576b2ab936f10fd3e8f266cce1843d42f1c
5
5
  SHA512:
6
- metadata.gz: 752515be72b99a4ea27e493c143afdedb69308bb37d92ffca0ea1de584a075150e63f5dc1a3d0c4da018a76947b32fd35d1ece54cd825988468f05b799691758
7
- data.tar.gz: 1c25fd60ad38d88054189b5faa7e7d2b3ffba5c9cb681e4800919fa9c3ddd57ac7f39332993f20f4489f29fc5266b459e3ec75dad75c2de6f4ec7e807cece368
6
+ metadata.gz: 0c7bfebf305491f6ae57bcb0a938605579eec3490611737533a090172d15f7f2f4813dec758b68d64373bd756dd0055db23be15f04af0d98f996575474a031a7
7
+ data.tar.gz: 151159953eb67f376fb0a6181eadadeb67d447894a9e482b275c50beddf8da6d1cac42d48ef17bddf61de4c60731f01acb33a8c6a56d508764b3ef96a0b81bc6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- http_store (0.2.0)
4
+ http_store (0.2.1)
5
5
  activerecord (>= 5.0, < 6.1)
6
6
  hashie (~> 3.0)
7
7
  rails (>= 5, < 7)
@@ -6,8 +6,9 @@ module HttpStore
6
6
 
7
7
  attr_accessor :meta
8
8
 
9
- include HttpStore::Requestable
10
- include HttpStore::Responseable
9
+ include HttpStore::Helpers::Requestable
10
+ include HttpStore::Helpers::Responseable
11
+ include HttpStore::Helpers::Storable
11
12
 
12
13
  def self.execute(requestable, other_params = {})
13
14
  new(requestable: requestable,
@@ -0,0 +1,43 @@
1
+ module HttpStore
2
+ module Helpers
3
+ module Requestable
4
+ # will return one hash to set the request meta
5
+ def set_request
6
+ {}
7
+ end
8
+
9
+ # you need rewrite this checker, when return false the request don't send
10
+ def request_valid?
11
+ true
12
+ end
13
+
14
+ def json_request?
15
+ data_type.to_s.casecmp('json').zero?
16
+ end
17
+
18
+ def uri
19
+ "#{url}?#{query_params.to_query}"
20
+ end
21
+
22
+ def get?
23
+ http_method.to_s.casecmp('get').zero?
24
+ end
25
+
26
+ def post?
27
+ http_method.to_s.casecmp('post').zero?
28
+ end
29
+
30
+ def format_request
31
+ @meta.http_method = get? ? 'GET' : 'POST' # only support get/post
32
+
33
+ @meta.headers ||= { charset: 'UTF-8' }
34
+ @meta.headers[:content_type] = :json if json_request?
35
+
36
+ @meta.query_params ||= {}
37
+ @meta.data ||= {}
38
+
39
+ @meta.request_valid = request_valid?
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,31 @@
1
+ module HttpStore
2
+ module Helpers
3
+ module Responseable
4
+ # check response message is right, if is right
5
+ def response_status_check
6
+ status_code == 200
7
+ end
8
+
9
+ def response_error_handle
10
+ 'error'
11
+ end
12
+
13
+ def response_success_handle
14
+ 'success'
15
+ end
16
+
17
+ def json_response?
18
+ response_headers_hash['content_type'].to_s =~ /json/
19
+ end
20
+
21
+ def build_response
22
+ @meta.status_code = response_obj.code
23
+ @meta.response = json_safe_parse(response_obj.body)
24
+ @meta.response_headers = response_obj.headers
25
+
26
+ @meta.response_valid = !!response_status_check
27
+ @meta.response_data = response_valid ? response_success_handle : response_error_handle
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ module HttpStore
2
+ module Helpers
3
+ module Storable
4
+ STRING_LIMIT_SIZE = 30_000
5
+ STORE_KEYS = HttpStore::Client::META_KEYS - %w[response_obj requestable]
6
+
7
+ # you can rewrite this callback, to store the request
8
+ def store_request
9
+ return if other_params.store_class == false
10
+
11
+ (other_params.store_class || HttpStore::HttpLog).new(storable_meta).save
12
+ end
13
+
14
+ def storable_meta
15
+ @storable_meta ||= gen_storable_meta
16
+ end
17
+
18
+ def gen_storable_meta
19
+ @meta.slice(*STORE_KEYS).map do |k, v|
20
+ [k, v.is_a?(Hash) ? storable_hash(v).to_json[0..STRING_LIMIT_SIZE] : v]
21
+ end.to_h
22
+ end
23
+
24
+ def storable_hash(hash)
25
+ hash.map do |k, v|
26
+ [k, case v
27
+ when Hash
28
+ storable_hash(v)
29
+ when String
30
+ storable_string(v)
31
+ else
32
+ v
33
+ end
34
+ ]
35
+ end.to_h
36
+ end
37
+
38
+ def storable_string(str)
39
+ { digest: Digest::SHA1.hexdigest(str), origin: str[0..1000] } if str.length > STRING_LIMIT_SIZE
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpStore
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/http_store.rb CHANGED
@@ -8,12 +8,16 @@ require 'rest-client'
8
8
  module HttpStore
9
9
  extend ActiveSupport::Autoload
10
10
 
11
+ module Helpers
12
+ extend ActiveSupport::Autoload
13
+
14
+ autoload :Requestable
15
+ autoload :Responseable
16
+ autoload :Storable
17
+ end
18
+
11
19
  autoload :Engine
12
20
  autoload :VERSION
13
-
14
- autoload :Requestable
15
- autoload :Responseable
16
- autoload :Storable
17
21
  autoload :HttpLog
18
22
  autoload :Client
19
23
 
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - black
@@ -156,10 +156,10 @@ files:
156
156
  - lib/http_store.rb
157
157
  - lib/http_store/client.rb
158
158
  - lib/http_store/engine.rb
159
+ - lib/http_store/helpers/requestable.rb
160
+ - lib/http_store/helpers/responseable.rb
161
+ - lib/http_store/helpers/storable.rb
159
162
  - lib/http_store/http_log.rb
160
- - lib/http_store/requestable.rb
161
- - lib/http_store/responseable.rb
162
- - lib/http_store/storable.rb
163
163
  - lib/http_store/version.rb
164
164
  homepage: https://github.com/308820773/http-store
165
165
  licenses: []
@@ -1,41 +0,0 @@
1
- module HttpStore
2
- module Requestable
3
- # will return one hash to set the request meta
4
- def set_request
5
- {}
6
- end
7
-
8
- # you need rewrite this checker, when return false the request don't send
9
- def request_valid?
10
- true
11
- end
12
-
13
- def json_request?
14
- data_type.to_s.casecmp('json').zero?
15
- end
16
-
17
- def uri
18
- "#{url}?#{query_params.to_query}"
19
- end
20
-
21
- def get?
22
- http_method.to_s.casecmp('get').zero?
23
- end
24
-
25
- def post?
26
- http_method.to_s.casecmp('post').zero?
27
- end
28
-
29
- def format_request
30
- @meta.http_method = get? ? 'GET' : 'POST' # only support get/post
31
-
32
- @meta.headers ||= { charset: 'UTF-8' }
33
- @meta.headers[:content_type] = :json if json_request?
34
-
35
- @meta.query_params ||= {}
36
- @meta.data ||= {}
37
-
38
- @meta.request_valid = request_valid?
39
- end
40
- end
41
- end
@@ -1,29 +0,0 @@
1
- module HttpStore
2
- module Responseable
3
- # check response message is right, if is right
4
- def response_status_check
5
- status_code == 200
6
- end
7
-
8
- def response_error_handle
9
- 'error'
10
- end
11
-
12
- def response_success_handle
13
- 'success'
14
- end
15
-
16
- def json_response?
17
- response_headers_hash['content_type'].to_s =~ /json/
18
- end
19
-
20
- def build_response
21
- @meta.status_code = response_obj.code
22
- @meta.response = json_safe_parse(response_obj.body)
23
- @meta.response_headers = response_obj.headers
24
-
25
- @meta.response_valid = !!response_status_check
26
- @meta.response_data = response_valid ? response_success_handle : response_error_handle
27
- end
28
- end
29
- end
@@ -1,41 +0,0 @@
1
- module HttpStore
2
- module Storable
3
- STRING_LIMIT_SIZE = 30_000
4
- STORE_KEYS = HttpStore::Client::META_KEYS - %w[response_obj requestable]
5
-
6
- # you can rewrite this callback, to store the request
7
- def store_request
8
- return if other_params.store_class != false
9
-
10
- (other_params.store_class || HttpStore::HttpLog).new(storable_meta).save
11
- end
12
-
13
- def storable_meta
14
- @storable_meta ||= gen_storable_meta
15
- end
16
-
17
- def gen_storable_meta
18
- @meta.slice(*STORE_KEYS).map do |k, v|
19
- [k, v.is_a?(Hash) ? storable_hash(v).to_json[0..STRING_LIMIT_SIZE] : v]
20
- end.to_h
21
- end
22
-
23
- def storable_hash(hash)
24
- hash.map do |k, v|
25
- [k, case value
26
- when Hash
27
- storable_hash(v)
28
- when String
29
- storable_string(v)
30
- else
31
- v
32
- end
33
- ]
34
- end.to_h
35
- end
36
-
37
- def storable_string(str)
38
- { digest: Digest::SHA1.hexdigest(str), origin: str[0..1000] } if str.length > STRING_LIMIT_SIZE
39
- end
40
- end
41
- end