Class: RuWoCo
- Inherits:
-
Object
- Object
- RuWoCo
- Defined in:
- lib/ruwoco.rb
Constant Summary collapse
- API_ENDPOINT =
"wc-api/v1/"
- HTTP_GET =
"GET"
- HTTP_POST =
"POST"
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#is_ssl ⇒ Object
Returns the value of attribute is_ssl.
Instance Method Summary collapse
- #generate_oauth_signature(endpoint, params, method) ⇒ Object
- #get_coupon(coupon_id) ⇒ Object
- #get_coupon_by_code(coupon_code) ⇒ Object
- #get_coupons(params = {}) ⇒ Object
- #get_coupons_count ⇒ Object
- #get_customer(customer_id) ⇒ Object
- #get_customer_by_email(email) ⇒ Object
- #get_customer_orders(customer_id) ⇒ Object
- #get_customers(params = {}) ⇒ Object
- #get_customers_count ⇒ Object
- #get_index ⇒ Object
- #get_order(order_id) ⇒ Object
- #get_order_notes(order_id) ⇒ Object
- #get_orders(params = {}) ⇒ Object
- #get_orders_count ⇒ Object
- #get_product(product_id) ⇒ Object
- #get_products ⇒ Object
- #get_products_count ⇒ Object
- #get_reports(params = {}) ⇒ Object
- #get_sales_report(params = {}) ⇒ Object
- #get_top_sellers_report(params = {}) ⇒ Object
-
#initialize(consumer_key, consumer_secret, store_url, is_ssl = true) ⇒ RuWoCo
constructor
A new instance of RuWoCo.
- #make_api_call(endpoint, params = {}, method = 'GET') ⇒ Object
- #update_order(order_id, params = {}) ⇒ Object
Constructor Details
#initialize(consumer_key, consumer_secret, store_url, is_ssl = true) ⇒ RuWoCo
Returns a new instance of RuWoCo.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruwoco.rb', line 16 def initialize(consumer_key, consumer_secret, store_url, is_ssl = true) if consumer_key && consumer_secret && store_url @consumer_key = consumer_key @consumer_secret = consumer_secret @api_url = store_url + API_ENDPOINT @is_ssl = is_ssl elsif !consumer_key raise "Consumer key missing" elsif !consumer_secret raise "Consumer secret missing" else raise "Store URL missing" end end |
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
10 11 12 |
# File 'lib/ruwoco.rb', line 10 def api_url @api_url end |
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
10 11 12 |
# File 'lib/ruwoco.rb', line 10 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
10 11 12 |
# File 'lib/ruwoco.rb', line 10 def consumer_secret @consumer_secret end |
#is_ssl ⇒ Object
Returns the value of attribute is_ssl.
10 11 12 |
# File 'lib/ruwoco.rb', line 10 def is_ssl @is_ssl end |
Instance Method Details
#generate_oauth_signature(endpoint, params, method) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ruwoco.rb', line 147 def generate_oauth_signature(endpoint, params, method) base_request_uri = CGI::escape(@api_url + endpoint) query_params = [] params.each { |key, value| query_params.push(CGI::escape(key) + "%3D" + CGI::escape(value)) } query_string = "%26".join(query_params) string_to_sign = method + "&" + base_request_uri + '&' + query_string return Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @consumer_secret, string_to_sign)) end |
#get_coupon(coupon_id) ⇒ Object
59 60 61 |
# File 'lib/ruwoco.rb', line 59 def get_coupon(coupon_id) return make_api_call("coupons/#{coupon_id}") end |
#get_coupon_by_code(coupon_code) ⇒ Object
67 68 69 |
# File 'lib/ruwoco.rb', line 67 def get_coupon_by_code(coupon_code) return make_api_call("coupons/code/#{coupon_code}") end |
#get_coupons(params = {}) ⇒ Object
55 56 57 |
# File 'lib/ruwoco.rb', line 55 def get_coupons(params = {}) return make_api_call("coupons", params) end |
#get_coupons_count ⇒ Object
63 64 65 |
# File 'lib/ruwoco.rb', line 63 def get_coupons_count() return make_api_call("coupons/count") end |
#get_customer(customer_id) ⇒ Object
75 76 77 |
# File 'lib/ruwoco.rb', line 75 def get_customer(customer_id) return make_api_call("customers/#{customer_id}") end |
#get_customer_by_email(email) ⇒ Object
79 80 81 |
# File 'lib/ruwoco.rb', line 79 def get_customer_by_email(email) return make_api_call("customers/email/#{email}") end |
#get_customer_orders(customer_id) ⇒ Object
87 88 89 |
# File 'lib/ruwoco.rb', line 87 def get_customer_orders(customer_id) return make_api_call("customers/#{customer_id}/orders") end |
#get_customers(params = {}) ⇒ Object
71 72 73 |
# File 'lib/ruwoco.rb', line 71 def get_customers(params = {}) return make_api_call("customers", params) end |
#get_customers_count ⇒ Object
83 84 85 |
# File 'lib/ruwoco.rb', line 83 def get_customers_count() return make_api_call("customers/count") end |
#get_index ⇒ Object
31 32 33 |
# File 'lib/ruwoco.rb', line 31 def get_index() return make_api_call() end |
#get_order(order_id) ⇒ Object
39 40 41 |
# File 'lib/ruwoco.rb', line 39 def get_order(order_id) return make_api_call("orders/#{order_id}") end |
#get_order_notes(order_id) ⇒ Object
47 48 49 |
# File 'lib/ruwoco.rb', line 47 def get_order_notes(order_id) return make_api_call("orders/#{order_id}/notes") end |
#get_orders(params = {}) ⇒ Object
35 36 37 |
# File 'lib/ruwoco.rb', line 35 def get_orders(params = {}) return make_api_call("orders", params) end |
#get_orders_count ⇒ Object
43 44 45 |
# File 'lib/ruwoco.rb', line 43 def get_orders_count() return make_api_call("orders/count") end |
#get_product(product_id) ⇒ Object
95 96 97 |
# File 'lib/ruwoco.rb', line 95 def get_product(product_id) return make_api_call("products/#{product_id}") end |
#get_products ⇒ Object
91 92 93 |
# File 'lib/ruwoco.rb', line 91 def get_products() return make_api_call("products") end |
#get_products_count ⇒ Object
99 100 101 |
# File 'lib/ruwoco.rb', line 99 def get_products_count() return make_api_call("products/count") end |
#get_reports(params = {}) ⇒ Object
103 104 105 |
# File 'lib/ruwoco.rb', line 103 def get_reports(params = {}) return make_api_call("reports") end |
#get_sales_report(params = {}) ⇒ Object
107 108 109 |
# File 'lib/ruwoco.rb', line 107 def get_sales_report(params = {}) return make_api_call("reports/sales", params) end |
#get_top_sellers_report(params = {}) ⇒ Object
111 112 113 |
# File 'lib/ruwoco.rb', line 111 def get_top_sellers_report(params = {}) return make_api_call("reports/sales/top_sellers", params) end |
#make_api_call(endpoint, params = {}, method = 'GET') ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ruwoco.rb', line 115 def make_api_call(endpoint, params = {}, method='GET') if (!@is_ssl) params[:oauth_consumer_key] = @consumer_key params[:oauth_nonce] = Digest::SHA1.hexdigest(Time.new.to_i) params[:oauth_signature_method] = 'HMAC-256' params[:oauth_timestamp] = Time.new.to_i params[:oauth_signature] = generate_oauth_signature(endpoint, params, method) end if (method == HTTP_GET) query = URI.encode_www_form(params) uri = URI(@api_url + endpoint + '?' + query) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true req = Net::HTTP::Get.new(uri.request_uri) req.basic_auth(@consumer_key, @consumer_secret) res = http.start { |test| test.request(req) } elsif (method == HTTP_POST) url = URI.parse(@api_url + endpoint) req = Net::HTTP::Post.new(url.path) req.basic_auth(@consumer_key, @consumer_secret) req.body = params.to_json sock = Net::HTTP.new(url.host, url.port) sock.use_ssl = true res = sock.start { |test| test.request(req) } else raise "Unsupported HTTP operation requested" end return res.body.to_json() end |
#update_order(order_id, params = {}) ⇒ Object
51 52 53 |
# File 'lib/ruwoco.rb', line 51 def update_order(order_id, params = {}) return make_api_call("orders/#{order_id}", params, 'POST') end |