ruby-brightpearl 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7950d4ca39461b4b4508b6db7ceefca32f3461bbb83f432ea29c80739e57a0cf
4
+ data.tar.gz: 8aaf0c72a2196d77f2f4d263b1e60dd078c92f5e20dbc226a70e38c1b2e9f476
5
+ SHA512:
6
+ metadata.gz: 816871310b3ef7c096cbf4fb3717e09887aea158e236b36b8e324c2233047bd86f047b8cff15db8d78f9e06e15790821b88abd7946942e9418310ba3f18155d0
7
+ data.tar.gz: d544a07698537948b18a9b040fcdcc704b26edcb53d8319535e617bb6f7d09a79372f7a68a29cc704feb60c0d001d7a0dcf397b9ae5b4f222e1eeaa34687701d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-01-21
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at vicvans20@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ruby-brightpearl.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-brightpearl (0.1.0)
5
+ httparty (~> 0.20)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ byebug (11.1.3)
13
+ crack (0.4.5)
14
+ rexml
15
+ diff-lcs (1.5.0)
16
+ hashdiff (1.0.1)
17
+ httparty (0.20.0)
18
+ mime-types (~> 3.0)
19
+ multi_xml (>= 0.5.2)
20
+ mime-types (3.4.1)
21
+ mime-types-data (~> 3.2015)
22
+ mime-types-data (3.2022.0105)
23
+ multi_xml (0.6.0)
24
+ public_suffix (4.0.6)
25
+ rake (13.0.6)
26
+ rexml (3.2.5)
27
+ rspec (3.10.0)
28
+ rspec-core (~> 3.10.0)
29
+ rspec-expectations (~> 3.10.0)
30
+ rspec-mocks (~> 3.10.0)
31
+ rspec-core (3.10.1)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-expectations (3.10.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-mocks (3.10.2)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-support (3.10.3)
40
+ vcr (6.0.0)
41
+ webmock (3.14.0)
42
+ addressable (>= 2.8.0)
43
+ crack (>= 0.3.2)
44
+ hashdiff (>= 0.4.0, < 2.0.0)
45
+
46
+ PLATFORMS
47
+ x86_64-linux
48
+
49
+ DEPENDENCIES
50
+ byebug
51
+ rake (~> 13.0)
52
+ rspec (~> 3.0)
53
+ ruby-brightpearl!
54
+ vcr (~> 6.0)
55
+ webmock
56
+
57
+ BUNDLED WITH
58
+ 2.3.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 vicvans20
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # Brightpearl
2
+
3
+ The Brightpearl gem allows Ruby developers to access the Brightpearl API resources for instance confidential apps.
4
+
5
+ The Brightpearl API is implemented as a REST API JSON interface, using various HTTP verbs such as: GET/POST/PUT/PATCH/OPTIONS/DELETE. Each resource, like Order, Product, or Contact, has different operations available.
6
+
7
+ To read more, follow the link to the official documentation: https://api-docs.brightpearl.com/
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ruby-brightpearl'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ruby-brightpearl
23
+
24
+ ## Usage
25
+
26
+ ### Requirements
27
+
28
+ All API usage happens through brightpearl applications, created by developers for private usage or for use by other brightpearl site owners:
29
+
30
+ A brightpearl developer account is required to begin the development of apps, to read more info about the account creation refer to: https://help.brightpearl.com/hc/en-us/articles/211124886-Register-as-a-Brightpearl-developer
31
+
32
+ Once an account is provided, developers can create applications through the official portal of brightpearl applications: https://developer.brightpearl.com/
33
+
34
+ ### Usage
35
+
36
+ #### 1) Create an app
37
+
38
+ Using the official portal create a new application, the type must be `instance`, once the app has been created an "application ID" and "secret" should be provided.
39
+
40
+ #### 2) Requesting access for a brightpearl account
41
+
42
+ Start by setting up the initial configuration for the client:
43
+ ```ruby
44
+ Brightpearl.config.account = ACCOUNT # From developer account creation
45
+ Brightpearl.config.dev_ref = DEV_REF # From developer account creation
46
+
47
+ Brightpearl.config.app_ref = APP_REF # Brightpearl app ID
48
+ 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
+ ```
52
+
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
+
55
+ The oauth URL can be then generated with:
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
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
60
+
61
+ #### 3) Trading your `auth code` for an access token.
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.
64
+
65
+ This can be done by calling:
66
+
67
+ ```ruby
68
+ Brightpearl::Auth.request_token(AUTH_TOKEN) # => { token: "XXX", refresh_token: "XYZ", api_domain: "ws-use.brightpearl.com" }
69
+ ```
70
+
71
+ After the token is obtained it can be added to client by setting it on the config:
72
+ ```ruby
73
+ Brightpearl.config.api_domain = API_DOMAIN # Such as ws-use.brightpearl.com
74
+ Brightpearl.config.token = TOKEN
75
+ ```
76
+
77
+ Token has a expiration time, when the token has expired a new one can be obtained using the refresh token.
78
+
79
+ #### 4) Making requests
80
+ 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
+
82
+ NOTE: Search operations returns a Ruby object for the resource as well for easier access to the attributes, this is returned in the `:records` key on the result.
83
+
84
+ ```ruby
85
+ result = Brightpearl::Order.get(1)
86
+
87
+ order = result[:payload]["response"].first
88
+ puts order["id"] # => 1
89
+ puts order["orderTypeCode"] # => "SO"
90
+ ```
91
+
92
+ Brightpearl API allows for multiple items to be requested at once on most resources:
93
+ ```ruby
94
+ product_ids = [1, 2, 3]
95
+ result = Brightpearl::Product.get(product_ids.join(","))
96
+ puts result[:payload]["response"].size # => 3
97
+ ```
98
+
99
+ NOTE: For more information about brightpearl API syntax refer to: https://help.brightpearl.com/hc/en-us/articles/212645003-URI-syntax
100
+
101
+
102
+ ## Development
103
+
104
+ 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.
105
+
106
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby-brightpearl. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ruby-brightpearl/blob/master/CODE_OF_CONDUCT.md).
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
115
+
116
+ ## Code of Conduct
117
+
118
+ Everyone interacting in the Brightpearl project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ruby-brightpearl/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "brightpearl"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ module Brightpearl
2
+ module APIOperations
3
+ module Get
4
+ def get(id_set, query_params = nil)
5
+ path = "#{resource_path}/#{id_set}"
6
+ path = "#{path}?#{to_query(query_params)}" if query_params
7
+ send_request(path: path, method: :get)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Brightpearl
2
+ module APIOperations
3
+ module Options
4
+ def options(id_set)
5
+ send_request(path: "#{resource_path}/#{id_set}", method: :options)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Brightpearl
2
+ module APIOperations
3
+ module Patch
4
+ def patch(id, params)
5
+ send_request(path: "#{resource_path}/#{id}", method: :patch, body: params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Brightpearl
2
+ module APIOperations
3
+ module Post
4
+ def post(params)
5
+ send_request(path: "#{resource_path}", method: :post, body: params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Brightpearl
2
+ module APIOperations
3
+ module Put
4
+ def put(id, params)
5
+ send_request(path: "#{resource_path}/#{id}", method: :put, body: params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'brightpearl/api_operations/get'
2
+ require 'brightpearl/api_operations/post'
3
+ require 'brightpearl/api_operations/patch'
4
+ require 'brightpearl/api_operations/put'
5
+ require 'brightpearl/api_operations/options'
@@ -0,0 +1,33 @@
1
+ module Brightpearl
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}"
5
+ end
6
+
7
+ def self.request_token(auth_token)
8
+ token_endpoint = "https://oauth.brightpearl.com/token/#{Brightpearl.config.account}"
9
+ body = {
10
+ grant_type: "authorization_code",
11
+ code: auth_token,
12
+ client_id: Brightpearl.config.app_ref,
13
+ client_secret: Brightpearl.config.app_secret,
14
+ redirect_uri: Brightpearl.config.oauth_redirect_url
15
+ }
16
+
17
+ response = HTTParty.post(token_endpoint,
18
+ body: body,
19
+ headers: {
20
+ 'Content-Type' => 'application/x-www-form-urlencoded',
21
+ 'charset' => 'utf-8'
22
+ }
23
+ )
24
+ data = JSON.parse(response.body)
25
+
26
+ return {
27
+ token: data["access_token"],
28
+ refresh_token: data["refresh_token"],
29
+ api_domain: data["api_domain"],
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,62 @@
1
+ module Brightpearl
2
+ class Client
3
+ # attr_accessor :token
4
+ def self.send_request(path:, method: :get, **options )
5
+ headers = {
6
+ "brightpearl-app-ref": "#{Brightpearl.config.app_ref}",
7
+ "brightpearl-dev-ref": "#{Brightpearl.config.dev_ref}",
8
+ "Authorization": "Bearer #{Brightpearl.config.token}"
9
+ }
10
+
11
+ url = "#{base_url}/#{path}"
12
+
13
+ case method
14
+ when :get
15
+ response = HTTParty.get(url, headers: headers )
16
+ when :delete
17
+ response = HTTParty.delete(url, headers: headers)
18
+ when :patch
19
+ response = HTTParty.patch(url,
20
+ headers: headers.merge({ "Content-Type": "application/json" }),
21
+ body: options[:body].to_json,
22
+ )
23
+ when :put
24
+ response = HTTParty.put(url,
25
+ headers: headers.merge({ "Content-Type": "application/json" }),
26
+ body: options[:body].to_json,
27
+ )
28
+ when :post
29
+ response = HTTParty.post(url,
30
+ headers: headers.merge({ "Content-Type": "application/json" }),
31
+ body: options[:body].to_json,
32
+ )
33
+ when :options
34
+ response = HTTParty.options(url, headers: headers )
35
+ else
36
+ puts "Unrecognized http method"
37
+ end
38
+
39
+ puts url
40
+ json = JSON.parse(response.body)
41
+
42
+ if response.code == 503 # Unavailable MOST likeyly throttled
43
+ raise Brightpearl::Throttled.new("Throttled", response: json, status: response.code)
44
+ elsif response.code == 401
45
+ raise Brightpearl::InvalidToken.new(json["response"], response: json, status: 401)
46
+ elsif !!json["errors"]
47
+ raise Brightpearl::RequestError.new("Request Error", response: json, status: response.code)
48
+ end
49
+
50
+ return {
51
+ payload: json,
52
+ quota_remaining: response.headers["brightpearl-requests-remaining"]
53
+ }
54
+
55
+ end
56
+
57
+ def self.base_url
58
+ "https://#{Brightpearl.config.api_domain}/public-api/#{Brightpearl.config.account}";
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,17 @@
1
+ module Brightpearl
2
+ class Config
3
+ # Developer Account
4
+ attr_accessor :dev_ref # Developer ID
5
+ attr_accessor :account # Brightpearl account
6
+
7
+ # Application specific
8
+ attr_accessor :app_ref # App ref
9
+ attr_accessor :app_secret # App secret
10
+ attr_accessor :oauth_redirect_url # App authorized oauth redirect for app
11
+
12
+ # Based on authentication process
13
+ attr_accessor :token # API token
14
+ attr_accessor :api_domain # API URL base. Depends on instance region. Obtained after auth process
15
+ # attr_accessor :refresh_token
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ module Brightpearl
2
+ class RequestError < StandardError
3
+ attr_reader :response, :code, :status
4
+ def initialize(msg, response: nil, status: nil)
5
+ if response["errors"] && response["errors"].size == 1 # If error is easily identifiable then set it as the Error message
6
+ error = response["errors"][0]["message"]
7
+ @code = response["errors"][0]["code"]
8
+ super(error)
9
+ else
10
+ super(msg)
11
+ end
12
+ puts @code
13
+ puts error
14
+ @response = response
15
+ @status = status
16
+ end
17
+ end
18
+
19
+ class Throttled < StandardError
20
+ attr_reader :response, :code, :status
21
+ def initialize(msg, response: nil, status: nil)
22
+ super(msg)
23
+ @response = response
24
+ @status = status
25
+ end
26
+ end
27
+
28
+ class InvalidToken < StandardError
29
+ attr_reader :response, :status
30
+ def initialize(msg, response: nil, status: nil)
31
+ super(msg)
32
+ @response = response
33
+ @status = status
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module Brightpearl
2
+ class Resource
3
+
4
+ def self.send_request(path:, method: :get, **options)
5
+ Brightpearl::Client.send_request(path: path, method: method, **options)
6
+ end
7
+
8
+ def self.to_query(query_params)
9
+ URI.encode_www_form(query_params)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,72 @@
1
+ module Brightpearl
2
+ class Order < Resource
3
+ extend Brightpearl::APIOperations::Get
4
+ extend Brightpearl::APIOperations::Post
5
+ extend Brightpearl::APIOperations::Patch
6
+ extend Brightpearl::APIOperations::Options
7
+
8
+ attr_accessor :id, :order_type_id, :contact_id, :order_status_id, :order_stock_status_id,
9
+ :created_on, :created_by_id,
10
+ :customer_ref, :order_payment_status_id, :updated_on,
11
+ :parent_order_id, :order_shipping_status_id, :external_ref,
12
+ :installed_integration_instance_id, :warehouse_id, :staff_owner_contact_id,
13
+ :tax_date, :department_id, :delivery_date
14
+
15
+ class << self
16
+ def resource_path
17
+ "order-service/order"
18
+ end
19
+
20
+ # https://api-docs.brightpearl.com/order/order/get.html
21
+ # https://api-docs.brightpearl.com/order/order/post.html
22
+ # https://api-docs.brightpearl.com/order/order/patch.html
23
+ # https://api-docs.brightpearl.com/order/order/options.html
24
+
25
+
26
+ # https://api-docs.brightpearl.com/order/order/search.html
27
+ def search(query_params = {})
28
+ response = send_request(path: "order-service/order-search?#{to_query(query_params)}", method: :get)
29
+ return response.merge({ # modify final payload to set search results as objects
30
+ records: response[:payload]["response"]["results"].map { |item| Order.new(item) },
31
+ })
32
+ end
33
+ end
34
+
35
+ # DSL
36
+ # ARA => API Record Array(from search and other endpoints)
37
+ def initialize(ara, as_response: false)
38
+ unless as_response
39
+ @id = ara[0];
40
+ @order_type_id = ara[1]; @contact_id = ara[2]; @order_status_id = ara[3]; @order_stock_status_id = ara[4];
41
+ @created_on = ara[5]; @created_by_id = ara[6];
42
+ @customer_ref = ara[7]; @order_payment_status_id = ara[8]; @updated_on = ara[9];
43
+ @parent_order_id = ara[10]; @order_shipping_status_id = ara[11]; @external_ref = ara[12];
44
+ @installed_integration_instance_id = ara[13]; @warehouse_id = ara[14]; @staff_owner_contact_id = ara[15];
45
+ @tax_date = ara[16];
46
+ @department_id = ara[17];
47
+ @delivery_date = ara[18];
48
+ else # Order response from get_order
49
+ @id = ara["id"]
50
+ @order_type_id = nil
51
+ @contact_id = ara["parties"]["customer"]["contactId"]
52
+ @order_status_id = ara["orderStatus"]["orderStatusId"]
53
+ @order_stock_status_id = nil
54
+ @created_on = ara["createdOn"]
55
+ @created_by_id = ara["createdById"]
56
+ @customer_ref = nil
57
+ @order_payment_status_id = nil
58
+ @updated_on = ara["updatedOn"]
59
+ @parent_order_id = ara["parentOrderId"]
60
+ @order_shipping_status_id = nil
61
+ @external_ref = ara["reference"]
62
+ @installed_integration_instance_id = nil
63
+ @warehouse_id = ara["warehousrId"]
64
+ @staff_owner_contact_id = ara["assignment"]["current"]["staffOwnerContactId"]
65
+ @tax_date = ara["invoices"][0] && ara["invoices"][0]["taxDate"]
66
+ @department_id = nil
67
+ @delivery_date = nil
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,21 @@
1
+ module Brightpearl
2
+ class OrderRow < Resource
3
+ class << self
4
+ # https://api-docs.brightpearl.com/order/order-row/post.html
5
+ def post(order_id, params)
6
+ send_request(path: "order-service/order/#{order_id}/row", method: :post, body: params)
7
+ end
8
+
9
+ # https://api-docs.brightpearl.com/order/order-row/patch.html
10
+ def patch(order_id, id, params)
11
+ send_request(path: "order-service/order/#{order_id}/row/#{id}", method: :patch, body: params)
12
+ end
13
+
14
+ # https://api-docs.brightpearl.com/order/order-row/patch.html
15
+ def delete(order_id, id)
16
+ send_request(path: "order-service/order/#{order_id}/row/#{id}", method: :delete)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module Brightpearl
2
+ class OrderShippingStatus < Resource
3
+
4
+ class << self
5
+ # https://api-docs.brightpearl.com/order/order-shipping-status/get.html
6
+ def get
7
+ send_request(path: "order-service/order-shipping-status", method: :get)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Brightpearl
2
+ class OrderStatus < Resource
3
+ extend Brightpearl::APIOperations::Get
4
+ extend Brightpearl::APIOperations::Post
5
+ class << self
6
+ def resource_path
7
+ "order-service/order-status"
8
+ end
9
+
10
+ # https://api-docs.brightpearl.com/order/order-status/get.html
11
+ # https://api-docs.brightpearl.com/order/order-status/post.html
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module Brightpearl
2
+ class OrderStatusUpdate < Resource
3
+
4
+ class << self
5
+ # https://api-docs.brightpearl.com/order/order-status-update/put.html
6
+ def put(id, params)
7
+ send_request(path: "order-service/order/#{id}/status", method: :put, body: params)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Brightpearl
2
+ class PriceList < Resource
3
+ extend Brightpearl::APIOperations::Get
4
+ class << self
5
+ def resource_path
6
+ "product-service/price-list"
7
+ end
8
+
9
+ # https://api-docs.brightpearl.com/product/price-list/get.html
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,49 @@
1
+ module Brightpearl
2
+ class Product < Resource
3
+ extend Brightpearl::APIOperations::Get
4
+ extend Brightpearl::APIOperations::Post
5
+ extend Brightpearl::APIOperations::Put
6
+ extend Brightpearl::APIOperations::Options
7
+
8
+ attr_accessor :id, :product_name, :sku, :barcode,
9
+ :ean, :upc, :isbn, :mpn,
10
+ :stock_tracked, :sales_channel_name, :created_on, :updated_on,
11
+ :bright_pearl_category_code, :product_group_id, :brand_id,
12
+ :product_type_id, :product_status, :primary_supplier_id
13
+
14
+ class << self
15
+ def resource_path
16
+ "product-service/product"
17
+ end
18
+
19
+ # https://api-docs.brightpearl.com/product/product/get.html
20
+ # https://api-docs.brightpearl.com/product/product/post.html
21
+ # https://api-docs.brightpearl.com/product/product/put.html
22
+ # https://api-docs.brightpearl.com/product/product/options.html
23
+
24
+ # https://api-docs.brightpearl.com/product/product/search.html
25
+ def search(query_params = {})
26
+ response = send_request(path: "product-service/product-search?#{to_query(query_params)}", method: :get)
27
+ return response.merge({ # modify final payload to set search results as objects
28
+ records: response[:payload]["response"]["results"].map { |item| Product.new(item) },
29
+ })
30
+ end
31
+ end
32
+
33
+ # DSL
34
+ # ARA => API Record Array
35
+ def initialize(ara)
36
+ @id = ara[0];
37
+ @product_name = ara[1]; @sku = ara[2]; @barcode = ara[3];
38
+ @ean = ara[4]; @upc = ara[5]; @isbn = ara[6]; @mpn = ara[7];
39
+ @stock_tracked = ara[8]; @sales_channel_name = ara[9];
40
+ @created_on = ara[10]; @updated_on = ara[11];
41
+ @bright_pearl_category_code = ara[12]; @product_group_id = ara[13];
42
+ @brand_id = ara[14];
43
+ @product_type_id = ara[15];
44
+ @product_status = ara[16];
45
+ @primary_supplier_id = ara[17];
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,25 @@
1
+ module Brightpearl
2
+ class ProductPrice < Resource
3
+ class << self
4
+ # https://api-docs.brightpearl.com/product/product-price/get.html
5
+ def get(product_idset, price_list_idset = nil)
6
+ path = "product-service/product-price/#{product_idset}"
7
+ path = "#{path}/price-list/#{price_list_id}" if price_list_idset
8
+ send_request(path: path, method: :get)
9
+ end
10
+
11
+ # https://api-docs.brightpearl.com/product/product-price/put.html
12
+ def put(product_id, params)
13
+ send_request(path: "product-service/product-price/#{product_id}/price-list", method: :put, body: params)
14
+ end
15
+
16
+ # https://api-docs.brightpearl.com/product/product-price/options.html
17
+ def options(product_idset, price_list_idset = nil)
18
+ path = "product-service/product-price/#{product_idset}"
19
+ path = "#{path}/price-list/#{price_list_id}" if price_list_idset
20
+ send_request(path: path, method: :options)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ require 'brightpearl/resource'
2
+ require 'brightpearl/resources/order'
3
+ require 'brightpearl/resources/order_row'
4
+ require 'brightpearl/resources/order_shipping_status'
5
+ require 'brightpearl/resources/order_status'
6
+ require 'brightpearl/resources/order_status_update'
7
+ require 'brightpearl/resources/product'
8
+ require 'brightpearl/resources/product_price'
9
+ require 'brightpearl/resources/price_list'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brightpearl
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require 'httparty'
3
+ require 'uri'
4
+
5
+ require "brightpearl/version"
6
+ require 'brightpearl/config'
7
+ require 'brightpearl/errors'
8
+ require 'brightpearl/client'
9
+ require 'brightpearl/auth'
10
+
11
+ require 'brightpearl/api_operations'
12
+ require 'brightpearl/resources'
13
+
14
+ module Brightpearl
15
+ class Error < StandardError; end
16
+ # Your code goes here...
17
+
18
+ def self.config
19
+ @config ||= Config.new
20
+ end
21
+
22
+ end
data/playground.rb ADDED
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
2
+
3
+ require 'pry'
4
+ require_relative './lib/brightpearl.rb'
5
+
6
+ Brightpearl.config.api_domain = ENV["RUBY_BRIGHTPEARL_ENDPOINT"]
7
+ Brightpearl.config.app_ref = ENV["RUBY_BRIGHTPEARL_APP_REF"]
8
+ Brightpearl.config.dev_ref = ENV["RUBY_BRIGHTPEARL_DEV_REF"]
9
+ Brightpearl.config.account = ENV["RUBY_BRIGHTPEARL_ACCOUNT"]
10
+ Brightpearl.config.token = ENV["RUBY_BRIGHTPEARL_TOKEN"] # It expires
11
+
12
+ puts "hello"
13
+
14
+ puts Brightpearl::Order.get(1)
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/brightpearl/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruby-brightpearl"
7
+ spec.version = Brightpearl::VERSION
8
+ spec.authors = ["vicvans20"]
9
+ spec.email = ["vicvans20@gmail.com"]
10
+
11
+ spec.summary = "Brightpearl API ruby client."
12
+ spec.description = "Simple Ruby client to interact with brightpearl API."
13
+ spec.homepage = "https://github.com/vicvans20/ruby-brightpearl"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/vicvans20/ruby-brightpearl."
21
+ spec.metadata["changelog_uri"] = "https://github.com/vicvans20/ruby-brightpearl/blob/master/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency 'httparty', '~> 0.20'
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+
38
+ spec.add_development_dependency("vcr", "~> 6.0")
39
+ spec.add_development_dependency("webmock")
40
+ spec.add_development_dependency("byebug")
41
+
42
+ # For more information and examples about making a new gem, check out our
43
+ # guide at: https://bundler.io/guides/creating_gem.html
44
+ end
@@ -0,0 +1,4 @@
1
+ module Brightpearl
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-brightpearl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vicvans20
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.20'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.20'
27
+ - !ruby/object:Gem::Dependency
28
+ name: vcr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple Ruby client to interact with brightpearl API.
70
+ email:
71
+ - vicvans20@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - CHANGELOG.md
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/brightpearl.rb
87
+ - lib/brightpearl/api_operations.rb
88
+ - lib/brightpearl/api_operations/get.rb
89
+ - lib/brightpearl/api_operations/options.rb
90
+ - lib/brightpearl/api_operations/patch.rb
91
+ - lib/brightpearl/api_operations/post.rb
92
+ - lib/brightpearl/api_operations/put.rb
93
+ - lib/brightpearl/auth.rb
94
+ - lib/brightpearl/client.rb
95
+ - lib/brightpearl/config.rb
96
+ - lib/brightpearl/errors.rb
97
+ - lib/brightpearl/resource.rb
98
+ - lib/brightpearl/resources.rb
99
+ - lib/brightpearl/resources/order.rb
100
+ - lib/brightpearl/resources/order_row.rb
101
+ - lib/brightpearl/resources/order_shipping_status.rb
102
+ - lib/brightpearl/resources/order_status.rb
103
+ - lib/brightpearl/resources/order_status_update.rb
104
+ - lib/brightpearl/resources/price_list.rb
105
+ - lib/brightpearl/resources/product.rb
106
+ - lib/brightpearl/resources/product_price.rb
107
+ - lib/brightpearl/version.rb
108
+ - playground.rb
109
+ - ruby-brightpearl.gemspec
110
+ - sig/ruby/brightpearl.rbs
111
+ homepage: https://github.com/vicvans20/ruby-brightpearl
112
+ licenses:
113
+ - MIT
114
+ metadata:
115
+ allowed_push_host: https://rubygems.org
116
+ homepage_uri: https://github.com/vicvans20/ruby-brightpearl
117
+ source_code_uri: https://github.com/vicvans20/ruby-brightpearl.
118
+ changelog_uri: https://github.com/vicvans20/ruby-brightpearl/blob/master/CHANGELOG.md
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: 2.6.0
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubygems_version: 3.2.32
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Brightpearl API ruby client.
138
+ test_files: []