2015-02-18 13:15:25 +03:00
|
|
|
require "sinatra/base"
|
2015-06-30 14:27:42 +03:00
|
|
|
require 'rack/accept_media_types'
|
2015-02-18 13:15:25 +03:00
|
|
|
|
|
|
|
|
module Sinatra
|
|
|
|
|
|
|
|
|
|
class Base
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
|
|
# TODO: add protect! method
|
|
|
|
|
def get_with_headers path, opt={}, &block
|
|
|
|
|
headers = opt.delete(:headers) || []
|
|
|
|
|
before path do
|
|
|
|
|
check_headers *headers
|
2015-03-06 12:20:30 +03:00
|
|
|
protect!
|
2015-02-18 13:15:25 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
get path, opt, &block
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post_with_headers path, opt={}, &block
|
|
|
|
|
headers = opt.delete(:headers) || []
|
|
|
|
|
before path do
|
|
|
|
|
check_headers *headers
|
2015-03-06 12:20:30 +03:00
|
|
|
protect!
|
2015-02-18 13:15:25 +03:00
|
|
|
end
|
2015-02-19 11:27:56 +03:00
|
|
|
post_with_statistic path, opt, &block
|
|
|
|
|
end
|
2015-02-18 13:15:25 +03:00
|
|
|
|
2015-02-19 11:27:56 +03:00
|
|
|
def post_with_statistic path, opt={}, &block
|
2015-02-18 13:15:25 +03:00
|
|
|
post path, opt, &block
|
|
|
|
|
|
|
|
|
|
after path do
|
|
|
|
|
statistic
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def put_with_headers path, opt={}, &block
|
|
|
|
|
headers = opt.delete(:headers) || []
|
|
|
|
|
before path do
|
|
|
|
|
check_headers *headers
|
2015-03-06 12:20:30 +03:00
|
|
|
protect!
|
2015-02-18 13:15:25 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
put path, opt, &block
|
|
|
|
|
|
|
|
|
|
after path do
|
|
|
|
|
statistic
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def delete_with_headers path, opt={}, &block
|
|
|
|
|
headers = opt.delete(:headers) || []
|
|
|
|
|
before path do
|
|
|
|
|
check_headers *headers
|
2015-03-06 12:20:30 +03:00
|
|
|
protect!
|
2015-02-18 13:15:25 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
delete path, opt, &block
|
|
|
|
|
|
|
|
|
|
after path do
|
|
|
|
|
statistic
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-02-18 13:50:02 +03:00
|
|
|
|
|
|
|
|
def multi_routes path, opts={}, hash={}
|
2015-02-19 12:01:39 +03:00
|
|
|
headers = opts.delete(:headers) || []
|
2015-02-18 13:50:02 +03:00
|
|
|
before path do
|
2015-02-19 11:27:56 +03:00
|
|
|
if request.get?
|
|
|
|
|
check_headers :accept
|
|
|
|
|
else
|
|
|
|
|
check_headers *headers
|
|
|
|
|
end
|
2015-03-06 12:20:30 +03:00
|
|
|
protect!
|
2015-02-18 13:50:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
hash.each do |method, block|
|
|
|
|
|
route(method, path, opts, &block)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
after path do
|
|
|
|
|
statistic
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-02-18 13:15:25 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Check request headers
|
|
|
|
|
def check_headers *headers
|
|
|
|
|
ha = (headers.empty? ? [:accept, :content_type] : headers)
|
|
|
|
|
ha.each do |h|
|
|
|
|
|
case h
|
|
|
|
|
when :accept, "accept"
|
|
|
|
|
accept_json
|
|
|
|
|
when :content_type, "content_type"
|
|
|
|
|
request_json
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Check Accept header
|
|
|
|
|
#
|
|
|
|
|
# Can client works with JSON?
|
|
|
|
|
def accept_json
|
2015-06-30 14:27:42 +03:00
|
|
|
types = request.accept_media_types
|
2015-07-15 16:52:50 +03:00
|
|
|
unless types.include?('application/json') or types.include?("*/*")
|
2015-02-18 13:15:25 +03:00
|
|
|
response.headers['Accept'] = 'application/json'
|
|
|
|
|
halt_response("Accept header should contains 'application/json' type", 406)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Check Content-Type header
|
|
|
|
|
def request_json
|
|
|
|
|
halt_response("Content-Type should be 'application/json'", 415) if request.media_type.nil? or request.media_type != 'application/json'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|