Class: Squarespace::Client
- Inherits:
-
Object
- Object
- Squarespace::Client
- Defined in:
- lib/squarespace/client.rb
Constant Summary collapse
- COMMERCE_API_VERSION =
0.1
Instance Attribute Summary collapse
-
#commerce_url ⇒ Object
readonly
Returns the value of attribute commerce_url.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #commerce_request(method, route = '', headers = {}, parameters = {}, body = nil) ⇒ Object
- #fulfill_order(order_id, shipments, send_notification = true) ⇒ Object
- #get_fulfilled_orders ⇒ Object
- #get_order(id) ⇒ Object
- #get_orders(fulfillment_status = nil) ⇒ Object
- #get_pending_orders ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 |
# File 'lib/squarespace/client.rb', line 12 def initialize(={}) @logger = Logger.new(STDOUT) @logger.level = .delete('log_level') || 'INFO' @config = Squarespace::Config.new() @commerce_url = "#{@config.api_url}/#{COMMERCE_API_VERSION}/commerce/orders" end |
Instance Attribute Details
#commerce_url ⇒ Object (readonly)
Returns the value of attribute commerce_url.
8 9 10 |
# File 'lib/squarespace/client.rb', line 8 def commerce_url @commerce_url end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/squarespace/client.rb', line 8 def logger @logger end |
Instance Method Details
#commerce_request(method, route = '', headers = {}, parameters = {}, body = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/squarespace/client.rb', line 83 def commerce_request(method, route='', headers={}, parameters={}, body=nil) response = connection(@commerce_url).send(method.downcase) do |req| if method.eql?('post') req.headers['Content-Type'] = 'application/json' end parameters.each { |k,v| req.params["#{k}"] = v } if parameters.any? headers.each { |k,v| req.headers["#{k}"] = v } if headers.any? # We always need an Authorization header req.headers['Authorization'] = "Bearer #{@config.api_key}" req.url route req.body = body unless body.nil? end end |
#fulfill_order(order_id, shipments, send_notification = true) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/squarespace/client.rb', line 46 def fulfill_order(order_id, shipments, send_notification=true) # fulfill_order(string, array[hash], boolean) # # Shipment array example: # # [{ # tracking_number: 'test_tracking_number1', # tracking_url: 'https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=test_tracking_number2', # carrier_name: 'USPS', # service: 'ground' # },{ # tracking_number: 'test_tracking_number2', # tracking_url: nil, # carrier_name: 'USPS', # service: 'prioritt' # }] shipments_arry = [] shipments.each do |shipment| shipments_arry << { "carrierName": shipment[:carrier_name], "service": shipment[:service], "shipDate": "#{Time.now.utc.iso8601}", "trackingNumber": shipment[:tracking_number], "trackingUrl": shipment[:tracking_url] } end request_body = { "shipments": shipments_arry, "shouldSendNotification": send_notification } commerce_request('post', "#{order_id}/fulfillments", {}, {}, request_body.to_json) end |
#get_fulfilled_orders ⇒ Object
42 43 44 |
# File 'lib/squarespace/client.rb', line 42 def get_fulfilled_orders get_orders('fulfilled') end |
#get_order(id) ⇒ Object
19 20 21 22 23 |
# File 'lib/squarespace/client.rb', line 19 def get_order(id) order_response = commerce_request('get', id.to_s) logger.debug("Order response: #{order_response.body}") Order.new(JSON.parse(order_response.body)) end |
#get_orders(fulfillment_status = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/squarespace/client.rb', line 25 def get_orders(fulfillment_status = nil) if fulfillment_status.nil? order_response = commerce_request('get') else # binding.pry order_response = commerce_request('get', '', {}, {"fulfillmentStatus"=>fulfillment_status.upcase}) end logger.debug("Order response: #{order_response.body}") Order.new(JSON.parse(order_response.body)) end |
#get_pending_orders ⇒ Object
38 39 40 |
# File 'lib/squarespace/client.rb', line 38 def get_pending_orders get_orders('pending') end |