├── .editorconfig ├── .gitignore ├── .travis.yml ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── Rakefile ├── lib ├── woocommerce_api.rb └── woocommerce_api │ ├── oauth.rb │ └── version.rb ├── test └── test.rb └── woocommerce_api.gemspec /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | tab_width = 2 9 | indent_style = space 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | 16 | [*.txt] 17 | trim_trailing_whitespace = false 18 | 19 | [*.json] 20 | insert_final_newline = false 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | run.rb 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.0.0 4 | - 2.1.5 5 | - 2.2.1 6 | - 2.3.0 7 | - ruby-head 8 | script: bundle exec rake test 9 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "httparty" 4 | gem "json" 5 | 6 | group :test do 7 | gem "rake" 8 | gem "minitest" 9 | gem "fakeweb" 10 | end 11 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | fakeweb (1.3.0) 5 | httparty (0.14.0) 6 | multi_xml (>= 0.5.2) 7 | json (2.0.2) 8 | minitest (5.10.1) 9 | multi_xml (0.6.0) 10 | rake (12.0.0) 11 | 12 | PLATFORMS 13 | ruby 14 | 15 | DEPENDENCIES 16 | fakeweb 17 | httparty 18 | json 19 | minitest 20 | rake 21 | 22 | BUNDLED WITH 23 | 1.12.5 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | [![build status](https://secure.travis-ci.org/woocommerce/wc-api-ruby.svg)](http://travis-ci.org/woocommerce/wc-api-ruby) 6 | [![gem version](https://badge.fury.io/rb/woocommerce_api.svg)](https://rubygems.org/gems/woocommerce_api) 7 | 8 | ## Installation 9 | 10 | ``` 11 | gem install woocommerce_api 12 | ``` 13 | 14 | ## Getting started 15 | 16 | Generate API credentials (Consumer Key & Consumer Secret) following this instructions 17 | . 18 | 19 | Check out the WooCommerce API endpoints and data that can be manipulated in . 20 | 21 | ## Setup 22 | 23 | Setup for the new WP REST API integration (WooCommerce 2.6 or later): 24 | 25 | ```ruby 26 | require "woocommerce_api" 27 | 28 | woocommerce = WooCommerce::API.new( 29 | "http://example.com", 30 | "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 31 | "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 32 | { 33 | wp_api: true, 34 | version: "wc/v1" 35 | } 36 | ) 37 | ``` 38 | 39 | Setup for the WooCommerce legacy API: 40 | 41 | ```ruby 42 | require "woocommerce_api" 43 | 44 | woocommerce = WooCommerce::API.new( 45 | "http://example.com", 46 | "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 47 | "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 48 | { 49 | version: "v3" 50 | } 51 | ) 52 | ``` 53 | 54 | ### Options 55 | 56 | | Option | Type | Required | Description | 57 | | ----------------- | -------- | -------- | ---------------------------------------- | 58 | | `url` | `String` | yes | Your Store URL, example: http://woo.dev/ | 59 | | `consumer_key` | `String` | yes | Your API consumer key | 60 | | `consumer_secret` | `String` | yes | Your API consumer secret | 61 | | `args` | `Hash` | no | Extra arguments (see Args options table) | 62 | 63 | #### Args options 64 | 65 | | Option | Type | Required | Description | 66 | |---------------------|----------|----------|--------------------------------------------------------------------------------------------------------------| 67 | | `wp_api` | `Bool` | no | Allow requests to the WP REST API (WooCommerce 2.6 or later) | 68 | | `version` | `String` | no | API version, default is `v3` | 69 | | `verify_ssl` | `Bool` | no | Verify SSL when connect, use this option as `false` when need to test with self-signed certificates | 70 | | `signature_method` | `String` | no | Signature method used for oAuth requests, works with `HMAC-SHA1` and `HMAC-SHA256`, default is `HMAC-SHA256` | 71 | | `query_string_auth` | `Bool` | no | Force Basic Authentication as query string when `true` and using under HTTPS, default is `false` | 72 | | `debug_mode` | `Bool` | no | Enables HTTParty debug mode | 73 | | `httparty_args` | `Hash` | no | Allows extra HTTParty args | 74 | 75 | ## Methods 76 | 77 | | Params | Type | Description | 78 | | ---------- | -------- | ------------------------------------------------------------ | 79 | | `endpoint` | `String` | WooCommerce API endpoint, example: `customers` or `order/12` | 80 | | `data` | `Hash` | Only for POST and PUT, data that will be converted to JSON | 81 | | `query` | `Hash` | Only for GET and DELETE, request query string | 82 | 83 | ### GET 84 | 85 | - `.get(endpoint, query)` 86 | 87 | ### POST 88 | 89 | - `.post(endpoint, data)` 90 | 91 | ### PUT 92 | 93 | - `.put(endpoint, data)` 94 | 95 | ### DELETE 96 | 97 | - `.delete(endpoint, query)` 98 | 99 | ### OPTIONS 100 | 101 | - `.options(endpoint)` 102 | 103 | #### Response 104 | 105 | All methods will return [HTTParty::Response](https://github.com/jnunemaker/httparty) object. 106 | 107 | ```ruby 108 | response = api.get "customers" 109 | 110 | puts response.parsed_response # A Hash of the parsed JSON response 111 | # Example: {"customers"=>[{"id"=>8, "created_at"=>"2015-05-06T17:43:51Z", "email"=> 112 | 113 | puts response.code # A Integer of the HTTP code response 114 | # Example: 200 115 | 116 | puts response.headers["x-wc-total"] # Total of items 117 | # Example: 2 118 | 119 | puts response.headers["x-wc-totalpages"] # Total of pages 120 | # Example: 1 121 | ``` 122 | 123 | ## Release History 124 | 125 | - 2016-12-14 - 1.4.0 - Introduces `httparty_args` arg and fixed compatibility with WordPress 4.7. 126 | - 2016-09-15 - 1.3.0 - Added the `query_string_auth` and `debug_mode` options. 127 | - 2016-06-26 - 1.2.1 - Fixed oAuth signature for WP REST API. 128 | - 2016-05-09 - 1.2.0 - Added support for WP REST API and added method to do HTTP OPTIONS requests. 129 | - 2015-12-07 - 1.1.2 - Stop send `body` in GET and DELETE requests. 130 | - 2015-12-07 - 1.1.1 - Fixed the encode when have spaces in the URL parameters. 131 | - 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 132 | - 2015-08-27 - 1.0.3 - Encode all % characters in query string for OAuth 1.0a. 133 | - 2015-08-12 - 1.0.2 - Fixed the release date. 134 | - 2015-08-12 - 1.0.1 - Escaped oauth_signature in url query string. 135 | - 2015-07-15 - 1.0.0 - Stable release. 136 | - 2015-07-15 - 0.0.1 - Beta release. 137 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "rake/testtask" 2 | 3 | Rake::TestTask.new do |t| 4 | t.libs << "test" 5 | end 6 | 7 | desc "Run tests" 8 | task :default => :test 9 | -------------------------------------------------------------------------------- /lib/woocommerce_api.rb: -------------------------------------------------------------------------------- 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 = { 18 | wp_api: false, 19 | version: "v3", 20 | verify_ssl: true, 21 | signature_method: "HMAC-SHA256", 22 | httparty_args: {} 23 | } 24 | args = defaults.merge(args) 25 | 26 | @wp_api = args[:wp_api] 27 | @version = args[:version] 28 | @verify_ssl = args[:verify_ssl] == true 29 | @signature_method = args[:signature_method] 30 | @debug_mode = args[:debug_mode] 31 | @query_string_auth = args[:query_string_auth] 32 | @httparty_args = args[:httparty_args] 33 | 34 | # Internal args 35 | @is_ssl = @url.start_with? "https" 36 | end 37 | 38 | # Public: GET requests. 39 | # 40 | # endpoint - A String naming the request endpoint. 41 | # query - A Hash with query params. 42 | # 43 | # Returns the request Hash. 44 | def get endpoint, query = nil 45 | do_request :get, add_query_params(endpoint, query) 46 | end 47 | 48 | # Public: POST requests. 49 | # 50 | # endpoint - A String naming the request endpoint. 51 | # data - The Hash data for the request. 52 | # 53 | # Returns the request Hash. 54 | def post endpoint, data 55 | do_request :post, endpoint, data 56 | end 57 | 58 | # Public: PUT requests. 59 | # 60 | # endpoint - A String naming the request endpoint. 61 | # data - The Hash data for the request. 62 | # 63 | # Returns the request Hash. 64 | def put endpoint, data 65 | do_request :put, endpoint, data 66 | end 67 | 68 | # Public: DELETE requests. 69 | # 70 | # endpoint - A String naming the request endpoint. 71 | # query - A Hash with query params. 72 | # 73 | # Returns the request Hash. 74 | def delete endpoint, query = nil 75 | do_request :delete, add_query_params(endpoint, query) 76 | end 77 | 78 | # Public: OPTIONS requests. 79 | # 80 | # endpoint - A String naming the request endpoint. 81 | # 82 | # Returns the request Hash. 83 | def options endpoint 84 | do_request :options, add_query_params(endpoint, nil) 85 | end 86 | 87 | protected 88 | 89 | # Internal: Appends data as query params onto an endpoint 90 | # 91 | # endpoint - A String naming the request endpoint. 92 | # data - A hash of data to flatten and append 93 | # 94 | # Returns an endpoint string with the data appended 95 | def add_query_params endpoint, data 96 | return endpoint if data.nil? || data.empty? 97 | 98 | endpoint += "?" unless endpoint.include? "?" 99 | endpoint += "&" unless endpoint.end_with? "?" 100 | endpoint + URI.encode(flatten_hash(data).join("&")) 101 | end 102 | 103 | # Internal: Get URL for requests 104 | # 105 | # endpoint - A String naming the request endpoint. 106 | # method - The method used in the url (for oauth querys) 107 | # 108 | # Returns the endpoint String. 109 | def get_url endpoint, method 110 | api = @wp_api ? 'wp-json' : 'wc-api' 111 | url = @url 112 | url = "#{url}/" unless url.end_with? "/" 113 | url = "#{url}#{api}/#{@version}/#{endpoint}" 114 | 115 | @is_ssl ? url : oauth_url(url, method) 116 | end 117 | 118 | # Internal: Requests default options. 119 | # 120 | # method - A String naming the request method 121 | # endpoint - A String naming the request endpoint. 122 | # data - The Hash data for the request. 123 | # 124 | # Returns the response in JSON String. 125 | def do_request method, endpoint, data = {} 126 | url = get_url(endpoint, method) 127 | options = { 128 | format: :json 129 | } 130 | 131 | # Allow custom HTTParty args. 132 | options = options.merge(@httparty_args) 133 | 134 | # Set headers. 135 | options[:headers] = { 136 | "User-Agent" => "WooCommerce API Client-Ruby/#{WooCommerce::VERSION}", 137 | "Accept" => "application/json" 138 | } 139 | options[:headers]["Content-Type"] = "application/json;charset=utf-8" if !data.empty? 140 | 141 | # Set basic authentication. 142 | if @is_ssl 143 | options[:verify] = @verify_ssl 144 | 145 | if @query_string_auth 146 | options.merge!(query: { 147 | consumer_key: @consumer_key, 148 | consumer_secret: @consumer_secret 149 | }) 150 | else 151 | options.merge!(basic_auth: { 152 | username: @consumer_key, 153 | password: @consumer_secret 154 | }) 155 | end 156 | end 157 | 158 | options.merge!(debug_output: $stdout) if @debug_mode 159 | options.merge!(body: data.to_json) if !data.empty? 160 | 161 | HTTParty.send(method, url, options) 162 | end 163 | 164 | # Internal: Generates an oauth url given current settings 165 | # 166 | # url - A String naming the current request url 167 | # method - The HTTP verb of the request 168 | # 169 | # Returns a url to be used for the query. 170 | def oauth_url url, method 171 | oauth = WooCommerce::OAuth.new( 172 | url, 173 | method, 174 | @version, 175 | @consumer_key, 176 | @consumer_secret, 177 | @signature_method 178 | ) 179 | oauth.get_oauth_url 180 | end 181 | 182 | # Internal: Flattens a hash into an array of query relations 183 | # 184 | # hash - A hash to flatten 185 | # 186 | # Returns an array full of key value paired strings 187 | def flatten_hash hash 188 | hash.flat_map do |key, value| 189 | case value 190 | when Hash 191 | value.map do |inner_key, inner_value| 192 | "#{key}[#{inner_key}]=#{inner_value}" 193 | end 194 | when Array 195 | value.map { |inner_value| "#{key}[]=#{inner_value}" } 196 | else 197 | "#{key}=#{value}" 198 | end 199 | end 200 | end 201 | end 202 | end 203 | -------------------------------------------------------------------------------- /lib/woocommerce_api/oauth.rb: -------------------------------------------------------------------------------- 1 | require "digest/sha1" 2 | require "cgi" 3 | require "uri" 4 | require "base64" 5 | require "openssl" 6 | 7 | module WooCommerce 8 | class OAuth 9 | class InvalidSignatureMethodError < StandardError; end 10 | 11 | def initialize url, method, version, consumer_key, consumer_secret, signature_method = "HMAC-SHA256" 12 | @url = url 13 | @method = method.upcase 14 | @version = version 15 | @consumer_key = consumer_key 16 | @consumer_secret = consumer_secret 17 | @signature_method = signature_method 18 | end 19 | 20 | # Public: Get OAuth url 21 | # 22 | # Returns the OAuth url. 23 | def get_oauth_url 24 | params = {} 25 | url = @url 26 | 27 | if url.include?("?") 28 | parsed_url = URI::parse(url) 29 | CGI::parse(parsed_url.query).each do |key, value| 30 | params[key] = value[0] 31 | end 32 | params = Hash[params.sort] 33 | 34 | url = parsed_url.to_s.gsub(/\?.*/, "") 35 | end 36 | 37 | nonce_lifetime = 15 * 60 # Woocommerce keeps nonces for 15 minutes 38 | 39 | params["oauth_consumer_key"] = @consumer_key 40 | params["oauth_nonce"] = Digest::SHA1.hexdigest((Time.new.to_f % nonce_lifetime + (Process.pid * nonce_lifetime)).to_s) 41 | params["oauth_signature_method"] = @signature_method 42 | params["oauth_timestamp"] = Time.new.to_i 43 | params["oauth_signature"] = CGI::escape(generate_oauth_signature(params, url)) 44 | 45 | query_string = URI::encode(params.map{|key, value| "#{key}=#{value}"}.join("&")) 46 | 47 | "#{url}?#{query_string}" 48 | end 49 | 50 | protected 51 | 52 | # Internal: Generate the OAuth Signature 53 | # 54 | # params - A Hash with the OAuth params. 55 | # url - A String with a URL 56 | # 57 | # Returns the oauth signature String. 58 | def generate_oauth_signature params, url 59 | base_request_uri = CGI::escape(url.to_s) 60 | query_params = [] 61 | 62 | params.sort.map do |key, value| 63 | query_params.push(encode_param(key.to_s) + "%3D" + encode_param(value.to_s)) 64 | end 65 | 66 | query_string = query_params 67 | .join("%26") 68 | string_to_sign = "#{@method}&#{base_request_uri}&#{query_string}" 69 | 70 | if !["v1", "v2"].include? @version 71 | consumer_secret = "#{@consumer_secret}&" 72 | else 73 | consumer_secret = @consumer_secret 74 | end 75 | 76 | return Base64.strict_encode64(OpenSSL::HMAC.digest(digest, consumer_secret, string_to_sign)) 77 | end 78 | 79 | # Internal: Digest object based on signature method 80 | # 81 | # Returns a digest object. 82 | def digest 83 | case @signature_method 84 | when "HMAC-SHA256" then OpenSSL::Digest.new("sha256") 85 | when "HMAC-SHA1" then OpenSSL::Digest.new("sha1") 86 | else 87 | fail InvalidSignatureMethodError 88 | end 89 | end 90 | 91 | # Internal: Encode param 92 | # 93 | # text - A String to be encoded 94 | # 95 | # Returns the encoded String. 96 | def encode_param text 97 | CGI::escape(text).gsub("+", "%20").gsub("%", "%25") 98 | end 99 | end 100 | end 101 | -------------------------------------------------------------------------------- /lib/woocommerce_api/version.rb: -------------------------------------------------------------------------------- 1 | module WooCommerce 2 | VERSION = '1.4.0' 3 | end 4 | -------------------------------------------------------------------------------- /test/test.rb: -------------------------------------------------------------------------------- 1 | require "minitest/autorun" 2 | require "fakeweb" 3 | require "json" 4 | require "woocommerce_api" 5 | 6 | class WooCommerceAPITest < Minitest::Test 7 | def setup 8 | @basic_auth = WooCommerce::API.new( 9 | "https://dev.test/", 10 | "user", 11 | "pass" 12 | ) 13 | 14 | @oauth = WooCommerce::API.new( 15 | "http://dev.test/", 16 | "user", 17 | "pass" 18 | ) 19 | end 20 | 21 | def test_basic_auth_get 22 | FakeWeb.register_uri(:get, "https://user:pass@dev.test/wc-api/v3/customers", 23 | body: '{"customers":[]}', 24 | content_type: "application/json" 25 | ) 26 | response = @basic_auth.get "customers" 27 | 28 | assert_equal 200, response.code 29 | end 30 | 31 | def test_oauth_get 32 | FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, 33 | body: '{"customers":[]}', 34 | content_type: "application/json" 35 | ) 36 | response = @oauth.get "customers" 37 | 38 | assert_equal 200, response.code 39 | end 40 | 41 | def test_oauth_get_puts_data_in_alpha_order 42 | FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?abc=123&oauth_consumer_key=user&oauth_d=456&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)&xyz=789/, 43 | body: '{"customers":[]}', 44 | content_type: "application/json" 45 | ) 46 | response = @oauth.get "customers", abc: '123', oauth_d: '456', xyz: '789' 47 | 48 | assert_equal 200, response.code 49 | end 50 | 51 | def test_basic_auth_post 52 | FakeWeb.register_uri(:post, "https://user:pass@dev.test/wc-api/v3/products", 53 | body: '{"products":[]}', 54 | content_type: "application/json", 55 | status: ["201", "Created"] 56 | ) 57 | 58 | data = { 59 | product: { 60 | title: "Testing product" 61 | } 62 | } 63 | response = @basic_auth.post "products", data 64 | 65 | assert_equal 201, response.code 66 | end 67 | 68 | def test_oauth_post 69 | FakeWeb.register_uri(:post, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, 70 | body: '{"products":[]}', 71 | content_type: "application/json", 72 | status: ["201", "Created"] 73 | ) 74 | 75 | data = { 76 | product: { 77 | title: "Testing product" 78 | } 79 | } 80 | response = @oauth.post "products", data 81 | 82 | assert_equal 201, response.code 83 | end 84 | 85 | def test_basic_auth_put 86 | FakeWeb.register_uri(:put, "https://user:pass@dev.test/wc-api/v3/products/1234", 87 | body: '{"customers":[]}', 88 | content_type: "application/json" 89 | ) 90 | 91 | data = { 92 | product: { 93 | title: "Updating product title" 94 | } 95 | } 96 | response = @basic_auth.put "products/1234", data 97 | 98 | assert_equal 200, response.code 99 | end 100 | 101 | def test_oauth_put 102 | FakeWeb.register_uri(:put, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, 103 | body: '{"products":[]}', 104 | content_type: "application/json" 105 | ) 106 | 107 | data = { 108 | product: { 109 | title: "Updating product title" 110 | } 111 | } 112 | response = @oauth.put "products", data 113 | 114 | assert_equal 200, response.code 115 | end 116 | 117 | def test_basic_auth_delete 118 | FakeWeb.register_uri(:delete, "https://user:pass@dev.test/wc-api/v3/products/1234?force=true", 119 | body: '{"message":"Permanently deleted product"}', 120 | content_type: "application/json", 121 | status: ["202", "Accepted"] 122 | ) 123 | 124 | response = @basic_auth.delete "products/1234?force=true" 125 | 126 | assert_equal 202, response.code 127 | assert_equal '{"message":"Permanently deleted product"}', response.to_json 128 | end 129 | 130 | def test_basic_auth_delete_params 131 | FakeWeb.register_uri(:delete, "https://user:pass@dev.test/wc-api/v3/products/1234?force=true", 132 | body: '{"message":"Permanently deleted product"}', 133 | content_type: "application/json", 134 | status: ["202", "Accepted"] 135 | ) 136 | 137 | response = @basic_auth.delete "products/1234", force: true 138 | 139 | assert_equal 202, response.code 140 | assert_equal '{"message":"Permanently deleted product"}', response.to_json 141 | end 142 | 143 | def test_oauth_put 144 | FakeWeb.register_uri(:delete, /http:\/\/dev\.test\/wc-api\/v3\/products\/1234\?force=true&oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, 145 | body: '{"message":"Permanently deleted product"}', 146 | content_type: "application/json", 147 | status: ["202", "Accepted"] 148 | ) 149 | 150 | response = @oauth.delete "products/1234?force=true" 151 | 152 | assert_equal 202, response.code 153 | assert_equal '{"message":"Permanently deleted product"}', response.to_json 154 | end 155 | 156 | def test_adding_query_params 157 | url = @oauth.send(:add_query_params, 'foo.com', filter: { sku: '123' }, order: 'created_at') 158 | assert_equal url, URI.encode('foo.com?filter[sku]=123&order=created_at') 159 | end 160 | 161 | def test_invalid_signature_method 162 | assert_raises WooCommerce::OAuth::InvalidSignatureMethodError do 163 | client = WooCommerce::API.new("http://dev.test/", "user", "pass", signature_method: 'GARBAGE') 164 | client.get 'products' 165 | end 166 | end 167 | end 168 | -------------------------------------------------------------------------------- /woocommerce_api.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path("../lib", __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require "rubygems" 5 | require "woocommerce_api/version" 6 | 7 | 8 | Gem::Specification.new do |s| 9 | s.name = "woocommerce_api" 10 | s.version = WooCommerce::VERSION 11 | s.date = "2016-12-14" 12 | 13 | s.summary = "A Ruby wrapper for the WooCommerce API" 14 | s.description = "This gem provide a wrapper to deal with the WooCommerce REST API" 15 | s.license = "MIT" 16 | 17 | s.authors = ["Claudio Sanches"] 18 | s.email = "claudio@automattic.com" 19 | s.files = Dir["lib/woocommerce_api.rb", "lib/woocommerce_api/*.rb"] 20 | s.homepage = "https://github.com/woocommerce/wc-api-ruby" 21 | 22 | s.rdoc_options = ["--charset=UTF-8"] 23 | s.extra_rdoc_files = %w[README.md LICENSE] 24 | 25 | s.add_runtime_dependency "httparty", "~> 0.14", ">= 0.14.0" 26 | s.add_runtime_dependency "json", "~> 2.0", ">= 2.0.0" 27 | end 28 | --------------------------------------------------------------------------------