-
Notifications
You must be signed in to change notification settings - Fork 0
/
recreate.rb
executable file
·112 lines (100 loc) · 3.64 KB
/
recreate.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
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'json'
require 'easypost'
require 'open-uri'
Dir.chdir "./RubyScripts";
file = File.read('./misc.JSON');
client = EasyPost::Client.new(api_key: ENV['EASYPOST_TEST_KEY']);
data_hash = JSON.parse(file);
properties = Array["created_at", "messages", "status", "tracking_code", "updated_at",
"batch_id", "batch_status", "batch_message", "id", "order_id",
"postage_label", "tracker", "selected_rate", "scan_form", "usps_zone",
"refund_status", "mode", "fees", "object", "rates", "insurance", "forms", "verifications"];
obj = Array[data_hash["to_address"], data_hash["from_address"], data_hash["return_address"], data_hash["buyer_address"], data_hash["parcel"]]
properties.each do |i|
data_hash.delete(i);
end;
obj.each do |i|
properties.each do |j|
i.delete(j);
end;
end
if data_hash["customs_info"] != nil
properties.each do |i|
data_hash["customs_info"].delete(i);
end;
data_hash["customs_info"]["customs_items"].each do |i|
properties.each do |j|
i.delete(j);
end;
end;
end;
if data_hash["options"]["print_custom"]
data_hash["options"].delete("print_custom");
end;
puts "Press 1 for Rate/Buy Flow, Press 2 For One-Call-Buy";
user = gets.chomp
begin
loop do
#Rate/Buy Flow
if user == "1"
shipment = client.shipment.create(
to_address: data_hash["to_address"],
from_address: data_hash["from_address"],
return_address: data_hash["return_address"],
parcel: data_hash["parcel"],
customs_info: data_hash["customs_info"],
options: data_hash["options"],
reference: data_hash["reference"],
is_return: data_hash["is_return"],
#carrier_accounts: [""]
);
if(shipment["messages"].length > 0)
shipment["messages"].each do |i|
puts i["carrier"];
puts i["message"];
puts "\n--------------------------------------------\n\n";
end;
end;
if(shipment["rates"].length > 0)
shipment["rates"].each do |i|
puts i["carrier"] + " | " + i["carrier_account_id"];
puts i["service"] + " | " + i["rate"];
puts i["id"];
puts "\n--------------------------------------------\n\n";
end;
elsif(shipment["rates"].length == 0)
break;
end;
puts shipment["id"];
puts "\nPlease enter the rate you wish to purchase(or, press enter to quit): ";
user = gets.chomp
if user.match(/rate_/)
purchased = client.shipment.buy(shipment["id"], rate: {id: user});
#Any data you wish to display or open to.
system("open #{purchased["postage_label"]["label_url"]}");
else
#Any data you wish to display or open to.
break;
end;
break;
#One Call Buy
elsif user == "2"
shipment = client.shipment.create(
to_address: data_hash["to_address"],
from_address: data_hash["from_address"],
return_address: data_hash["return_address"],
parcel: data_hash["parcel"],
customs_info: data_hash["customs_info"],
options: data_hash["options"],
reference: data_hash["reference"],
is_return: data_hash["is_return"],
carrier_accounts: [""],
service: "yadda yadda yadda");
#Any data you wish to display or open to.
system("open #{shipment["postage_label"]["label_url"]}");
break;
end;
end;
rescue EasyPost::Errors::ApiError => e
puts "#{e.code}: #{e.message}";
end;