ruby-brightpearl 0.1.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7950d4ca39461b4b4508b6db7ceefca32f3461bbb83f432ea29c80739e57a0cf
4
- data.tar.gz: 8aaf0c72a2196d77f2f4d263b1e60dd078c92f5e20dbc226a70e38c1b2e9f476
3
+ metadata.gz: 898c21eda2183ad39766aed1a0973d4d3501df69c572bedc50cbe55497de8bfc
4
+ data.tar.gz: 6f9bcc5638f5fdce7f58f0d6d0a7f8611d86ff3b5a89dd0bd447b036bdf6a73b
5
5
  SHA512:
6
- metadata.gz: 816871310b3ef7c096cbf4fb3717e09887aea158e236b36b8e324c2233047bd86f047b8cff15db8d78f9e06e15790821b88abd7946942e9418310ba3f18155d0
7
- data.tar.gz: d544a07698537948b18a9b040fcdcc704b26edcb53d8319535e617bb6f7d09a79372f7a68a29cc704feb60c0d001d7a0dcf397b9ae5b4f222e1eeaa34687701d
6
+ metadata.gz: 523f4ca185fff9c6622cbb06e32469eb95f039d878ebe015fe1631926d9f17c54b5285aa5338397a7bcfe6cd35509b2f96536b93617e1640c28d222be91b2085
7
+ data.tar.gz: bd89d6e7a680e41092cb74d3f9be7a47870bf71a5de4017401f8845f25ac835db959d9fd614ae03dd8d9f7580e4de664d3cf1d37a1e5b2cf12240e70d329ce6a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2022-02-03
4
+ - New resource TaxCode
5
+ - Fix minor typos
6
+ - New optional config `debug_mode`
7
+ ## [0.3.0] - 2022-02-03
8
+ - New resource ProductAvailability
9
+ ## [0.2.0] - 2022-02-02
10
+ - Add refresh token mechanic with method `use_refresh_token`
11
+ - Change config values
12
+ - Add refresh_token to allow automatic update of tokens when calling the new method `use_refresh_token`
13
+ - Remove `oauth_redirect_url` in favor of keyword arguments on `oauth_url` and `request_token`
14
+ - Create a temp method for Client to use a token per request
15
+ - Add error handling on auth request_token method and tests for some common errors.
16
+
3
17
  ## [0.1.0] - 2022-01-21
4
18
 
5
19
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-brightpearl (0.1.0)
4
+ ruby-brightpearl (0.4.0)
5
5
  httparty (~> 0.20)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -41,41 +41,55 @@ Using the official portal create a new application, the type must be `instance`,
41
41
 
42
42
  Start by setting up the initial configuration for the client:
43
43
  ```ruby
44
- Brightpearl.config.account = ACCOUNT # From developer account creation
44
+ require 'brightpearl'
45
+
46
+ Brightpearl.config.account = ACCOUNT # Account to request API access
45
47
  Brightpearl.config.dev_ref = DEV_REF # From developer account creation
46
48
 
47
49
  Brightpearl.config.app_ref = APP_REF # Brightpearl app ID
48
50
  Brightpearl.config.app_secret = APP_REF # Brightpearl app secret
49
-
50
- Brightpearl.config.oauth_redirect_url = OAUTH_REDIRECT_URL # Brightpearl app authorized redirect url
51
51
  ```
52
52
 
53
53
  Before the API calls can be performed you will need to get a token, to get one the authentication flow must be followed as per brightpearl documentation: https://help.brightpearl.com/hc/en-us/articles/360032240811-Set-up-the-Authorization-Code-Grant-flow
54
54
 
55
55
  The oauth URL can be then generated with:
56
56
  ```ruby
57
- Brightpearl::Auth.oauth_url("random-passcode") # => "https://oauth.brightpearl.com/authorize/testAccount?response_type=code&client_id=testAppName&redirect_uri=https://2f826695ec8a.ngrok.io/oauth&state=random-passcode
57
+ Brightpearl::Auth.oauth_url(state: "random-passcode", redirect_uri: "https://www.something.io/oauth") # => "https://oauth.brightpearl.com/authorize/testAccount?response_type=code&client_id=testAppName&redirect_uri=https://www.something.io/oauth&state=random-passcode
58
58
  ```
59
- NOTE: The argument on `oauth_url` is the state, this should be a non guessable string that the authorization server will pass back to you on redirection which you should check against to prevent CSRF attacks
59
+ NOTE: The state argument on `oauth_url` method is a string defined by yourself, this should be a non guessable string that the authorization server will pass back to you on redirection which you should check against to prevent CSRF attacks
60
60
 
61
- #### 3) Trading your `auth code` for an access token.
61
+ #### 3) Trading your `code` for an access token.
62
62
 
63
- The oauth process will return an url with a param called `code`, the value of this parameter is a temporary token that the app can exchange for an access token.
63
+ The oauth process will redirect to your `redirect_uri` with a param called `code`, the value of this parameter is a temporary token that the app can exchange for an access token.
64
64
 
65
- This can be done by calling:
65
+ This process be done by:
66
66
 
67
67
  ```ruby
68
- Brightpearl::Auth.request_token(AUTH_TOKEN) # => { token: "XXX", refresh_token: "XYZ", api_domain: "ws-use.brightpearl.com" }
68
+ Brightpearl::Auth.request_token(auth_token: AUTH_TOKEN, redirect_uri: "https://www.something.io/oauth") # => { payload: { "access_token" => "XXX", "refresh_token" => "XYZ", "api_domain" => "ws-use.brightpearl.com" } }
69
69
  ```
70
70
 
71
71
  After the token is obtained it can be added to client by setting it on the config:
72
72
  ```ruby
73
73
  Brightpearl.config.api_domain = API_DOMAIN # Such as ws-use.brightpearl.com
74
74
  Brightpearl.config.token = TOKEN
75
+ Brightpearl.config.refresh_token = REFRESH_TOKEN
76
+ ```
77
+
78
+ NOTES:
79
+ * The token has a expiration time, when the token has expired a new one can be obtained using a refresh token.
80
+ * The redirect_uri used on `request_token` should be the same used on `oauth_url`
81
+
82
+ #### 3A) Using the refresh token to get a new access token
83
+
84
+ When the token has expired, the `use_refresh_token` method can be used:
85
+ ```ruby
86
+ Brightpearl::Auth.use_refresh_token(refresh_token: "XXX")
87
+ # If refresh_token is loaded on config just call the method
88
+ Brightpearl::Auth.use_refresh_token()
75
89
  ```
76
90
 
77
- Token has a expiration time, when the token has expired a new one can be obtained using the refresh token.
78
-
91
+ The return value is the same as `request_token`, additionally by default the new `token` and `refresh_token` are loaded on `Brightpearl.config`, if for some reason this is undesired it can be turned off by calling the method as `Brightpearl::Auth.use_refresh_token(autoupdate: false)`
92
+
79
93
  #### 4) Making requests
80
94
  Responses to REST requests are parsed into a hash with the keys `:payload` with the actual response from brightpearl API and `:quota_remaining` with the value of the current quota.
81
95
 
@@ -1,17 +1,17 @@
1
1
  module Brightpearl
2
2
  class Auth
3
- def self.oauth_url(state)
4
- "https://oauth.brightpearl.com/authorize/#{Brightpearl.config.account}?response_type=code&client_id=#{Brightpearl.config.app_ref}&redirect_uri=#{Brightpearl.config.oauth_redirect_url}&state=#{state}"
3
+ def self.oauth_url(state:, redirect_uri:)
4
+ "https://oauth.brightpearl.com/authorize/#{Brightpearl.config.account}?response_type=code&client_id=#{Brightpearl.config.app_ref}&redirect_uri=#{redirect_uri}&state=#{state}"
5
5
  end
6
6
 
7
- def self.request_token(auth_token)
7
+ def self.request_token(auth_token:, redirect_uri:)
8
8
  token_endpoint = "https://oauth.brightpearl.com/token/#{Brightpearl.config.account}"
9
9
  body = {
10
10
  grant_type: "authorization_code",
11
11
  code: auth_token,
12
12
  client_id: Brightpearl.config.app_ref,
13
13
  client_secret: Brightpearl.config.app_secret,
14
- redirect_uri: Brightpearl.config.oauth_redirect_url
14
+ redirect_uri: redirect_uri, # SAME AS THE ONE USED ON oauth_url
15
15
  }
16
16
 
17
17
  response = HTTParty.post(token_endpoint,
@@ -21,13 +21,48 @@ module Brightpearl
21
21
  'charset' => 'utf-8'
22
22
  }
23
23
  )
24
- data = JSON.parse(response.body)
24
+ json = JSON.parse(response.body)
25
+ raise Brightpearl::RequestError.new(json["error_description"] || json["error"], response: json, status: 400) if response.code == 400
25
26
 
26
27
  return {
27
- token: data["access_token"],
28
- refresh_token: data["refresh_token"],
29
- api_domain: data["api_domain"],
28
+ payload: json,
29
+ data: {
30
+ token: json["access_token"],
31
+ refresh_token: json["refresh_token"],
32
+ api_domain: json["api_domain"],
33
+ }
30
34
  }
31
35
  end
36
+
37
+ def self.use_refresh_token(refresh_token: nil, autoupdate: true)
38
+ token_endpoint = "https://oauth.brightpearl.com/token/#{Brightpearl.config.account}"
39
+ rtoken = refresh_token || Brightpearl.config.refresh_token
40
+ body = {
41
+ grant_type: "refresh_token",
42
+ refresh_token: rtoken,
43
+ client_id: Brightpearl.config.app_ref,
44
+ }
45
+ response = HTTParty.post(token_endpoint,
46
+ body: body,
47
+ headers: { "Content-Type": "application/x-www-form-urlencoded", 'charset' => 'utf-8' }
48
+ )
49
+
50
+ json = JSON.parse(response.body)
51
+ if json["access_token"] && autoupdate
52
+ Brightpearl.config.token = json["access_token"]
53
+ Brightpearl.config.refresh_token = json["refresh_token"]
54
+ end
55
+
56
+ return {
57
+ payload: json,
58
+ data: {
59
+ token: json["access_token"],
60
+ refresh_token: json["refresh_token"],
61
+ api_domain: json["api_domain"]
62
+ }
63
+ }
64
+
65
+ end
66
+
32
67
  end
33
68
  end
@@ -1,6 +1,16 @@
1
1
  module Brightpearl
2
2
  class Client
3
- # attr_accessor :token
3
+ # Send a request using a different token than the global config. (Useful when using a different token for specific calls)
4
+ def self.temp(token:, &block)
5
+ original_token = Brightpearl.config.token
6
+ begin
7
+ Brightpearl.config.token = token
8
+ yield
9
+ ensure
10
+ Brightpearl.config.token = original_token
11
+ end
12
+ end
13
+
4
14
  def self.send_request(path:, method: :get, **options )
5
15
  headers = {
6
16
  "brightpearl-app-ref": "#{Brightpearl.config.app_ref}",
@@ -36,7 +46,7 @@ module Brightpearl
36
46
  puts "Unrecognized http method"
37
47
  end
38
48
 
39
- puts url
49
+ puts url if Brightpearl.config.debug_mode
40
50
  json = JSON.parse(response.body)
41
51
 
42
52
  if response.code == 503 # Unavailable MOST likeyly throttled
@@ -7,11 +7,12 @@ module Brightpearl
7
7
  # Application specific
8
8
  attr_accessor :app_ref # App ref
9
9
  attr_accessor :app_secret # App secret
10
- attr_accessor :oauth_redirect_url # App authorized oauth redirect for app
11
10
 
12
11
  # Based on authentication process
13
12
  attr_accessor :token # API token
14
13
  attr_accessor :api_domain # API URL base. Depends on instance region. Obtained after auth process
15
- # attr_accessor :refresh_token
14
+ attr_accessor :refresh_token # API refresh token
15
+
16
+ attr_accessor :debug_mode # Display log data for request done when true
16
17
  end
17
18
  end
@@ -9,8 +9,6 @@ module Brightpearl
9
9
  else
10
10
  super(msg)
11
11
  end
12
- puts @code
13
- puts error
14
12
  @response = response
15
13
  @status = status
16
14
  end
@@ -0,0 +1,13 @@
1
+ module Brightpearl
2
+ class ProductAvailability < Resource
3
+ extend Brightpearl::APIOperations::Get
4
+ class << self
5
+ def resource_path
6
+ "warehouse-service/product-availability"
7
+ end
8
+
9
+ # https://api-docs.brightpearl.com/warehouse/product-availability/get.html
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module Brightpearl
2
+ class TaxCode < Resource
3
+ extend Brightpearl::APIOperations::Get
4
+ class << self
5
+ def resource_path
6
+ "accounting-service/tax-code"
7
+ end
8
+
9
+ def get(id_set = nil)
10
+ if id_set
11
+ super
12
+ else
13
+ super(nil)
14
+ end
15
+ end
16
+
17
+ # https://api-docs.brightpearl.com/accounting/tax-code/get.html
18
+
19
+ end
20
+ end
21
+ end
@@ -6,4 +6,7 @@ require 'brightpearl/resources/order_status'
6
6
  require 'brightpearl/resources/order_status_update'
7
7
  require 'brightpearl/resources/product'
8
8
  require 'brightpearl/resources/product_price'
9
- require 'brightpearl/resources/price_list'
9
+ require 'brightpearl/resources/price_list'
10
+ require 'brightpearl/resources/product_availability'
11
+ # Accounting
12
+ require 'brightpearl/resources/tax_code'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Brightpearl
4
- VERSION = "0.1.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
18
 
19
19
  spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/vicvans20/ruby-brightpearl."
20
+ spec.metadata["source_code_uri"] = "https://github.com/vicvans20/ruby-brightpearl"
21
21
  spec.metadata["changelog_uri"] = "https://github.com/vicvans20/ruby-brightpearl/blob/master/CHANGELOG.md"
22
22
 
23
23
  # Specify which files should be added to the gem when it is released.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-brightpearl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vicvans20
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-29 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -103,7 +103,9 @@ files:
103
103
  - lib/brightpearl/resources/order_status_update.rb
104
104
  - lib/brightpearl/resources/price_list.rb
105
105
  - lib/brightpearl/resources/product.rb
106
+ - lib/brightpearl/resources/product_availability.rb
106
107
  - lib/brightpearl/resources/product_price.rb
108
+ - lib/brightpearl/resources/tax_code.rb
107
109
  - lib/brightpearl/version.rb
108
110
  - playground.rb
109
111
  - ruby-brightpearl.gemspec
@@ -114,7 +116,7 @@ licenses:
114
116
  metadata:
115
117
  allowed_push_host: https://rubygems.org
116
118
  homepage_uri: https://github.com/vicvans20/ruby-brightpearl
117
- source_code_uri: https://github.com/vicvans20/ruby-brightpearl.
119
+ source_code_uri: https://github.com/vicvans20/ruby-brightpearl
118
120
  changelog_uri: https://github.com/vicvans20/ruby-brightpearl/blob/master/CHANGELOG.md
119
121
  post_install_message:
120
122
  rdoc_options: []