makeleaps-ruby 0.1.2 → 0.1.3

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: 197f77977864602aa6998870839d8b46802af2a1283894c61bdc8ffb1a67b12f
4
- data.tar.gz: dd6d57e4176c9fc5b0598f7fe571c9996ae63809bf88b07bfe663154ac0814dc
3
+ metadata.gz: 4660d3c49dd4f6c72b502a5f407ef51735878871dcc993b6fa48d853abc4210b
4
+ data.tar.gz: f38d04e5c5878aa7149ba81a33fe40083516f751cc80e59eadb6bf91c73793f2
5
5
  SHA512:
6
- metadata.gz: d4c6d791a94edaa44e903b763e7414eef73497cec011e2002305695b8a6e96c137910414182fa738d7b8294b307418f37e60ddc508e1c9ca2ede29ed1865205c
7
- data.tar.gz: 5592b35fe4ad502f60890e12e341edc0a3fac41148b6d372dd6366b13090cfff422733749197c582c7b68f1205438bc0bb1f3c92909977cd1b8dc40a4dce1656
6
+ metadata.gz: e222e2a49796f5c6bfed46d6c5ee2e3b357d3536e8a1c71ab964490db730c0878689328dd58c921b681a48e88dc9dbc8873da903961eb2ec4a338da936001ce1
7
+ data.tar.gz: 3266c18b2ce9187be95966f01078995a7ee910f1981ce6e1d339b4990ca934fe41631522fe1a81e52a55de8933cbcea5553701d5bac158511e9877fa6feeb196
data/README.md CHANGED
@@ -59,6 +59,21 @@ client.get :document, yyyyy # en
59
59
  client.get :document # fetching a list of documents
60
60
  client.get :contact # fetching a list of contacts
61
61
 
62
+ # adding a query
63
+ documents = client.get :document { |req| req.params['document_number'] = "1000" }
64
+ documents = client.get :document { |req| req.params['date__lt'] = "2020-01-30" }
65
+ ```
66
+
67
+ #### Traversal access
68
+ When you want to retrieve a collection of resources (e.g. invoices, cliet contacts..) which size is larger than that a single response can contain, you need to make a consecutive chain of calls, following the `next` link provided by the prior response.
69
+
70
+ To traverse multiple pages following `next` links, below methods such as `#each_page`, `#each_resource`, `#find_resource` are available for convenience.
71
+
72
+ WARNING: this could potentially invoke an unexpected number of API calls. Consider limiting your possibile accesses, otherwise you might exceed the maximum allowed number of requests (see [throttling](https://app.makeleaps.com/api/docs/) in the API document)
73
+
74
+ Current default limit is set as 100 pages / each method, and the rate limit is set as 0.1sec / request so that it won't cause accidental traffic to the API.
75
+
76
+ ```ruby
62
77
  # visiting all pages (following `next` links provided by API)
63
78
  # (requests are limited at maximum rate of 0.1sec per each)
64
79
  total_count = 0
@@ -66,8 +81,25 @@ client.each_page :document do |documents|
66
81
  total_count += documents.count
67
82
  end
68
83
 
84
+ # query strings can be added with `params` option
85
+ # this will invoke
86
+ # GET 'https://api.makeleaps.com/api/partner/<partner_id>/document/?document_type=invoice&&date__gte=2019-01-01&&date__lt=2019-04-01'
87
+ # and follows `next` url until it reaches to the last page OR the maximum limit page (default 100)
88
+ total_count = 0
89
+ client.each_page :document, params: {document_type: :invoice, date__gte: '2019-01-01', date__lt: '2019-04-01'} do |documents|
90
+ total_count += documents.count
91
+ end
92
+
93
+ # each resources (that each page contains) can also be accessed via #each_resource
94
+ # (interface are consistent with #each_page)
95
+ client.each_resouce :document, params: {document_type: :invoice, date__gte: '2019-01-01', date__lt: '2019-04-01'} do |invoice|
96
+ puts invoice['display_name']
97
+ end
98
+
69
99
  # find a resource (out of all the pages)
70
- client.find_resource(:document) do |document|
100
+ # visits next_urls one after another. terminates when it find the first resource (with whivh the given block evaluates as true), or when it reaches to one of either the last page OR the max page (default 100)
101
+ # returns the detected resource or nil (when not found)
102
+ client.find_resource(:document), params: {document_type: :invoice} do |document|
71
103
  document['title'].match? /URGENT/
72
104
  end
73
105
 
@@ -76,10 +108,6 @@ documents = client.get :document
76
108
  document.find_resource do |document|
77
109
  document['document_number'].to_i >= 123 && document['note'].match? /IMPORTANT/
78
110
  end
79
-
80
- # adding a query
81
- documents = client.get :document { |req| req.params['document_number'] = "1000" }
82
- documents = client.get :document { |req| req.params['date__lt'] = "2020-01-30" }
83
111
  ```
84
112
 
85
113
  #### POST/PATCH/PUT/DELETE
@@ -10,18 +10,20 @@ module Makeleaps
10
10
  attr_reader :request
11
11
  def_delegators :@request, :get, :post, :put, :patch, :delete, :options
12
12
  def_delegators :@request, :each_page, :each_resource, :find_resource, :set_partner!
13
+ def_delegators :@request, :connection
13
14
 
14
15
  def initialize(username, password)
15
- @auth = Makeleaps::Request::BasicAuth.new(username, password)
16
+ @auth_request = Makeleaps::Request::BasicAuth.new(username, password)
16
17
  end
17
18
 
18
19
  def connect!
19
- token_store = @auth.make_request!
20
- @request = Makeleaps::Request::Generic.new(token_store.access_token)
20
+ @token_store ||= @auth_request.authenticate!
21
+ @request = Makeleaps::Request::Generic.new(@token_store.access_token) if @token_store.valid?
21
22
  end
22
23
 
23
- def connection
24
- request.connection
24
+ def disconnect!
25
+ @auth_request.revoke!(@token_store.access_token) if @token_store&.valid?
26
+ @token_store = nil
25
27
  end
26
28
  end
27
29
  end
@@ -5,7 +5,8 @@ module Makeleaps
5
5
  class BasicAuth < Base
6
6
  include ErrorHandler
7
7
 
8
- AUTH_ENDPOINT = 'user/oauth2/token/'
8
+ AUTH_ENDPOINT = 'user/oauth2/token/'
9
+ REVOCATKON_ENDPOINT = 'user/oauth2/revoke-token/'
9
10
 
10
11
  def initialize(username, password)
11
12
  super() do |conn|
@@ -13,12 +14,18 @@ module Makeleaps
13
14
  end
14
15
  end
15
16
 
16
- def make_request!
17
+ def authenticate!
17
18
  response = handle_api_response do
18
- connection.post(AUTH_ENDPOINT) { |req| req.params['grant_type'] = 'client_credentials'}
19
+ connection.post(AUTH_ENDPOINT) { |req| req.params['grant_type'] = 'client_credentials' }
19
20
  end
20
21
  Makeleaps::Response::TokenStore.new response
21
22
  end
23
+
24
+ def revoke!(token)
25
+ handle_api_response do
26
+ connection.post(REVOCATKON_ENDPOINT) { |req| req.params['token'] = token }
27
+ end
28
+ end
22
29
  end
23
30
  end
24
31
  end
@@ -27,12 +27,16 @@ module Makeleaps
27
27
  partners.find_resource { |partner| partner['name'] == name }
28
28
  end
29
29
 
30
- # process sequentially (avoid eager loading) to ensure minimum API access
31
- def each_page(start_page , &block)
30
+ # set max_pages to change the maximum number of retrievable pages
31
+ # WARNING: this might invoke too many API accesses.
32
+ def each_page(start_page, *args, params: nil, max_pages: nil, &block)
32
33
  next_url = start_page
34
+ max_pages = max_pages || 100 # TODO: let default value be customizeable using config
33
35
 
34
- loop do
35
- response = get(next_url)
36
+ max_pages.times do
37
+ response = get(next_url, *args) { |req|
38
+ req.params.merge!(params) unless params.nil?
39
+ }
36
40
  block.call(response)
37
41
  next_url = response.next
38
42
  break unless next_url
@@ -40,16 +44,18 @@ module Makeleaps
40
44
  end
41
45
  end
42
46
 
43
- def each_resource(start_page , &block)
44
- each_page(start_page) do |page|
47
+ # set max_pages to change the maximum number of retrievable pages
48
+ # WARNING: this might invoke too many API accesses.
49
+ def each_resource(start_page, *args, params: nil, max_pages: nil, &block)
50
+ each_page(start_page, *args, params: params, max_pages: max_pages) do |page|
45
51
  page.each_resource do |resource|
46
52
  block.call(resource)
47
53
  end
48
54
  end
49
55
  end
50
56
 
51
- def find_resource(start_page, *args, &block)
52
- each_page(start_page) do |page|
57
+ def find_resource(start_page, *args, params: nil, max_pages: nil, &block)
58
+ each_page(start_page, params: params, max_pages: max_pages) do |page|
53
59
  resource = page.find_resource(*args, &block)
54
60
  return resource if resource
55
61
  end
@@ -14,10 +14,12 @@ module Makeleaps
14
14
  @access_token ||= body['access_token']
15
15
  end
16
16
 
17
- def valid_request?
17
+ def valid?
18
18
  response && response.status == 200 && !expired?
19
19
  end
20
20
 
21
+ private
22
+
21
23
  def expired?
22
24
  Time.now >= requested_at + expiration_period
23
25
  end
@@ -1,3 +1,3 @@
1
1
  module Makeleaps
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makeleaps-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji Onishi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-21 00:00:00.000000000 Z
11
+ date: 2019-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday