-
Notifications
You must be signed in to change notification settings - Fork 0
/
submitorder.rb
99 lines (84 loc) · 2.55 KB
/
submitorder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require './oneflowclient'
require './models/order'
require './models/item'
require './models/component'
require './models/shipment'
require './models/carrier'
require './models/address'
require 'json'
#initialise the sdk
endpoint = "ONEFLOW-ENDPOINT" # Don't forget the /api at the end of the endpoint
token = "ONEFLOW-TOKEN"
secret = "ONEFLOW-SECRET"
#order settings
destination = "DESTINATION-ACCOUNT"
orderId = "order-"+rand(1000000).to_s
itemId = orderId + "-1"
fetchUrl = "FETCHURL"
quantity = 1
skuCode = "SKU-CODE"
componentCode = "COMPONENT-CODE"
carrierCode = "CARRIER-CODE"
carrierService = "CARRIER-SERVICE"
name = "Nigel Watson"
address1 = "999 Letsbe Avenue"
town = "London"
postcode = "EC2N 0BJ"
isoCountry = "GB"
# Retry options
class Options
attr_accessor :retries
def initialize()
@retries = 3
end
def retryCondition(response)
return response.code != 200 # retry everything but OK
end
def retryDelay(response, retryCount)
sleep 1 # just delay 1 sec
end
end
#create the onelflow client. Pass Options.new as 4th argument and customize logic or skip for defaults (exponential retry in case of 429)
client = OneflowClient.new(endpoint, token, secret)
# No need to edit below here
order = client.create_order(destination)
order.orderData.sourceOrderId = orderId
item = Item.new
item.sku = skuCode
item.quantity = quantity
item.sourceItemId = itemId
component = Component.new
component.code = componentCode
component.fetch = false
component.path = fetchUrl
item.components.push(component)
order.orderData.items.push(item)
shipment = Shipment.new
shipment.carrier = Carrier.new(carrierCode, carrierService)
address = Address.new
address.name = name
address.address1 = address1
address.town = town
address.isoCountry = isoCountry
address.postcode = postcode
shipment.shipTo = address
order.orderData.shipments.push(shipment)
response = client.submit_order
response_json = JSON.parse(response.body)
if (response.code != 200)
puts "Error"
puts "====="
puts response_json["error"] ? response_json["error"]["message"] : response_json["message"]
if (response_json["error"] && response_json["error"]["validations"])
response_json["error"]["validations"].each {|val| puts val["path"] + " -> " + val["message"]}
end
else
saved_order = response_json["order"]
puts "Success"
puts "======="
puts "Order ID : " + saved_order["_id"]
# for file in saved_order["files"]
# puts "Uploading File : " + file["_id"]
# # res = client.upload_file(file, "files/" + file["path"])
# end
end