2019-01-16 10:22:44 -08:00
module Sinatra
module OrderHelpers
def fetch_order_by_uuid
2019-02-08 13:34:40 -08:00
Order . where ( uuid : params [ :uuid ] ) . first || uuid_not_found_error
2019-01-16 10:22:44 -08:00
end
def authorize_order! ( order )
if order . user_auth_token != params [ :auth_token ]
2019-02-08 13:34:40 -08:00
halt 401 , error_object ( " Unauthorized " , " Invalid authentication token " , ERROR :: CODES [ :INVALID_AUTH_TOKEN ] )
2019-01-16 10:22:44 -08:00
else
order
end
end
2019-05-19 20:55:31 -07:00
def fetch_order_by_tx_seq_num
Order . find_by ( tx_seq_num : params [ :tx_seq_num ] ) || tx_seq_num_not_found_error
end
2019-01-16 10:22:44 -08:00
def get_and_authenticate_order
authorize_order! ( fetch_order_by_uuid )
end
2019-02-08 13:34:40 -08:00
def invalid_date_error
halt 400 , error_object ( " Invalid date " , " Couldn't parse date given by before param " , ERROR :: CODES [ :INVALID_DATE ] )
end
def sequence_number_not_found_error
halt 404 , error_object ( " Sequence number not found " , " Sent order with that tx sequence number not found " , ERROR :: CODES [ :SEQUENCE_NUMBER_NOT_FOUND ] )
end
def uuid_not_found_error
2019-02-08 14:08:55 -08:00
halt 404 , error_object ( " UUID not found " , " UUID #{ params [ :uuid ] } not found " , ERROR :: CODES [ :ORDER_NOT_FOUND ] )
2019-02-08 13:34:40 -08:00
end
2019-05-19 20:55:31 -07:00
def tx_seq_num_not_found_error
halt 404 , error_object ( " tx sequence number not found " , " tx sequence number #{ params [ :tx_seq_num ] } not found " , ERROR :: CODES [ :ORDER_NOT_FOUND ] )
end
2019-02-08 13:34:40 -08:00
2019-02-15 00:22:49 -08:00
def message_missing_error
halt 400 , error_object ( " Message upload problem " , " Either a message file or a message parameter is required " , ERROR :: CODES [ :MESSAGE_MISSING ] )
2019-02-08 13:34:40 -08:00
end
2019-02-15 00:22:49 -08:00
def message_file_missing_error
halt 400 , error_object ( " Message upload problem " , " No tempfile received " , ERROR :: CODES [ :FILE_MISSING ] )
2019-02-08 13:34:40 -08:00
end
2019-02-15 00:22:49 -08:00
2019-02-08 13:34:40 -08:00
def message_file_too_large_error
halt 413 , error_object ( " Message upload problem " , " Message size exceeds max size #{ MAX_MESSAGE_SIZE } " , ERROR :: CODES [ :MESSAGE_FILE_TOO_LARGE ] )
end
def message_file_too_small_error
halt 400 , error_object ( " Message upload problem " , " Message too small. Minimum message size is #{ MIN_MESSAGE_SIZE } byte " , ERROR :: CODES [ :MESSAGE_FILE_TOO_SMALL ] )
end
def bid_too_small_error ( min_bid )
halt 413 , error_object ( " Bid too low " , " Per byte bid cannot be below #{ MIN_PER_BYTE_BID } millisatoshis per byte. The minimum bid for this message is #{ min_bid } millisatoshis. " , ERROR :: CODES [ :BID_TOO_SMALL ] )
end
2019-04-11 13:56:42 +01:00
def order_bump_error ( order )
2019-02-08 13:34:40 -08:00
halt 400 , error_object ( " Cannot bump order " , " Order already #{ order . status } " , ERROR :: CODES [ :ORDER_BUMP_ERROR ] )
end
2019-04-11 13:56:42 +01:00
def order_cancellation_error ( order )
2019-02-08 13:34:40 -08:00
halt 400 , error_object ( " Cannot cancel order " , " Order already #{ order . status } " , ERROR :: CODES [ :ORDER_CANCELLATION_ERROR ] )
end
2019-01-16 10:22:44 -08:00
end
helpers OrderHelpers
end