-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
142 lines (119 loc) · 3.99 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# app.rb
# Monkey patch until this is merged
# https://github.com/nehresma/cupsffi/pull/30 `
class File
def self.exists?(filename)
self.exist?(filename)
end
end
module ShippingApp
class App < Sinatra::Base
enable :sessions
COUNTRY_FLAGS = {
'US' => '🇺🇸',
'CA' => '🇨🇦',
'GB' => '🇬🇧',
'PR' => '🇵🇷',
'AU' => '🇦🇺'
}.freeze
configure do
Environment.setup
end
helpers do
def with_vcr
if Environment.development?
puts "Using VCR cassette"
VCR.use_cassette('tindie_orders', record: :once) { yield }
else
puts "Not using VCR cassette"
yield
end
end
def purchased_labels
session[:orders] || {}
end
end
get '/orders' do
tindie_api = TindieApi::TindieOrdersAPI.new(
ENV['TINDIE_USERNAME'],
ENV['TINDIE_API_KEY']
)
unshipped_orders = with_vcr { tindie_api.get_all_orders(false) }
# puts unshipped_orders.inspect
erb :orders, locals: {
orders: unshipped_orders,
purchased_labels: purchased_labels,
username: ENV['TINDIE_USERNAME'],
api_key: ENV['TINDIE_API_KEY'],
countries: COUNTRY_FLAGS,
total_count: unshipped_orders.length
}
end
post '/buy_label/:order_number' do
order_number = params[:order_number]
puts "Buying label for order: #{order_number}"
order_data = {
'shipping_name' => params[:shipping_name],
'shipping_street' => params[:shipping_street],
'shipping_city' => params[:shipping_city],
'shipping_state' => params[:shipping_state],
'shipping_postcode' => params[:shipping_postcode],
'shipping_country' => params[:shipping_country],
'shipping_phone' => params[:shipping_phone].to_s.empty? ? nil : params[:shipping_phone],
'email' => params[:email].to_s.empty? ? nil : params[:email]
}
puts "Order Data: #{order_data.inspect}"
result = ShippingApp::ShippingService.new.create_label(order_number, order_data)
# Store the label information in the session
session[:orders] ||= {}
session[:orders][order_number] = {
tracking_code: result[:tracking_code],
label_url: result[:label_url]
}
content_type :json
result.to_json
end
post '/print_label' do
content_type :json
begin
data = JSON.parse(request.body.read)
puts "Downloading label: #{data}"
label_url = data['label_url']
# Cache handling code remains the same
cache_dir = File.join(Dir.pwd, 'cache', 'labels')
FileUtils.mkdir_p(cache_dir)
original_filename = File.basename(URI.parse(label_url).path)
cached_file = File.join(cache_dir, original_filename)
unless File.exist?(cached_file)
URI.open(label_url) do |url_file|
File.open(cached_file, 'wb') do |file|
puts "Caching label: #{cached_file}"
file.write(url_file.read)
end
end
end
printer = CupsPrinter.new("PM-241-BT", :hostname => ENV['CUPS_HOST'], :port => 631)
puts "Printing label: #{cached_file} on printer #{ENV['CUPS_HOST']}"
job = printer.print_file(cached_file)
begin
status = job.status
rescue RuntimeError => e
if e.message.include?('Job not found')
# If job is not found, it likely completed successfully
status = "completed (fast job)"
else
raise e
end
end
{ success: true, message: "Print job sent successfully. Status: #{status}" }.to_json
rescue => e
puts "ERROR in print_label: #{e.class} - #{e.message}"
puts e.backtrace
{ success: false, message: "Error: #{e.message}" }.to_json
end
end
get '/' do
redirect '/orders'
end
end
end