woocommercerb 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: 2f7654089a34f24c6933243630226e58da26617e8022f5354622c51616df4d3c
4
+ data.tar.gz: 821bacc1a3ac46dd98c45eed3fc67e435023ac52f1f716566fe6d6e89bc0d82c
5
+ SHA512:
6
+ metadata.gz: b9c911b990804c87b1877149d7cde1f9117037ded67c3b9b0f2b93b6fd1819e82844cf23c78610f0eb92203fe4d5043ae922aa24e26907ee98a62690f48563df
7
+ data.tar.gz: 4da457a6d06095edc6ed3a633cca039c9cfb953f99122f2a001580a7d75f92074083576eb050e90b3616c47753f1fe81c5efd58aa39e7213d278e2550991490e
data/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ URL=
2
+ API_KEY=
3
+ API_SECRET=
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in woocommerce.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ woocommercerb (0.1.0)
5
+ faraday (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ dotenv (2.7.6)
11
+ faraday (2.2.0)
12
+ faraday-net_http (~> 2.0)
13
+ ruby2_keywords (>= 0.0.4)
14
+ faraday-net_http (2.0.1)
15
+ rake (13.0.6)
16
+ ruby2_keywords (0.0.5)
17
+
18
+ PLATFORMS
19
+ x86_64-linux
20
+
21
+ DEPENDENCIES
22
+ dotenv
23
+ rake (~> 13.0)
24
+ woocommercerb!
25
+
26
+ BUNDLED WITH
27
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dean Perry
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,39 @@
1
+ # WooCommerceRB
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/woocommerce`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'woocommerce'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install woocommerce
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/woocommerce.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,52 @@
1
+ module WooCommerce
2
+ class Client
3
+
4
+ attr_reader :url, :api_key, :api_secret, :adapter
5
+
6
+ def initialize(url:, api_key:, api_secret:, adapter: Faraday.default_adapter, stubs: nil)
7
+ @url = url
8
+ @api_key = api_key
9
+ @api_secret = api_secret
10
+ @adapter = adapter
11
+
12
+ # Test stubs for requests
13
+ @stubs = stubs
14
+ end
15
+
16
+ def coupons
17
+ CouponsResource.new(self)
18
+ end
19
+
20
+ def customers
21
+ CustomersResource.new(self)
22
+ end
23
+
24
+ def orders
25
+ OrdersResource.new(self)
26
+ end
27
+
28
+ def order_notes
29
+ OrderNotesResource.new(self)
30
+ end
31
+
32
+ def products
33
+ ProductsResource.new(self)
34
+ end
35
+
36
+ def product_categories
37
+ ProductCategoriesResource.new(self)
38
+ end
39
+
40
+ def connection
41
+ @connection ||= Faraday.new("#{url}/wp-json/wc/v3") do |conn|
42
+ conn.request :authorization, :basic, api_key, api_secret
43
+ conn.request :json
44
+
45
+ conn.response :json, content_type: "application/json"
46
+
47
+ conn.adapter adapter, @stubs
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,19 @@
1
+ module WooCommerce
2
+ class Collection
3
+ attr_reader :data, :total
4
+
5
+ def self.from_response(response, type:)
6
+ body = response.body
7
+
8
+ new(
9
+ data: body.map { |attrs| type.new(attrs) },
10
+ total: body.count,
11
+ )
12
+ end
13
+
14
+ def initialize(data:, total:)
15
+ @data = data
16
+ @total = total
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module WooCommerce
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module WooCommerce
2
+ class Coupon < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module WooCommerce
2
+ class Customer < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module WooCommerce
2
+ class Order < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module WooCommerce
2
+ class OrderNote < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module WooCommerce
2
+ class Product < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module WooCommerce
2
+ class ProductCategory < Object
3
+ end
4
+ end
@@ -0,0 +1,54 @@
1
+ module WooCommerce
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body:, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body:, headers: {})
20
+ handle_response client.connection.patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body:, headers: {})
24
+ handle_response client.connection.put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {})
28
+ handle_response client.connection.delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Error 400: Your request was malformed. '#{response.body["message"]}'"
35
+ when 401
36
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["message"]}'"
37
+ when 403
38
+ raise Error, "Error 403: You are not allowed to perform that action."
39
+ when 404
40
+ raise Error, "Error 404: No results were found for your request."
41
+ when 409
42
+ raise Error, "Error 409: Your request was a conflict."
43
+ when 429
44
+ raise Error, "Error 429: Your request exceeded the API rate limit."
45
+ when 500
46
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems."
47
+ when 503
48
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second."
49
+ end
50
+
51
+ response
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,28 @@
1
+ module WooCommerce
2
+ class CouponsResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request("coupons", params: params)
6
+ Collection.from_response(response, type: Coupon)
7
+ end
8
+
9
+ def retrieve(id:)
10
+ Coupon.new get_request("coupons/#{id}").body
11
+ end
12
+
13
+ def create(code:, discount_type:, amount:, **params)
14
+ attributes = {code: code, discount_type: discount_type, amount: amount.to_s}
15
+ Coupon.new post_request("coupons", body: attributes.merge(params)).body
16
+ end
17
+
18
+ def update(id:, **params)
19
+ Coupon.new put_request("coupons/#{id}", body: params).body
20
+ end
21
+
22
+ def delete(id:)
23
+ response = delete_request("coupons/#{id}", params: {force: true})
24
+ return true if response.success?
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module WooCommerce
2
+ class CustomersResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request("customers", params: params)
6
+ Collection.from_response(response, type: Customer)
7
+ end
8
+
9
+ def retrieve(id:)
10
+ Customer.new get_request("customers/#{id}").body
11
+ end
12
+
13
+ def create(email:, **params)
14
+ attributes = {email: email}
15
+ Customer.new post_request("customers", body: attributes.merge(params)).body
16
+ end
17
+
18
+ def update(id:, **params)
19
+ Customer.new put_request("customers/#{id}", body: params).body
20
+ end
21
+
22
+ def delete(id:)
23
+ response = delete_request("customers/#{id}", params: {force: true})
24
+ return true if response.success?
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module WooCommerce
2
+ class OrderNotesResource < Resource
3
+
4
+ def list(order:, **params)
5
+ response = get_request("orders/#{order}/notes", params: params)
6
+ Collection.from_response(response, type: OrderNote)
7
+ end
8
+
9
+ def retrieve(order:, id:)
10
+ OrderNote.new get_request("orders/#{order}/notes/#{id}").body
11
+ end
12
+
13
+ def create(order:, note:, **params)
14
+ attributes = {note: note}
15
+ OrderNote.new post_request("orders/#{order}/notes", body: attributes.merge(params)).body
16
+ end
17
+
18
+ def update(order:, id:, **params)
19
+ OrderNote.new put_request("orders/#{order}/notes/#{id}", body: params).body
20
+ end
21
+
22
+ def delete(order:, id:)
23
+ response = delete_request("orders/#{order}/notes/#{id}", params: {force: true})
24
+ return true if response.success?
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module WooCommerce
2
+ class OrdersResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request("orders", params: params)
6
+ Collection.from_response(response, type: Order)
7
+ end
8
+
9
+ def retrieve(id:)
10
+ Order.new get_request("orders/#{id}").body
11
+ end
12
+
13
+ def create(**params)
14
+ Order.new post_request("orders", body: params).body
15
+ end
16
+
17
+ def update(id:, **params)
18
+ Order.new put_request("orders/#{id}", body: params).body
19
+ end
20
+
21
+ def delete(id:)
22
+ response = delete_request("orders/#{id}", params: {force: true})
23
+ return true if response.success?
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module WooCommerce
2
+ class ProductCategoriesResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request("products/categories", params: params)
6
+ Collection.from_response(response, type: ProductCategory)
7
+ end
8
+
9
+ def retrieve(id:)
10
+ ProductCategory.new get_request("products/categories/#{id}").body
11
+ end
12
+
13
+ def create(name:, **params)
14
+ attributes = {name: name}
15
+ ProductCategory.new post_request("products/categories", body: attributes.merge(params)).body
16
+ end
17
+
18
+ def update(id:, **params)
19
+ ProductCategory.new put_request("products/categories/#{id}", body: params).body
20
+ end
21
+
22
+ def delete(id:)
23
+ response = delete_request("products/categories/#{id}", params: {force: true})
24
+ return true if response.success?
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module WooCommerce
2
+ class ProductsResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request("products", params: params)
6
+ Collection.from_response(response, type: Product)
7
+ end
8
+
9
+ def retrieve(id:)
10
+ Product.new get_request("products/#{id}").body
11
+ end
12
+
13
+ def create(name:, **params)
14
+ attributes = {name: name}
15
+ Product.new post_request("products", body: attributes.merge(params)).body
16
+ end
17
+
18
+ def update(id:, **params)
19
+ Product.new put_request("products/#{id}", body: params).body
20
+ end
21
+
22
+ def delete(id:)
23
+ response = delete_request("products/#{id}", params: {force: true})
24
+ return true if response.success?
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WooCommerce
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,27 @@
1
+ require "faraday"
2
+ require "json"
3
+ require_relative "woo_commerce/version"
4
+
5
+ module WooCommerce
6
+ class Error < StandardError; end
7
+
8
+ autoload :Client, "woo_commerce/client"
9
+ autoload :Collection, "woo_commerce/collection"
10
+ autoload :Resource, "woo_commerce/resource"
11
+ autoload :Object, "woo_commerce/object"
12
+
13
+ autoload :CouponsResource, "woo_commerce/resources/coupons"
14
+ autoload :CustomersResource, "woo_commerce/resources/customers"
15
+ autoload :OrdersResource, "woo_commerce/resources/orders"
16
+ autoload :OrderNotesResource, "woo_commerce/resources/order_notes"
17
+ autoload :ProductsResource, "woo_commerce/resources/products"
18
+ autoload :ProductCategoriesResource, "woo_commerce/resources/product_categories"
19
+
20
+ autoload :Coupon, "woo_commerce/objects/coupon"
21
+ autoload :Customer, "woo_commerce/objects/customer"
22
+ autoload :Order, "woo_commerce/objects/order"
23
+ autoload :OrderNote, "woo_commerce/objects/order_note"
24
+ autoload :Product, "woo_commerce/objects/product"
25
+ autoload :ProductCategory, "woo_commerce/objects/product_category"
26
+
27
+ end
@@ -0,0 +1 @@
1
+ require "woo_commerce"
@@ -0,0 +1,4 @@
1
+ module Woocommerce
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/woo_commerce/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "woocommercerb"
7
+ spec.version = WooCommerce::VERSION
8
+ spec.authors = ["Dean Perry"]
9
+ spec.email = ["dean@deanpcmad.com"]
10
+
11
+ spec.summary = "Ruby Library for interacting with WooCommerce V3 APIs"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://deanpcmad.com"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/deanpcmad/woocommercerb"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "faraday", "~> 2.0"
32
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: woocommercerb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Ruby Library for interacting with WooCommerce V3 APIs
28
+ email:
29
+ - dean@deanpcmad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/woo_commerce.rb
41
+ - lib/woo_commerce/client.rb
42
+ - lib/woo_commerce/collection.rb
43
+ - lib/woo_commerce/object.rb
44
+ - lib/woo_commerce/objects/coupon.rb
45
+ - lib/woo_commerce/objects/customer.rb
46
+ - lib/woo_commerce/objects/order.rb
47
+ - lib/woo_commerce/objects/order_note.rb
48
+ - lib/woo_commerce/objects/product.rb
49
+ - lib/woo_commerce/objects/product_category.rb
50
+ - lib/woo_commerce/resource.rb
51
+ - lib/woo_commerce/resources/coupons.rb
52
+ - lib/woo_commerce/resources/customers.rb
53
+ - lib/woo_commerce/resources/order_notes.rb
54
+ - lib/woo_commerce/resources/orders.rb
55
+ - lib/woo_commerce/resources/product_categories.rb
56
+ - lib/woo_commerce/resources/products.rb
57
+ - lib/woo_commerce/version.rb
58
+ - lib/woocommercerb.rb
59
+ - sig/woocommerce.rbs
60
+ - woocommercerb.gemspec
61
+ homepage: https://deanpcmad.com
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://deanpcmad.com
66
+ source_code_uri: https://github.com/deanpcmad/woocommercerb
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.6.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.2.22
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Ruby Library for interacting with WooCommerce V3 APIs
86
+ test_files: []