-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_orders_from_csv.rb
79 lines (60 loc) · 1.7 KB
/
import_orders_from_csv.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
#!/usr/bin/ruby
# Upload kickstarter orders from a CSV to the TPR-Coordinator
#
# Usage: ruby import_orders_from_csv.rb <path_to_csv> <env> <auth_token> <source>
require 'CSV'
require 'httparty'
# set arguments
backers_csv = CSV.read(ARGV[0])
env = ARGV[1]
auth_token = ARGV[2]
source = ARGV[3]
case env
when 'production'
url='https://api.thepublicrad.io/orders'
when 'staging'
url='https://api-staging.thepublicrad.io/orders'
end
# Import CSV
headers = backers_csv.shift
backer_list = []
backers_csv.each do |backer|
hash = {}
headers.each_with_index do |header,i|
hash[header] = backer[i]
end
backer_list << hash
end
backer_list.each do |backer|
if !backer['Shipping Name'].nil?
name = backer['Shipping Name']
else
name = backer['Backer Name']
end
order_params = {
name: name,
order_source: source,
email: backer['Email'],
street_address_1: backer['Shipping Address 1'],
street_address_2: backer['Shipping Address 2'],
city: backer['Shipping City'],
state: backer['Shipping State'],
postal_code: backer['Shipping Postal Code'],
country: backer['Shipping Country Code'],
phone: backer['Shipping Phone Number']
}
frequencies = []
backer.select{ |s| s.include?('Radio') }.each do |k,v|
frequencies << v
end
order_params['frequencies'] = { backer['Shipping Country Code'] => frequencies.compact }
# Post to TPR Coordinator
headers = {'Authorization' => "Bearer #{auth_token}", 'Content-Type' => 'application/json'}
puts url
response = HTTParty.post(url, headers: headers, body: order_params.to_json)
if (200..299).include?(response.code)
puts 'Success!'
else
puts "An error occured uplading order #{order_params}: #{response.body}"
end
end;nil