hubrise_client 2.0.3 → 2.0.4
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 +4 -4
- data/README.md +18 -0
- data/lib/hubrise_client/base.rb +34 -27
- data/lib/hubrise_client/request.rb +52 -27
- data/lib/hubrise_client/response.rb +28 -3
- data/lib/hubrise_client/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: 58aebdf2e9ba790c1303eacf547aaaf5ccdf92f360a63195e6a6e9db1e200b42
|
4
|
+
data.tar.gz: 3776550d4ef358b832c1e4f02094da4ef352b8fddb5ce2d174ae092c6f6ce504
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38787fe30bacac00daae63823a52d69f10347380276afb7b8e9c87386560aed7d97300c66a9abe29dcc03c3ccb3de43f11b9cfca14b3c2d432fd8426ab8ffe4c
|
7
|
+
data.tar.gz: c8304210fb9fcb47b574d131b690f3e3ac11e7be4ce3633e37ef64aa320a3fcf3a1615066789558aa331bd18b5e61c54651d2e2ecbb6fd0fc681e9e6fa777934
|
data/README.md
CHANGED
@@ -78,3 +78,21 @@ Now you can call the [helper methods](https://github.com/HubRise/ruby-client/blo
|
|
78
78
|
```ruby
|
79
79
|
client.get_account
|
80
80
|
```
|
81
|
+
|
82
|
+
## Pagination
|
83
|
+
|
84
|
+
`#next_page?`, `#next_page` and `#each_page` might be helpful when working with paginated [endpoints](https://www.hubrise.com/developers/api/general-concepts/#pagination)
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
response = client.get_all_customers
|
88
|
+
|
89
|
+
response.next_page?
|
90
|
+
# => true
|
91
|
+
|
92
|
+
response.next_page.data
|
93
|
+
# => [{ first_name:... }]
|
94
|
+
|
95
|
+
response.each_page do |page_response|
|
96
|
+
page_response.data
|
97
|
+
# => [{ first_name:... }]
|
98
|
+
end
|
data/lib/hubrise_client/base.rb
CHANGED
@@ -18,13 +18,13 @@ module HubriseClient
|
|
18
18
|
:request_callback
|
19
19
|
|
20
20
|
def initialize(app_id, app_secret, params = {})
|
21
|
-
@app_id
|
21
|
+
@app_id = app_id
|
22
22
|
@app_secret = app_secret
|
23
|
-
@api_host
|
24
|
-
@api_port
|
23
|
+
@api_host = params[:api_host] || DEFAULT_API_HOST
|
24
|
+
@api_port = params[:api_port] || DEFAULT_API_PORT
|
25
25
|
@oauth_host = params[:oauth_host] || DEFAULT_OAUTH_HOST
|
26
26
|
@oauth_port = params[:oauth_port] || DEFAULT_OAUTH_PORT
|
27
|
-
@use_https
|
27
|
+
@use_https = !!params.fetch(:use_https, USE_HTTPS)
|
28
28
|
@request_callback = params[:request_callback]
|
29
29
|
|
30
30
|
initialize_scope_params(params)
|
@@ -47,10 +47,13 @@ module HubriseClient
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def authorize!(authorization_code)
|
50
|
-
api_response = api_request(
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
api_response = api_request(
|
51
|
+
oauth2_hubrise_hostname_with_version, "/token", :post, data: {
|
52
|
+
client_id: @app_id,
|
53
|
+
client_secret: @app_secret,
|
54
|
+
code: authorization_code,
|
55
|
+
}
|
56
|
+
).perform
|
54
57
|
|
55
58
|
case api_response.code
|
56
59
|
when "200"
|
@@ -67,33 +70,37 @@ module HubriseClient
|
|
67
70
|
protected
|
68
71
|
|
69
72
|
def initialize_scope_params(params)
|
70
|
-
@access_token
|
71
|
-
@app_instance_id
|
72
|
-
@user_id
|
73
|
-
@account_id
|
74
|
-
@location_id
|
75
|
-
@catalog_id
|
73
|
+
@access_token = params[:access_token] || params["access_token"]
|
74
|
+
@app_instance_id = params[:app_instance_id] || params["app_instance_id"]
|
75
|
+
@user_id = params[:user_id] || params["user_id"]
|
76
|
+
@account_id = params[:account_id] || params["account_id"]
|
77
|
+
@location_id = params[:location_id] || params["location_id"]
|
78
|
+
@catalog_id = params[:catalog_id] || params["catalog_id"]
|
76
79
|
@customer_list_id = params[:customer_list_id] || params["customer_list_id"]
|
77
80
|
end
|
78
81
|
|
79
82
|
def call_api(path, method = :get, data: {}, headers: {}, json: true)
|
80
83
|
raise(HubriseAccessTokenMissing) if @access_token.nil?
|
81
84
|
|
82
|
-
api_request(
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
api_request(
|
86
|
+
"#{@api_host}:#{@api_port}/#{version}", path, method, data: data,
|
87
|
+
json: json,
|
88
|
+
headers: headers,
|
89
|
+
access_token: @access_token,
|
90
|
+
callback: @request_callback
|
91
|
+
).perform
|
89
92
|
end
|
90
93
|
|
91
|
-
def api_request(hostname,
|
92
|
-
Request.
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
94
|
+
def api_request(hostname, path, method, attrs = {})
|
95
|
+
Request.from_h(
|
96
|
+
{
|
97
|
+
hostname: hostname,
|
98
|
+
path: path,
|
99
|
+
method: method,
|
100
|
+
use_https: @use_https,
|
101
|
+
logger: @verbous && @logger,
|
102
|
+
json: true,
|
103
|
+
}.merge(attrs)
|
97
104
|
)
|
98
105
|
end
|
99
106
|
|
@@ -1,6 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module HubriseClient
|
3
|
-
class Request
|
3
|
+
class Request < Struct.new(:hostname,
|
4
|
+
:path,
|
5
|
+
:method,
|
6
|
+
:data,
|
7
|
+
:access_token,
|
8
|
+
:use_https,
|
9
|
+
:logger,
|
10
|
+
:json,
|
11
|
+
:headers,
|
12
|
+
:callback)
|
13
|
+
|
4
14
|
REQUESTS_HASH = {
|
5
15
|
get: Net::HTTP::Get,
|
6
16
|
post: Net::HTTP::Post,
|
@@ -9,20 +19,17 @@ module HubriseClient
|
|
9
19
|
delete: Net::HTTP::Delete,
|
10
20
|
}.freeze
|
11
21
|
|
12
|
-
|
13
|
-
|
14
|
-
@hostname = hostname
|
15
|
-
@access_token = access_token
|
16
|
-
@use_https = use_https
|
17
|
-
@protocol = use_https ? "https" : "http"
|
18
|
-
@logger = logger
|
22
|
+
def self.from_h(hash)
|
23
|
+
new(*hash.values_at(*members))
|
19
24
|
end
|
20
25
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@
|
25
|
-
|
26
|
+
attr_reader :http_request
|
27
|
+
|
28
|
+
def perform
|
29
|
+
@http_request = build_request
|
30
|
+
|
31
|
+
@http_response = perform_request(@http_request)
|
32
|
+
@response = Response.new(@http_response, self)
|
26
33
|
|
27
34
|
case @http_response
|
28
35
|
when Net::HTTPUnauthorized
|
@@ -35,25 +42,41 @@ module HubriseClient
|
|
35
42
|
rescue Errno::ECONNREFUSED
|
36
43
|
raise HubriseError, "API is not reachable"
|
37
44
|
ensure
|
38
|
-
|
45
|
+
callback.call(self, @response) if @http_request && callback
|
46
|
+
end
|
47
|
+
|
48
|
+
def next_page_request(new_cursor)
|
49
|
+
new_data = data || {}
|
50
|
+
new_data[:cursor] = new_cursor
|
51
|
+
|
52
|
+
Request.from_h(to_h.merge(data: new_data))
|
39
53
|
end
|
40
54
|
|
41
55
|
protected
|
42
56
|
|
43
|
-
def
|
44
|
-
|
57
|
+
def protocol
|
58
|
+
use_https ? "https" : "http"
|
59
|
+
end
|
45
60
|
|
46
|
-
|
47
|
-
|
61
|
+
def build_request
|
62
|
+
request_uri = URI.parse(protocol + "://" + hostname + path)
|
63
|
+
|
64
|
+
request_headers = headers || {}
|
65
|
+
request_headers["X-Access-Token"] = access_token if access_token
|
66
|
+
|
67
|
+
request_body_data = nil
|
48
68
|
|
49
|
-
|
69
|
+
if method == :get
|
70
|
+
request_uri = add_params_to_uri(request_uri, data) if data && !data.empty?
|
50
71
|
elsif json
|
51
|
-
|
52
|
-
|
72
|
+
request_headers["Content-Type"] ||= "application/json"
|
73
|
+
request_body_data = data.to_json
|
74
|
+
else
|
75
|
+
request_body_data = data
|
53
76
|
end
|
54
77
|
|
55
|
-
REQUESTS_HASH[method].new(
|
56
|
-
request.body =
|
78
|
+
REQUESTS_HASH[method].new(request_uri, request_headers).tap do |request|
|
79
|
+
request.body = request_body_data
|
57
80
|
end
|
58
81
|
end
|
59
82
|
|
@@ -62,10 +85,12 @@ module HubriseClient
|
|
62
85
|
uri
|
63
86
|
end
|
64
87
|
|
65
|
-
def perform_request(
|
66
|
-
|
67
|
-
|
68
|
-
http.
|
88
|
+
def perform_request(request)
|
89
|
+
uri = request.uri
|
90
|
+
|
91
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
92
|
+
http.use_ssl = use_https
|
93
|
+
http.set_debug_output(logger) if logger
|
69
94
|
http.request(request)
|
70
95
|
end
|
71
96
|
end
|
@@ -4,9 +4,10 @@ module HubriseClient
|
|
4
4
|
attr_reader :code, :failed, :data, :error_type, :error_message, :errors, :http_response
|
5
5
|
alias_method :failed?, :failed
|
6
6
|
|
7
|
-
def initialize(http_response)
|
8
|
-
@http_response
|
9
|
-
@
|
7
|
+
def initialize(http_response, request)
|
8
|
+
@http_response = http_response
|
9
|
+
@request = request
|
10
|
+
@code = http_response.code
|
10
11
|
|
11
12
|
json_body = begin
|
12
13
|
JSON.parse(http_response.body)
|
@@ -32,5 +33,29 @@ module HubriseClient
|
|
32
33
|
def retry_after
|
33
34
|
http_response.is_a?(Net::HTTPTooManyRequests) && http_response["retry-after"].to_i
|
34
35
|
end
|
36
|
+
|
37
|
+
def each_page
|
38
|
+
return enum_for(:each_page) unless block_given?
|
39
|
+
|
40
|
+
yield(self)
|
41
|
+
|
42
|
+
response = self
|
43
|
+
while response.next_page?
|
44
|
+
response = response.next_page
|
45
|
+
yield(response)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def next_page?
|
50
|
+
!!cursor_next
|
51
|
+
end
|
52
|
+
|
53
|
+
def next_page
|
54
|
+
@request.next_page_request(cursor_next).perform
|
55
|
+
end
|
56
|
+
|
57
|
+
def cursor_next
|
58
|
+
http_response["X-Cursor-Next"]
|
59
|
+
end
|
35
60
|
end
|
36
61
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hubrise_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Antoine Monnier
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-04-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|