Class: Squarespace::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/squarespace/client.rb

Constant Summary collapse

COMMERCE_API_VERSION =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
# File 'lib/squarespace/client.rb', line 13

def initialize(options={})
  @logger = Logger.new(STDOUT)
  if ENV['LOG_LEVEL'].nil?
    @logger.level = options.delete('log_level') || 'INFO'
  else
    @logger.level = ENV['LOG_LEVEL']
  end
  @config = Squarespace::Config.new(options)
  @commerce_url = "#{@config.api_url}/#{COMMERCE_API_VERSION}/commerce/orders"
end

Instance Attribute Details

#commerce_urlObject (readonly)

Returns the value of attribute commerce_url.



9
10
11
# File 'lib/squarespace/client.rb', line 9

def commerce_url
  @commerce_url
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/squarespace/client.rb', line 9

def logger
  @logger
end

Instance Method Details

#check_response_status(code) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/squarespace/client.rb', line 55

def check_response_status(code)
  case code
  when 200
    return code
  when 400
    raise Squarespace::Errors::BadRequest
  when 401
    raise Squarespace::Errors::Unauthorized
  when 404
    raise Squarespace::Errors::NotFound
  when 405
    raise Squarespace::Errors::MethodNotAllowed
  when 429
    raise Squarespace::Errors::TooManyRequests
  else
    return code
  end
end

#commerce_request(method, route = '', headers = {}, parameters = {}, body = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/squarespace/client.rb', line 119

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/squarespace/client.rb', line 82

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_ordersObject



78
79
80
# File 'lib/squarespace/client.rb', line 78

def get_fulfilled_orders
  get_orders('fulfilled')
end

#get_order(id) ⇒ Object



24
25
26
27
28
# File 'lib/squarespace/client.rb', line 24

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/squarespace/client.rb', line 30

def get_orders(fulfillment_status = nil)
  if fulfillment_status.nil?
    order_response = commerce_request('get')
  else
    order_response = commerce_request('get', '', {}, 
      {"fulfillmentStatus"=>fulfillment_status.upcase})
  end

  logger.debug("Order response: #{order_response.body}")
  check_response_status(order_response.status)
  parsed_body = parse_commerce_response_body(order_response.body)
  
  Order.new(parsed_body)
end

#get_pending_ordersObject



74
75
76
# File 'lib/squarespace/client.rb', line 74

def get_pending_orders
  get_orders('pending')
end

#parse_commerce_response_body(body) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/squarespace/client.rb', line 45

def parse_commerce_response_body(body)
  begin
    parsed_response = JSON.parse(body)
  rescue JSON::ParserError => e
    logger.error("Error parsing response body as JSON: #{body}")
    raise e
  end
  return parsed_response
end