hubrise_client 2.0.3 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- 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/v1.rb +3 -2
- 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: 9ec084acadf8cc23be2422bcf89bbcc26322efff9d33dfa74a2534829e93610e
|
4
|
+
data.tar.gz: 14b9bce9ce0467ac3eadde32f3a74e5490cee27a0a074b06fb72a75a1a5c6779
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e5a24a35da42f212dc95b9fe945d2ef21660dc800611c623ee88063fdb752b86170849594c4e26e8a1a81fa26a6f2cc05b3bb31fd7f8db572c48693d2882086
|
7
|
+
data.tar.gz: e4df31dc16a7889e6144c76dcc731ded45c809c37488f29af48641f8d6819a86c2ae287411149c863c723310bc3d554f9b43d57328a7e22ceefe40817a139ae0
|
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
|
data/lib/hubrise_client/v1.rb
CHANGED
@@ -126,8 +126,9 @@ module HubriseClient
|
|
126
126
|
call_api("/accounts/#{account_id}/catalogs")
|
127
127
|
end
|
128
128
|
|
129
|
-
def get_catalog(catalog_id = nil)
|
130
|
-
|
129
|
+
def get_catalog(catalog_id = nil, hide_data: false)
|
130
|
+
query_params = hide_data ? { hide_data: true } : {}
|
131
|
+
call_api("/catalogs/#{catalog_id_fallback(catalog_id)}", :get, data: query_params)
|
131
132
|
end
|
132
133
|
|
133
134
|
def create_account_catalog(params)
|
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.6
|
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-07-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|