woocommerce-ruby3-api 1.5.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +237 -0
- data/lib/woocommerce-ruby3-api.rb +6 -0
- data/lib/woocommerce_api/oauth.rb +103 -0
- data/lib/woocommerce_api/version.rb +5 -0
- data/lib/woocommerce_api.rb +207 -0
- data/woocommerce-ruby3-api.gemspec +45 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ebee0d5eded71aaceb36236edeff4d0d06baaa8a5689449126b62fa6e492a4d0
|
4
|
+
data.tar.gz: 871f97a97a96811df311a792a290f2524122996c240687c3e995dbdba663f7a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d05ee031281d786350affd50f0624b8bb72669494e444dffe55760473dec366ce5a777c72b56bf8f206c16c3548abadc242e651446453a099c7a55e29d75027
|
7
|
+
data.tar.gz: e8c5aed129cb2fc2195cf054181f9e460095471d24de111a2671f033c2680d8f7727279a3a72611d3f8e6c6060058378140a425cd2399ffcb150a88faf94a84c
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015, WooThemes (https://woocommerce.com/)
|
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,237 @@
|
|
1
|
+
# WooCommerce Ruby 3 API
|
2
|
+
|
3
|
+
A Ruby wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API using this library, optimized for Ruby 3.0+.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/woocommerce-ruby3-api)
|
6
|
+
[](https://github.com/chemica/woocommerce-ruby3-api/actions/workflows/test.yml)
|
7
|
+
|
8
|
+
## About
|
9
|
+
|
10
|
+
This is a fork of the [WooCommerce REST API - Ruby Client](https://github.com/woocommerce/wc-api-ruby) that has been updated to be compatible with modern Ruby versions (3.1+).
|
11
|
+
|
12
|
+
Key changes from the original:
|
13
|
+
- Fixed deprecated `URI.encode` calls using `Addressable::URI`
|
14
|
+
- Added explicit dependency on `base64` gem for Ruby 3.4+ compatibility
|
15
|
+
- All tests updated to use webmock instead of fakeweb
|
16
|
+
- Improved test organization
|
17
|
+
- Modern CI workflow with GitHub Actions
|
18
|
+
- Automated dependency updates with Dependabot
|
19
|
+
- Security scanning with bundler-audit
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
```
|
24
|
+
gem install woocommerce-ruby3-api
|
25
|
+
```
|
26
|
+
|
27
|
+
Or with Bundler:
|
28
|
+
|
29
|
+
```
|
30
|
+
gem 'woocommerce-ruby3-api'
|
31
|
+
```
|
32
|
+
|
33
|
+
## Getting started
|
34
|
+
|
35
|
+
Generate API credentials (Consumer Key & Consumer Secret) in your WooCommerce store. Instructions: <https://woocommerce.com/document/woocommerce-rest-api/>.
|
36
|
+
|
37
|
+
Check out the WooCommerce API endpoints and data that can be manipulated in <https://woocommerce.github.io/woocommerce-rest-api-docs/>.
|
38
|
+
|
39
|
+
## Setup
|
40
|
+
|
41
|
+
Setup for the WP REST API (recommended):
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
require "woocommerce-ruby3-api"
|
45
|
+
|
46
|
+
woocommerce = WooCommerce::API.new(
|
47
|
+
"https://example.com",
|
48
|
+
"consumer_key",
|
49
|
+
"consumer_secret",
|
50
|
+
{
|
51
|
+
wp_api: true,
|
52
|
+
version: "wc/v3"
|
53
|
+
}
|
54
|
+
)
|
55
|
+
```
|
56
|
+
|
57
|
+
Setup for the WooCommerce legacy API:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
require "woocommerce-ruby3-api"
|
61
|
+
|
62
|
+
woocommerce = WooCommerce::API.new(
|
63
|
+
"https://example.com",
|
64
|
+
"consumer_key",
|
65
|
+
"consumer_secret",
|
66
|
+
{
|
67
|
+
version: "v3"
|
68
|
+
}
|
69
|
+
)
|
70
|
+
```
|
71
|
+
|
72
|
+
### Options
|
73
|
+
|
74
|
+
| Option | Type | Required | Description |
|
75
|
+
| ----------------- | -------- | -------- | ---------------------------------------- |
|
76
|
+
| `url` | `String` | yes | Your Store URL, example: https://example.com/ |
|
77
|
+
| `consumer_key` | `String` | yes | Your API consumer key |
|
78
|
+
| `consumer_secret` | `String` | yes | Your API consumer secret |
|
79
|
+
| `args` | `Hash` | no | Extra arguments (see Args options table) |
|
80
|
+
|
81
|
+
#### Args options
|
82
|
+
|
83
|
+
| Option | Type | Required | Description |
|
84
|
+
|---------------------|----------|----------|--------------------------------------------------------------------------------------------------------------|
|
85
|
+
| `wp_api` | `Bool` | no | Allow requests to the WP REST API (WooCommerce 2.6 or later) |
|
86
|
+
| `version` | `String` | no | API version, default is `v3` |
|
87
|
+
| `verify_ssl` | `Bool` | no | Verify SSL when connect, use this option as `false` when need to test with self-signed certificates |
|
88
|
+
| `signature_method` | `String` | no | Signature method used for oAuth requests, works with `HMAC-SHA1` and `HMAC-SHA256`, default is `HMAC-SHA256` |
|
89
|
+
| `query_string_auth` | `Bool` | no | Force Basic Authentication as query string when `true` and using under HTTPS, default is `false` |
|
90
|
+
| `debug_mode` | `Bool` | no | Enables HTTParty debug mode |
|
91
|
+
| `httparty_args` | `Hash` | no | Allows extra HTTParty args |
|
92
|
+
|
93
|
+
## Methods
|
94
|
+
|
95
|
+
| Params | Type | Description |
|
96
|
+
| ---------- | -------- | ------------------------------------------------------------ |
|
97
|
+
| `endpoint` | `String` | WooCommerce API endpoint, example: `customers` or `order/12` |
|
98
|
+
| `data` | `Hash` | Only for POST and PUT, data that will be converted to JSON |
|
99
|
+
| `query` | `Hash` | Only for GET and DELETE, request query string |
|
100
|
+
|
101
|
+
### GET
|
102
|
+
|
103
|
+
- `.get(endpoint, query)`
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# Get all customers
|
107
|
+
woocommerce.get "customers"
|
108
|
+
|
109
|
+
# Get a specific customer
|
110
|
+
woocommerce.get "customers/1"
|
111
|
+
|
112
|
+
# Get with parameters
|
113
|
+
woocommerce.get "customers", status: "processing"
|
114
|
+
```
|
115
|
+
|
116
|
+
### POST
|
117
|
+
|
118
|
+
- `.post(endpoint, data)`
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# Create a customer
|
122
|
+
data = {
|
123
|
+
customer: {
|
124
|
+
email: "john.doe@example.com",
|
125
|
+
first_name: "John",
|
126
|
+
last_name: "Doe",
|
127
|
+
billing_address: {
|
128
|
+
first_name: "John",
|
129
|
+
last_name: "Doe",
|
130
|
+
company: "",
|
131
|
+
address_1: "969 Market",
|
132
|
+
address_2: "",
|
133
|
+
city: "San Francisco",
|
134
|
+
state: "CA",
|
135
|
+
postcode: "94103",
|
136
|
+
country: "US",
|
137
|
+
email: "john.doe@example.com",
|
138
|
+
phone: "(555) 555-5555"
|
139
|
+
},
|
140
|
+
shipping_address: {
|
141
|
+
first_name: "John",
|
142
|
+
last_name: "Doe",
|
143
|
+
company: "",
|
144
|
+
address_1: "969 Market",
|
145
|
+
address_2: "",
|
146
|
+
city: "San Francisco",
|
147
|
+
state: "CA",
|
148
|
+
postcode: "94103",
|
149
|
+
country: "US"
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
woocommerce.post "customers", data
|
155
|
+
```
|
156
|
+
|
157
|
+
### PUT
|
158
|
+
|
159
|
+
- `.put(endpoint, data)`
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
# Update a customer
|
163
|
+
data = {
|
164
|
+
customer: {
|
165
|
+
last_name: "Doe"
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
woocommerce.put "customers/1", data
|
170
|
+
```
|
171
|
+
|
172
|
+
### DELETE
|
173
|
+
|
174
|
+
- `.delete(endpoint, query)`
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
# Delete a customer
|
178
|
+
woocommerce.delete "customers/1"
|
179
|
+
|
180
|
+
# Force delete a customer
|
181
|
+
woocommerce.delete "customers/1", force: true
|
182
|
+
```
|
183
|
+
|
184
|
+
### OPTIONS
|
185
|
+
|
186
|
+
- `.options(endpoint)`
|
187
|
+
|
188
|
+
#### Response
|
189
|
+
|
190
|
+
All methods will return [HTTParty::Response](https://github.com/jnunemaker/httparty) object.
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
response = woocommerce.get "customers"
|
194
|
+
|
195
|
+
puts response.parsed_response # A Hash of the parsed JSON response
|
196
|
+
# Example: {"customers"=>[{"id"=>8, "created_at"=>"2015-05-06T17:43:51Z", "email"=>...
|
197
|
+
|
198
|
+
puts response.code # A Integer of the HTTP code response
|
199
|
+
# Example: 200
|
200
|
+
|
201
|
+
puts response.headers["x-wc-total"] # Total of items
|
202
|
+
# Example: 2
|
203
|
+
|
204
|
+
puts response.headers["x-wc-totalpages"] # Total of pages
|
205
|
+
# Example: 1
|
206
|
+
```
|
207
|
+
|
208
|
+
## Release History
|
209
|
+
|
210
|
+
### Ruby 3 Version (woocommerce-ruby3-api)
|
211
|
+
- 2025-10-04 - 1.5.0 - Initial fork with Ruby 3+ compatibility using Addressable::URI and base64 gem
|
212
|
+
|
213
|
+
### Original Project (woocommerce_api)
|
214
|
+
- 2016-12-14 - 1.4.0 - Introduces `httparty_args` arg and fixed compatibility with WordPress 4.7.
|
215
|
+
- 2016-09-15 - 1.3.0 - Added the `query_string_auth` and `debug_mode` options.
|
216
|
+
- 2016-06-26 - 1.2.1 - Fixed oAuth signature for WP REST API.
|
217
|
+
- 2016-05-09 - 1.2.0 - Added support for WP REST API and added method to do HTTP OPTIONS requests.
|
218
|
+
- 2015-12-07 - 1.1.2 - Stop send `body` in GET and DELETE requests.
|
219
|
+
- 2015-12-07 - 1.1.1 - Fixed the encode when have spaces in the URL parameters.
|
220
|
+
- 2015-08-27 - 1.1.0 - Added `query` argument for GET and DELETE methods, reduced chance of nonce collisions and added support for urls including ports.
|
221
|
+
- 2015-08-27 - 1.0.3 - Encode all % characters in query string for OAuth 1.0a.
|
222
|
+
- 2015-08-12 - 1.0.2 - Fixed the release date.
|
223
|
+
- 2015-08-12 - 1.0.1 - Escaped oauth_signature in url query string.
|
224
|
+
- 2015-07-15 - 1.0.0 - Stable release.
|
225
|
+
- 2015-07-15 - 0.0.1 - Beta release.
|
226
|
+
|
227
|
+
## Contributing
|
228
|
+
|
229
|
+
1. Fork it
|
230
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
231
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
232
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
233
|
+
5. Create new Pull Request
|
234
|
+
|
235
|
+
## License
|
236
|
+
|
237
|
+
Released under the [MIT License](LICENSE).
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "digest/sha1"
|
4
|
+
require "cgi"
|
5
|
+
require "uri"
|
6
|
+
require "base64"
|
7
|
+
require "openssl"
|
8
|
+
require "addressable/uri"
|
9
|
+
|
10
|
+
module WooCommerce
|
11
|
+
class OAuth
|
12
|
+
class InvalidSignatureMethodError < StandardError; end
|
13
|
+
|
14
|
+
def initialize url, method, version, consumer_key, consumer_secret, signature_method = "HMAC-SHA256"
|
15
|
+
@url = url
|
16
|
+
@method = method.upcase
|
17
|
+
@version = version
|
18
|
+
@consumer_key = consumer_key
|
19
|
+
@consumer_secret = consumer_secret
|
20
|
+
@signature_method = signature_method
|
21
|
+
end
|
22
|
+
|
23
|
+
# Public: Get OAuth url
|
24
|
+
#
|
25
|
+
# Returns the OAuth url.
|
26
|
+
def get_oauth_url
|
27
|
+
params = {}
|
28
|
+
url = @url
|
29
|
+
|
30
|
+
if url.include?("?")
|
31
|
+
parsed_url = URI::parse(url)
|
32
|
+
CGI::parse(parsed_url.query).each do |key, value|
|
33
|
+
params[key] = value[0]
|
34
|
+
end
|
35
|
+
params = Hash[params.sort]
|
36
|
+
|
37
|
+
url = parsed_url.to_s.gsub(/\?.*/, "")
|
38
|
+
end
|
39
|
+
|
40
|
+
nonce_lifetime = 15 * 60 # Woocommerce keeps nonces for 15 minutes
|
41
|
+
|
42
|
+
params["oauth_consumer_key"] = @consumer_key
|
43
|
+
params["oauth_nonce"] = Digest::SHA1.hexdigest((Time.new.to_f % nonce_lifetime + (Process.pid * nonce_lifetime)).to_s)
|
44
|
+
params["oauth_signature_method"] = @signature_method
|
45
|
+
params["oauth_timestamp"] = Time.new.to_i
|
46
|
+
params["oauth_signature"] = CGI::escape(generate_oauth_signature(params, url))
|
47
|
+
|
48
|
+
query_string = Addressable::URI.encode(params.map{|key, value| "#{key}=#{value}"}.join("&"))
|
49
|
+
|
50
|
+
"#{url}?#{query_string}"
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
# Internal: Generate the OAuth Signature
|
56
|
+
#
|
57
|
+
# params - A Hash with the OAuth params.
|
58
|
+
# url - A String with a URL
|
59
|
+
#
|
60
|
+
# Returns the oauth signature String.
|
61
|
+
def generate_oauth_signature params, url
|
62
|
+
base_request_uri = CGI::escape(url.to_s)
|
63
|
+
query_params = []
|
64
|
+
|
65
|
+
params.sort.map do |key, value|
|
66
|
+
query_params.push(encode_param(key.to_s) + "%3D" + encode_param(value.to_s))
|
67
|
+
end
|
68
|
+
|
69
|
+
query_string = query_params
|
70
|
+
.join("%26")
|
71
|
+
string_to_sign = "#{@method}&#{base_request_uri}&#{query_string}"
|
72
|
+
|
73
|
+
if !["v1", "v2"].include? @version
|
74
|
+
consumer_secret = "#{@consumer_secret}&"
|
75
|
+
else
|
76
|
+
consumer_secret = @consumer_secret
|
77
|
+
end
|
78
|
+
|
79
|
+
return Base64.strict_encode64(OpenSSL::HMAC.digest(digest, consumer_secret, string_to_sign))
|
80
|
+
end
|
81
|
+
|
82
|
+
# Internal: Digest object based on signature method
|
83
|
+
#
|
84
|
+
# Returns a digest object.
|
85
|
+
def digest
|
86
|
+
case @signature_method
|
87
|
+
when "HMAC-SHA256" then OpenSSL::Digest.new("sha256")
|
88
|
+
when "HMAC-SHA1" then OpenSSL::Digest.new("sha1")
|
89
|
+
else
|
90
|
+
fail InvalidSignatureMethodError
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Internal: Encode param
|
95
|
+
#
|
96
|
+
# text - A String to be encoded
|
97
|
+
#
|
98
|
+
# Returns the encoded String.
|
99
|
+
def encode_param text
|
100
|
+
CGI::escape(text).gsub("+", "%20").gsub("%", "%25")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "httparty"
|
4
|
+
require "json"
|
5
|
+
require "cgi"
|
6
|
+
require "cgi"
|
7
|
+
require "addressable/uri"
|
8
|
+
|
9
|
+
require "woocommerce_api/oauth"
|
10
|
+
require "woocommerce_api/version"
|
11
|
+
|
12
|
+
module WooCommerce
|
13
|
+
class API
|
14
|
+
|
15
|
+
def initialize url, consumer_key, consumer_secret, args = {}
|
16
|
+
# Required args
|
17
|
+
@url = url
|
18
|
+
@consumer_key = consumer_key
|
19
|
+
@consumer_secret = consumer_secret
|
20
|
+
|
21
|
+
# Optional args
|
22
|
+
defaults = {
|
23
|
+
wp_api: false,
|
24
|
+
version: "v3",
|
25
|
+
verify_ssl: true,
|
26
|
+
signature_method: "HMAC-SHA256",
|
27
|
+
httparty_args: {}
|
28
|
+
}
|
29
|
+
args = defaults.merge(args)
|
30
|
+
|
31
|
+
@wp_api = args[:wp_api]
|
32
|
+
@version = args[:version]
|
33
|
+
@verify_ssl = args[:verify_ssl] == true
|
34
|
+
@signature_method = args[:signature_method]
|
35
|
+
@debug_mode = args[:debug_mode]
|
36
|
+
@query_string_auth = args[:query_string_auth]
|
37
|
+
@httparty_args = args[:httparty_args]
|
38
|
+
|
39
|
+
# Internal args
|
40
|
+
@is_ssl = @url.start_with? "https"
|
41
|
+
end
|
42
|
+
|
43
|
+
# Public: GET requests.
|
44
|
+
#
|
45
|
+
# endpoint - A String naming the request endpoint.
|
46
|
+
# query - A Hash with query params.
|
47
|
+
#
|
48
|
+
# Returns the request Hash.
|
49
|
+
def get endpoint, query = nil
|
50
|
+
do_request :get, add_query_params(endpoint, query)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Public: POST requests.
|
54
|
+
#
|
55
|
+
# endpoint - A String naming the request endpoint.
|
56
|
+
# data - The Hash data for the request.
|
57
|
+
#
|
58
|
+
# Returns the request Hash.
|
59
|
+
def post endpoint, data
|
60
|
+
do_request :post, endpoint, data
|
61
|
+
end
|
62
|
+
|
63
|
+
# Public: PUT requests.
|
64
|
+
#
|
65
|
+
# endpoint - A String naming the request endpoint.
|
66
|
+
# data - The Hash data for the request.
|
67
|
+
#
|
68
|
+
# Returns the request Hash.
|
69
|
+
def put endpoint, data
|
70
|
+
do_request :put, endpoint, data
|
71
|
+
end
|
72
|
+
|
73
|
+
# Public: DELETE requests.
|
74
|
+
#
|
75
|
+
# endpoint - A String naming the request endpoint.
|
76
|
+
# query - A Hash with query params.
|
77
|
+
#
|
78
|
+
# Returns the request Hash.
|
79
|
+
def delete endpoint, query = nil
|
80
|
+
do_request :delete, add_query_params(endpoint, query)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Public: OPTIONS requests.
|
84
|
+
#
|
85
|
+
# endpoint - A String naming the request endpoint.
|
86
|
+
#
|
87
|
+
# Returns the request Hash.
|
88
|
+
def options endpoint
|
89
|
+
do_request :options, add_query_params(endpoint, nil)
|
90
|
+
end
|
91
|
+
|
92
|
+
protected
|
93
|
+
|
94
|
+
# Internal: Appends data as query params onto an endpoint
|
95
|
+
#
|
96
|
+
# endpoint - A String naming the request endpoint.
|
97
|
+
# data - A hash of data to flatten and append
|
98
|
+
#
|
99
|
+
# Returns an endpoint string with the data appended
|
100
|
+
def add_query_params endpoint, data
|
101
|
+
return endpoint if data.nil? || data.empty?
|
102
|
+
|
103
|
+
endpoint += "?" unless endpoint.include? "?"
|
104
|
+
endpoint += "&" unless endpoint.end_with? "?"
|
105
|
+
endpoint + Addressable::URI.encode(flatten_hash(data).join("&"))
|
106
|
+
end
|
107
|
+
|
108
|
+
# Internal: Get URL for requests
|
109
|
+
#
|
110
|
+
# endpoint - A String naming the request endpoint.
|
111
|
+
# method - The method used in the url (for oauth querys)
|
112
|
+
#
|
113
|
+
# Returns the endpoint String.
|
114
|
+
def get_url endpoint, method
|
115
|
+
api = @wp_api ? 'wp-json' : 'wc-api'
|
116
|
+
url = @url
|
117
|
+
url = "#{url}/" unless url.end_with? "/"
|
118
|
+
url = "#{url}#{api}/#{@version}/#{endpoint}"
|
119
|
+
|
120
|
+
@is_ssl ? url : oauth_url(url, method)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Internal: Requests default options.
|
124
|
+
#
|
125
|
+
# method - A String naming the request method
|
126
|
+
# endpoint - A String naming the request endpoint.
|
127
|
+
# data - The Hash data for the request.
|
128
|
+
#
|
129
|
+
# Returns the response in JSON String.
|
130
|
+
def do_request method, endpoint, data = {}
|
131
|
+
url = get_url(endpoint, method)
|
132
|
+
options = {
|
133
|
+
format: :json
|
134
|
+
}
|
135
|
+
|
136
|
+
# Allow custom HTTParty args.
|
137
|
+
options = options.merge(@httparty_args)
|
138
|
+
|
139
|
+
# Set headers.
|
140
|
+
options[:headers] = {
|
141
|
+
"User-Agent" => "WooCommerce API Client-Ruby/#{WooCommerce::VERSION}",
|
142
|
+
"Accept" => "application/json"
|
143
|
+
}
|
144
|
+
options[:headers]["Content-Type"] = "application/json;charset=utf-8" if !data.empty?
|
145
|
+
|
146
|
+
# Set basic authentication.
|
147
|
+
if @is_ssl
|
148
|
+
options[:verify] = @verify_ssl
|
149
|
+
|
150
|
+
if @query_string_auth
|
151
|
+
options.merge!(query: {
|
152
|
+
consumer_key: @consumer_key,
|
153
|
+
consumer_secret: @consumer_secret
|
154
|
+
})
|
155
|
+
else
|
156
|
+
options.merge!(basic_auth: {
|
157
|
+
username: @consumer_key,
|
158
|
+
password: @consumer_secret
|
159
|
+
})
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
options.merge!(debug_output: $stdout) if @debug_mode
|
164
|
+
options.merge!(body: data.to_json) if !data.empty?
|
165
|
+
|
166
|
+
HTTParty.send(method, url, options)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Internal: Generates an oauth url given current settings
|
170
|
+
#
|
171
|
+
# url - A String naming the current request url
|
172
|
+
# method - The HTTP verb of the request
|
173
|
+
#
|
174
|
+
# Returns a url to be used for the query.
|
175
|
+
def oauth_url url, method
|
176
|
+
oauth = WooCommerce::OAuth.new(
|
177
|
+
url,
|
178
|
+
method,
|
179
|
+
@version,
|
180
|
+
@consumer_key,
|
181
|
+
@consumer_secret,
|
182
|
+
@signature_method
|
183
|
+
)
|
184
|
+
oauth.get_oauth_url
|
185
|
+
end
|
186
|
+
|
187
|
+
# Internal: Flattens a hash into an array of query relations
|
188
|
+
#
|
189
|
+
# hash - A hash to flatten
|
190
|
+
#
|
191
|
+
# Returns an array full of key value paired strings
|
192
|
+
def flatten_hash hash
|
193
|
+
hash.flat_map do |key, value|
|
194
|
+
case value
|
195
|
+
when Hash
|
196
|
+
value.map do |inner_key, inner_value|
|
197
|
+
"#{key}[#{inner_key}]=#{inner_value}"
|
198
|
+
end
|
199
|
+
when Array
|
200
|
+
value.map { |inner_value| "#{key}[]=#{inner_value}" }
|
201
|
+
else
|
202
|
+
"#{key}=#{value}"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# -*- encoding: utf-8 -*-
|
4
|
+
lib = File.expand_path("../lib", __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
# Version is loaded directly to avoid circular requires
|
7
|
+
require_relative "lib/woocommerce_api/version"
|
8
|
+
|
9
|
+
Gem::Specification.new do |s|
|
10
|
+
s.name = "woocommerce-ruby3-api"
|
11
|
+
s.version = WooCommerce::VERSION
|
12
|
+
s.date = "2025-10-04"
|
13
|
+
|
14
|
+
s.summary = "A Ruby 3+ wrapper for the WooCommerce API"
|
15
|
+
s.description = "This gem provides a wrapper to access the WooCommerce REST API, optimized for Ruby 3+"
|
16
|
+
s.license = "MIT"
|
17
|
+
|
18
|
+
s.authors = ["Claudio Sanches", "Benjamin Randles-Dunkley"]
|
19
|
+
s.email = "ben@chemica.co.uk"
|
20
|
+
|
21
|
+
# Files included in the gem
|
22
|
+
s.files = Dir[
|
23
|
+
"lib/**/*.rb",
|
24
|
+
"README.md",
|
25
|
+
"LICENSE",
|
26
|
+
"*.gemspec"
|
27
|
+
]
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
|
30
|
+
s.homepage = "https://github.com/chemica/woocommerce-ruby3-api"
|
31
|
+
s.required_ruby_version = ">= 3.1.0"
|
32
|
+
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.extra_rdoc_files = %w[README.md LICENSE]
|
35
|
+
|
36
|
+
s.add_runtime_dependency "httparty", "~> 0.23.1"
|
37
|
+
s.add_runtime_dependency "json", "~> 2.10.2", ">= 2.6.0"
|
38
|
+
s.add_runtime_dependency "base64", "~> 0.2.0"
|
39
|
+
s.add_runtime_dependency "addressable", "~> 2.8.1"
|
40
|
+
|
41
|
+
s.add_development_dependency "rake", "~> 13.2.1"
|
42
|
+
s.add_development_dependency "minitest", "~> 5.25.5"
|
43
|
+
s.add_development_dependency "webmock", "~> 3.25.1"
|
44
|
+
s.add_development_dependency "bundler-audit", "~> 0.9.1"
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: woocommerce-ruby3-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Claudio Sanches
|
8
|
+
- Benjamin Randles-Dunkley
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-10-04 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.23.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.23.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.10.2
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.6.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.10.2
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.6.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: base64
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.2.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 0.2.0
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: addressable
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 2.8.1
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.8.1
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rake
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 13.2.1
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 13.2.1
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: minitest
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 5.25.5
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 5.25.5
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: webmock
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 3.25.1
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 3.25.1
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: bundler-audit
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 0.9.1
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 0.9.1
|
131
|
+
description: This gem provides a wrapper to access the WooCommerce REST API, optimized
|
132
|
+
for Ruby 3+
|
133
|
+
email: ben@chemica.co.uk
|
134
|
+
executables: []
|
135
|
+
extensions: []
|
136
|
+
extra_rdoc_files:
|
137
|
+
- LICENSE
|
138
|
+
- README.md
|
139
|
+
files:
|
140
|
+
- LICENSE
|
141
|
+
- README.md
|
142
|
+
- lib/woocommerce-ruby3-api.rb
|
143
|
+
- lib/woocommerce_api.rb
|
144
|
+
- lib/woocommerce_api/oauth.rb
|
145
|
+
- lib/woocommerce_api/version.rb
|
146
|
+
- woocommerce-ruby3-api.gemspec
|
147
|
+
homepage: https://github.com/chemica/woocommerce-ruby3-api
|
148
|
+
licenses:
|
149
|
+
- MIT
|
150
|
+
metadata: {}
|
151
|
+
rdoc_options:
|
152
|
+
- "--charset=UTF-8"
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 3.1.0
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubygems_version: 3.6.7
|
167
|
+
specification_version: 4
|
168
|
+
summary: A Ruby 3+ wrapper for the WooCommerce API
|
169
|
+
test_files: []
|