woocommerce_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.md +98 -0
- data/lib/woocommerce_api.rb +123 -0
- data/lib/woocommerce_api/oauth.rb +76 -0
- data/lib/woocommerce_api/version.rb +3 -0
- metadata +85 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015, WooThemes (http://www.woothemes.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,98 @@
|
|
1
|
+
# WooCommerce API - Ruby Client
|
2
|
+
|
3
|
+
A Ruby wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API using this library.
|
4
|
+
|
5
|
+
[![npm version](https://img.shields.io/gem/v/formatador.svg)](https://rubygems.org/gems/woocommerce_api)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```
|
10
|
+
gem install woocommerce_api
|
11
|
+
```
|
12
|
+
|
13
|
+
## Getting started
|
14
|
+
|
15
|
+
Generate API credentials (Consumer Key & Consumer Secret) following this instructions <http://docs.woothemes.com/document/woocommerce-rest-api/>
|
16
|
+
.
|
17
|
+
|
18
|
+
Check out the WooCommerce API endpoints and data that can be manipulated in <http://woothemes.github.io/woocommerce-rest-api-docs/>.
|
19
|
+
|
20
|
+
## Setup
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require "woocommerce_api"
|
24
|
+
|
25
|
+
woocommerce = WooCommerce::API.new(
|
26
|
+
"http://example.com",
|
27
|
+
"ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
|
28
|
+
"cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
29
|
+
)
|
30
|
+
```
|
31
|
+
|
32
|
+
### Options
|
33
|
+
|
34
|
+
| Option | Type | Required | Description |
|
35
|
+
| ----------------- | -------- | -------- | ---------------------------------------- |
|
36
|
+
| `url` | `String` | yes | Your Store URL, example: http://woo.dev/ |
|
37
|
+
| `consumer_key` | `String` | yes | Your API consumer key |
|
38
|
+
| `consumer_secret` | `String` | yes | Your API consumer secret |
|
39
|
+
| `args` | `Hash` | no | Extra arguments (see Args options table) |
|
40
|
+
|
41
|
+
#### Args options
|
42
|
+
|
43
|
+
| Option | Type | Required | Description |
|
44
|
+
| ------------ | -------- | -------- | --------------------------------------------------------------------------------------------------- |
|
45
|
+
| `version` | `String` | no | API version, default is `v3` |
|
46
|
+
| `verify_ssl` | `Bool` | no | Verify SSL when connect, use this option as `false` when need to test with self-signed certificates |
|
47
|
+
|
48
|
+
## Methods
|
49
|
+
|
50
|
+
| Params | Type | Description |
|
51
|
+
| ---------- | ---------- | ------------------------------------------------------------ |
|
52
|
+
| `endpoint` | `String` | WooCommerce API endpoint, example: `customers` or `order/12` |
|
53
|
+
| `data` | `Object` | JS object, will be converted to JSON |
|
54
|
+
| `callback` | `Function` | Callback function. Returns `err`, `data` and `res` |
|
55
|
+
|
56
|
+
### GET
|
57
|
+
|
58
|
+
- `.get(endpoint)`
|
59
|
+
|
60
|
+
### POST
|
61
|
+
|
62
|
+
- `.post(endpoint, data)`
|
63
|
+
|
64
|
+
### PUT
|
65
|
+
|
66
|
+
- `.put(endpoint, data)`
|
67
|
+
|
68
|
+
### DELETE
|
69
|
+
|
70
|
+
- `.delete(endpoint)`
|
71
|
+
|
72
|
+
#### Response
|
73
|
+
|
74
|
+
All methods will return [HTTPary::Response](https://github.com/jnunemaker/httparty) object.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
response = api.get "customers"
|
78
|
+
|
79
|
+
puts response.parsed_response # A Hash of the parsed JSON response
|
80
|
+
# Example: {"customers"=>[{"id"=>8, "created_at"=>"2015-05-06T17:43:51Z", "email"=>
|
81
|
+
|
82
|
+
puts response.code # A Interger of the HTTP code response
|
83
|
+
# Example: 200
|
84
|
+
|
85
|
+
puts response.headers["x-wc-total"] # Total of items
|
86
|
+
# Example: 2
|
87
|
+
|
88
|
+
puts response.headers["x-wc-totalpages"] # Total of pages
|
89
|
+
# Example: 1
|
90
|
+
```
|
91
|
+
|
92
|
+
## Release History
|
93
|
+
|
94
|
+
- 2015-07-15 - 0.0.1 - Beta release.
|
95
|
+
|
96
|
+
## TODO
|
97
|
+
|
98
|
+
- Unit Tests
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
require "woocommerce_api/oauth"
|
5
|
+
require "woocommerce_api/version"
|
6
|
+
|
7
|
+
module WooCommerce
|
8
|
+
class API
|
9
|
+
|
10
|
+
def initialize url, consumer_key, consumer_secret, args = {}
|
11
|
+
# Required args
|
12
|
+
@url = url
|
13
|
+
@consumer_key = consumer_key
|
14
|
+
@consumer_secret = consumer_secret
|
15
|
+
|
16
|
+
# Optional args
|
17
|
+
defaults = {version: "v3", verify_ssl: true}
|
18
|
+
args = defaults.merge(args)
|
19
|
+
|
20
|
+
@version = args[:version]
|
21
|
+
@verify_ssl = args[:verify_ssl] == true
|
22
|
+
|
23
|
+
# Internal args
|
24
|
+
@is_ssl = @url.start_with? "https"
|
25
|
+
end
|
26
|
+
|
27
|
+
# Public: GET requests.
|
28
|
+
#
|
29
|
+
# endpoint - A String naming the request endpoint.
|
30
|
+
#
|
31
|
+
# Returns the request Hash.
|
32
|
+
def get endpoint
|
33
|
+
do_request :get, endpoint
|
34
|
+
end
|
35
|
+
|
36
|
+
# Public: POST requests.
|
37
|
+
#
|
38
|
+
# endpoint - A String naming the request endpoint.
|
39
|
+
# data - The Hash data for the request.
|
40
|
+
#
|
41
|
+
# Returns the request Hash.
|
42
|
+
def post endpoint, data
|
43
|
+
do_request :post, endpoint, data
|
44
|
+
end
|
45
|
+
|
46
|
+
# Public: PUT requests.
|
47
|
+
#
|
48
|
+
# endpoint - A String naming the request endpoint.
|
49
|
+
# data - The Hash data for the request.
|
50
|
+
#
|
51
|
+
# Returns the request Hash.
|
52
|
+
def put endpoint, data
|
53
|
+
do_request :put, endpoint, data
|
54
|
+
end
|
55
|
+
|
56
|
+
# Public: DELETE requests.
|
57
|
+
#
|
58
|
+
# endpoint - A String naming the request endpoint.
|
59
|
+
#
|
60
|
+
# Returns the request Hash.
|
61
|
+
def delete endpoint
|
62
|
+
do_request :delete, endpoint
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
|
67
|
+
# Internal: Get URL for requests
|
68
|
+
#
|
69
|
+
# endpoint - A String naming the request endpoint.
|
70
|
+
#
|
71
|
+
# Returns the endpoint String.
|
72
|
+
def get_url endpoint
|
73
|
+
url = @url
|
74
|
+
if !url.end_with? "/"
|
75
|
+
url = "#{url}/"
|
76
|
+
end
|
77
|
+
|
78
|
+
"#{url}wc-api/#{@version}/#{endpoint}"
|
79
|
+
end
|
80
|
+
|
81
|
+
# Internal: Requests default options.
|
82
|
+
#
|
83
|
+
# method - A String maning the request method
|
84
|
+
# endpoint - A String naming the request endpoint.
|
85
|
+
# data - The Hash data for the request.
|
86
|
+
#
|
87
|
+
# Returns the response in JSON String.
|
88
|
+
def do_request method, endpoint, data = nil
|
89
|
+
url = get_url endpoint
|
90
|
+
|
91
|
+
options = {
|
92
|
+
format: :json,
|
93
|
+
verify: @verify_ssl,
|
94
|
+
headers: {
|
95
|
+
"User-Agent" => "WooCommerce API Client-Ruby/#{WooCommerce::VERSION}",
|
96
|
+
"Content-Type" => "application/json;charset=utf-8",
|
97
|
+
"Accept" => "application/json"
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
if @is_ssl
|
102
|
+
options.merge!({
|
103
|
+
basic_auth: {
|
104
|
+
username: @consumer_key,
|
105
|
+
password: @consumer_secret
|
106
|
+
}
|
107
|
+
})
|
108
|
+
else
|
109
|
+
oauth = WooCommerce::OAuth.new url, method, @version, @consumer_key, @consumer_secret
|
110
|
+
url = oauth.get_oauth_url
|
111
|
+
end
|
112
|
+
|
113
|
+
if data
|
114
|
+
options.merge!({
|
115
|
+
body: data.to_json
|
116
|
+
})
|
117
|
+
end
|
118
|
+
|
119
|
+
HTTParty.send method, url, options
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "digest/sha1"
|
2
|
+
require "cgi"
|
3
|
+
require "uri"
|
4
|
+
require "base64"
|
5
|
+
require "openssl"
|
6
|
+
|
7
|
+
module WooCommerce
|
8
|
+
class OAuth
|
9
|
+
|
10
|
+
def initialize url, method, version, consumer_key, consumer_secret
|
11
|
+
@url = url
|
12
|
+
@method = method.upcase
|
13
|
+
@version = version
|
14
|
+
@consumer_key = consumer_key
|
15
|
+
@consumer_secret = consumer_secret
|
16
|
+
end
|
17
|
+
|
18
|
+
# Public: Get OAuth url
|
19
|
+
#
|
20
|
+
# Returns the OAuth url.
|
21
|
+
def get_oauth_url
|
22
|
+
params = {}
|
23
|
+
url = @url
|
24
|
+
|
25
|
+
if url.include?("?")
|
26
|
+
parsed_url = URI::parse(url)
|
27
|
+
CGI::parse(parsed_url.query).each do |key, value|
|
28
|
+
params[key] = value[0]
|
29
|
+
end
|
30
|
+
|
31
|
+
url = "#{parsed_url.scheme}://#{parsed_url.host}#{parsed_url.path}"
|
32
|
+
end
|
33
|
+
|
34
|
+
params["oauth_consumer_key"] = @consumer_key
|
35
|
+
params["oauth_nonce"] = Digest::SHA1.hexdigest("#{Time.new.to_i + rand(99999)}")
|
36
|
+
params["oauth_signature_method"] = "HMAC-SHA256"
|
37
|
+
params["oauth_timestamp"] = Time.new.to_i
|
38
|
+
params["oauth_signature"] = generate_oauth_signature(params, url)
|
39
|
+
|
40
|
+
query_string = URI::encode(params.map{|key, value| "#{key}=#{value}"}.join("&"))
|
41
|
+
|
42
|
+
"#{url}?#{query_string}"
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
# Internal: Generate the OAuth Signature
|
48
|
+
#
|
49
|
+
# params - A Hash with the OAuth params.
|
50
|
+
# url - A String with a URL
|
51
|
+
#
|
52
|
+
# Returns the oauth signature String.
|
53
|
+
def generate_oauth_signature params, url
|
54
|
+
base_request_uri = CGI::escape(url.to_s)
|
55
|
+
query_params = []
|
56
|
+
|
57
|
+
params.sort.map do |key, value|
|
58
|
+
query_params.push(CGI::escape(key.to_s) + "%3D" + CGI::escape(value.to_s))
|
59
|
+
end
|
60
|
+
|
61
|
+
query_string = query_params
|
62
|
+
.join("%26")
|
63
|
+
.gsub("%5B", "%255B")
|
64
|
+
.gsub("%5D", "%255D")
|
65
|
+
string_to_sign = "#{@method}&#{base_request_uri}&#{query_string}"
|
66
|
+
|
67
|
+
if @version == "v3"
|
68
|
+
consumer_secret = "#{@consumer_secret}&"
|
69
|
+
else
|
70
|
+
consumer_secret = @consumer_secret
|
71
|
+
end
|
72
|
+
|
73
|
+
return Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), consumer_secret, string_to_sign))
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: woocommerce_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Claudio Sanches
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-07-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: This gem provide a wrapper to deal with the WooCommerce REST API
|
47
|
+
email: claudio@woothemes.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files:
|
51
|
+
- README.md
|
52
|
+
- LICENSE
|
53
|
+
files:
|
54
|
+
- lib/woocommerce_api.rb
|
55
|
+
- lib/woocommerce_api/version.rb
|
56
|
+
- lib/woocommerce_api/oauth.rb
|
57
|
+
- README.md
|
58
|
+
- LICENSE
|
59
|
+
homepage: https://github.com/woothemes/wc-api-ruby
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.23
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A Ruby wrapper for the WooCommerce API
|
85
|
+
test_files: []
|