http_store 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +53 -18
- data/db/migrate/1_create_http_logs.rb +1 -0
- data/lib/http_store.rb +2 -2
- data/lib/http_store/client.rb +2 -2
- data/lib/http_store/helpers/requestable.rb +7 -2
- data/lib/http_store/helpers/responseable.rb +2 -2
- data/lib/http_store/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c630613b04c77a91abcfd9de63daa0f77f8c0942775a45a07c108f91b2a2e33
|
4
|
+
data.tar.gz: dadd6adda88db7852f73a467276d4ec61ce00e0889d7d865db21cc5d8f365ec2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72c9b03b19e5521d94ccde93bf3572fa59de2207783ec52ab9300a0a7761d4febbf3bde07f018f8835adab3a37208ff3938908f9fe8326c4f93b2c280dd967fd
|
7
|
+
data.tar.gz: 4ef08840bf1668823f0305d5f5dda5238af5d9693a8cf375f9908680562e0ec2c7b893d4b7cb3f8342da14cefd97cac3b3ebbd13188857bd9329447843060c87
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -18,37 +18,72 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install http_store
|
20
20
|
|
21
|
-
## Usage
|
21
|
+
## Default Usage
|
22
22
|
|
23
23
|
first, you need create migration whit this command.
|
24
24
|
|
25
|
+
```bash
|
26
|
+
rake http_store_engine:install:migrations # create table(default)
|
27
|
+
rails generator http_store:initializer # create global settings
|
25
28
|
```
|
26
|
-
rake http_store_engine:install:migrations
|
27
|
-
```
|
28
29
|
|
29
|
-
and you can extend the HttpStore::RestRequest to create your rest-client request.
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
### HttpStore::Client
|
32
|
+
you can extend the `HttpStore::Client` to create your rest-client request.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class BaseClient < HttpStore::Client
|
36
|
+
# support `http_method url data_type headers query_params data` keys
|
37
|
+
# url not include the query, use query_params to set query
|
38
|
+
def set_request
|
39
|
+
{
|
40
|
+
data_type: 'json', # support json/form default form
|
41
|
+
data: {}, # http body data
|
42
|
+
headers: {}, # request headers
|
43
|
+
http_method: 'post', # support get/post default post
|
44
|
+
url: 'url'
|
36
45
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
46
|
+
end
|
47
|
+
|
48
|
+
# generate you request digest meta data,
|
49
|
+
# default keys is `http_method url data_type data other_params requestable_id requestable_type`
|
50
|
+
def request_digest_hash
|
51
|
+
super.merge(test: 'your value')
|
40
52
|
end
|
41
|
-
|
42
|
-
|
43
|
-
|
53
|
+
|
54
|
+
# request checker, default is true, if return falsely the request not send
|
55
|
+
def request_valid?
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
# response valid, check response, if return true, next same request will use cache
|
60
|
+
def response_valid?
|
61
|
+
status_code == 200
|
44
62
|
end
|
45
|
-
|
46
|
-
def
|
47
|
-
|
63
|
+
|
64
|
+
def build_response_data
|
65
|
+
if response_valid
|
66
|
+
response.data
|
67
|
+
else
|
68
|
+
response.error
|
69
|
+
end
|
48
70
|
end
|
49
71
|
end
|
50
72
|
```
|
51
73
|
|
74
|
+
### HttpStore::HttpLog
|
75
|
+
|
76
|
+
Default use activerecord, you can to rewrite it by setting
|
77
|
+
|
78
|
+
### Configuration
|
79
|
+
|
80
|
+
- store_enable: default is true
|
81
|
+
- store_class: default is `HttpStore::HttpLog`
|
82
|
+
|
83
|
+
### File storable
|
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]'}`
|
86
|
+
|
52
87
|
## Development
|
53
88
|
|
54
89
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -13,6 +13,7 @@ class CreateHttpLogs < ActiveRecord::Migration[5.2]
|
|
13
13
|
t.text :other_params, comment: '其他自定义参数, 用于构建 query_params/data_type'
|
14
14
|
t.string :request_digest, comment: '请求唯一 id'
|
15
15
|
t.boolean :request_valid, comment: '请求是否符合规范, 若为 false, 不会发起请求'
|
16
|
+
t.boolean :force, comment: '忽略缓存, 强制请求'
|
16
17
|
|
17
18
|
# response
|
18
19
|
t.integer :status_code, comment: 'response http code'
|
data/lib/http_store.rb
CHANGED
@@ -8,12 +8,12 @@ require 'http_store/engine'
|
|
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]
|
11
|
+
REQUEST_KEYS = %w[http_method url data_type headers query_params data other_params force request_valid]
|
12
12
|
RESPONSE_KEYS = %w[status_code response response_headers response_data response_valid]
|
13
13
|
META_KEYS = %w[request_digest client_type requestable_id requestable_type]
|
14
14
|
TMP_KEYS = %w[requestable response_obj]
|
15
15
|
|
16
|
-
DIGEST_KEYS =
|
16
|
+
DIGEST_KEYS = %w[http_method url data_type data other_params requestable_id requestable_type]
|
17
17
|
ALL_KEYS = REQUEST_KEYS + RESPONSE_KEYS + META_KEYS + TMP_KEYS
|
18
18
|
STORE_KEYS = REQUEST_KEYS + RESPONSE_KEYS + META_KEYS
|
19
19
|
|
data/lib/http_store/client.rb
CHANGED
@@ -16,8 +16,8 @@ module HttpStore
|
|
16
16
|
|
17
17
|
return unless request_valid?
|
18
18
|
|
19
|
-
# exist request,
|
20
|
-
return if
|
19
|
+
# exist request or not force, return
|
20
|
+
return if !force && storeable_record.present?
|
21
21
|
|
22
22
|
execute # send request
|
23
23
|
raise HttpStore::RequestError, 'response_obj is nil' if response_obj.nil?
|
@@ -39,14 +39,19 @@ module HttpStore
|
|
39
39
|
@meta.requestable_id = requestable.try(:id)
|
40
40
|
@meta.requestable_type = requestable.try(:class).try(:to_s)
|
41
41
|
@meta.client_type = self.class.to_s
|
42
|
+
@meta.force = @meta.other_params.delete(:force) || false
|
42
43
|
@meta.request_digest = gen_request_digest
|
43
44
|
|
44
45
|
@meta.request_valid = request_valid?
|
45
46
|
end
|
46
47
|
|
48
|
+
# http_method url data_type data other_params requestable_id requestable_type
|
49
|
+
def request_digest_hash
|
50
|
+
@meta.slice(*HttpStore::DIGEST_KEYS)
|
51
|
+
end
|
52
|
+
|
47
53
|
def gen_request_digest
|
48
|
-
|
49
|
-
Digest::SHA1.hexdigest(request_str)
|
54
|
+
Digest::SHA1.hexdigest(storable_hash(request_digest_hash).to_json)
|
50
55
|
end
|
51
56
|
end
|
52
57
|
end
|
@@ -2,7 +2,7 @@ module HttpStore
|
|
2
2
|
module Helpers
|
3
3
|
module Responseable
|
4
4
|
# check response message is right, if is right
|
5
|
-
def
|
5
|
+
def response_valid?
|
6
6
|
status_code == 200
|
7
7
|
end
|
8
8
|
|
@@ -15,7 +15,7 @@ module HttpStore
|
|
15
15
|
@meta.response = json_safe_parse(response_obj.body)
|
16
16
|
@meta.response_headers = response_obj.headers
|
17
17
|
|
18
|
-
@meta.response_valid =
|
18
|
+
@meta.response_valid = response_valid?
|
19
19
|
@meta.response_data = build_response_data
|
20
20
|
raise HttpStore::RequestError, '三方请求异常, 请与管理员联系' if response_data.blank?
|
21
21
|
end
|
data/lib/http_store/version.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.2
|
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-
|
11
|
+
date: 2019-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|