Skip to content

Commit

Permalink
Merge pull request #38 from jamesbrink/misc_fixes
Browse files Browse the repository at this point in the history
Added support to catch invalid data on POST
  • Loading branch information
doomspork committed May 17, 2014
2 parents 460614d + eb8546c commit d494bd0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion barcoded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class Barcoded < Sinatra::Base
private

def create_barcode(encoding, value)
BarcodeFactory.build(encoding, value)
barcode = BarcodeFactory.build(encoding, value)
raise InvalidBarcodeData unless barcode.valid?
barcode
end

end
2 changes: 1 addition & 1 deletion lib/sinatra/exception_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ExceptionHandler

def handle_exception!(ex)
case ex
when ArgumentError
when ArgumentError, InvalidBarcodeData
bad_request(INVALID_DATA)
else
super(ex)
Expand Down
3 changes: 3 additions & 0 deletions lib/sinatra/invalid_barcode_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class InvalidBarcodeData < Exception

end
29 changes: 29 additions & 0 deletions spec/barcoded_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

describe Barcoded do
describe 'POST /barcodes' do
valid_data = {
'bookland' => '968-26-1240-3',
'code128a' => 'ABC123',
'code128b' => 'ABC123',
'code128c' => '123456',
'code25' => '0123456789',
'code25interleaved' => '12345670',
'code39' => 'TEST8052',
'code93' => 'TEST93',
'ean13' => '123456789012',
'ean8' => '1234567',
'iata' => '0123456789',
'supp2' => '12',
'supp5' => '12345',
'upca' => '12345678999'
}

shared_examples 'a supported content type' do
let(:encoding) { 'code-128a' }
let(:format) { 'png' }
Expand Down Expand Up @@ -45,6 +62,18 @@
expect(response['location']).to_not be_nil
end

valid_data.each do |encoding, data|
context 'with valid data' do
let(:encoding) { encoding }
let(:data) { data }

it 'will return a valid barcode' do
post '/barcodes',barcode_request, headers
expect(response['location']).to eq "http://example.org/img/#{encoding}/#{data}.#{format}"
end
end
end

it 'will support Cross Origin Resource Sharing' do
ENV['RACK_CORS'] = 'enabled'
ENV['RACK_CORS_ORIGINS'] = 'http://utensils.io/'
Expand Down

0 comments on commit d494bd0

Please sign in to comment.