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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efe74a757031f1123ecc87e61f81e2743ee95a8edb288a5feab2097a621c59cb
4
- data.tar.gz: ee93a150b6b6059663cfc73ce91d013777c0e876c66ccdc6204f32ca5ca18189
3
+ metadata.gz: 2c630613b04c77a91abcfd9de63daa0f77f8c0942775a45a07c108f91b2a2e33
4
+ data.tar.gz: dadd6adda88db7852f73a467276d4ec61ce00e0889d7d865db21cc5d8f365ec2
5
5
  SHA512:
6
- metadata.gz: a5d5c446224bfeb89f9c4336b53f572f0b2db3bbf83d3c366c70898f9bf6b9cb82c1270fbb662cad65cabaf13855b84dfc6701775912d548115453af07aa5989
7
- data.tar.gz: '09e9c146add47d8cb6482cc0e98d59ba289d1783ba227c02402339fb5b25372d839e77a76bcec5c71a4ba7ed9d3b56ba4f6bcb4546c8be2045bfc8edb979efb7'
6
+ metadata.gz: 72c9b03b19e5521d94ccde93bf3572fa59de2207783ec52ab9300a0a7761d4febbf3bde07f018f8835adab3a37208ff3938908f9fe8326c4f93b2c280dd967fd
7
+ data.tar.gz: 4ef08840bf1668823f0305d5f5dda5238af5d9693a8cf375f9908680562e0ec2c7b893d4b7cb3f8342da14cefd97cac3b3ebbd13188857bd9329447843060c87
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- http_store (0.3.1)
4
+ http_store (0.3.2)
5
5
  activerecord (>= 5.0, < 6.1)
6
6
  hashie (~> 3.0)
7
7
  rails (>= 5, < 7)
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
- ```ruby
32
- class YourRequest < HttpStore::RestRequest
33
- def build_request
34
- @query_params_hash = {
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
- self.http_method = 'POST'
39
- self.url = 'https://www.example.com'
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
- def response_status_check
43
- status_code == 200 && response_hash.access_token.present?
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 rsp_success_data
47
- response_hash.access_token
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 = REQUEST_KEYS + %w[requestable_id requestable_type]
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
 
@@ -16,8 +16,8 @@ module HttpStore
16
16
 
17
17
  return unless request_valid?
18
18
 
19
- # exist request, so return
20
- return if !@meta.other_params.force && storeable_record.present?
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
- request_str = storable_hash(@meta.slice(*HttpStore::DIGEST_KEYS)).to_json
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 response_status_check
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 = !!response_status_check
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
@@ -1,3 +1,3 @@
1
1
  module HttpStore
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  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.3.1
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-20 00:00:00.000000000 Z
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client