-
Notifications
You must be signed in to change notification settings - Fork 5
/
barcoded.rb
41 lines (34 loc) · 933 Bytes
/
barcoded.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
module Barcoded
class Service < Sinatra::Base
helpers Sinatra::RequestHelpers
helpers Sinatra::ResponseHelpers
register Sinatra::RespondWith
register Sinatra::CrossOrigin
include Sinatra::ExceptionHandler
enable :logging
before '/barcodes' do
normalize_params!
valid_request!
supported!
allow_cross_origin if cors_enabled?
end
post '/barcodes' do
create_barcode(encoding, data)
created
end
get '/img/*/*.*' do |encoding, data, format|
barcode = create_barcode(encoding, URI.unescape(data))
image = BarcodeImageFactory.build(barcode, format, options)
send_image image, format
end
get '/ping' do
healthy_response
end
private
def create_barcode(encoding, value)
barcode = BarcodeFactory.build(encoding, value)
raise InvalidBarcodeData unless barcode.valid?
barcode
end
end
end