fluke/devops-service/sinatra/methods_with_headers.rb

133 lines
3.1 KiB
Ruby
Raw Permalink Normal View History

2015-02-18 13:15:25 +03:00
require "sinatra/base"
2015-06-30 14:27:42 +03:00
require 'rack/accept_media_types'
2018-04-04 22:44:39 +03:00
require 'db/mongo/models/policy'
2015-02-18 13:15:25 +03:00
module Sinatra
class Base
class << self
2018-04-04 22:44:39 +03:00
@@policies = {}
# options:
# dependencies - array
def define_policy name, description, options={}
options[:description] = description
@@policies[name.to_sym] = Devops::Model::Policy.new(name, options)
end
def policies
@@policies
end
2015-02-18 13:15:25 +03:00
def get_with_headers path, opt={}, &block
before path do
2018-04-04 22:44:39 +03:00
check_headers :accept
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
2018-04-04 22:44:39 +03:00
route_with_headers "POST", path, opt, [:accept, :content_type], &block
2015-02-19 11:27:56 +03:00
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
2015-09-17 13:15:19 +03:00
insert_statistic
2015-02-18 13:15:25 +03:00
end
end
def put_with_headers path, opt={}, &block
2018-04-04 22:44:39 +03:00
route_with_headers "PUT", path, opt, [:accept, :content_type], &block
2015-02-18 13:15:25 +03:00
end
2015-08-04 12:36:10 +03:00
def patch_with_headers path, opt={}, &block
2018-04-04 22:44:39 +03:00
route_with_headers "PATCH", path, opt, [:accept, :content_type], &block
2015-08-04 12:36:10 +03:00
end
2015-02-18 13:15:25 +03:00
def delete_with_headers path, opt={}, &block
2018-04-04 22:44:39 +03:00
route_with_headers "DELETE", path, opt, [:accept], &block
end
def route_with_headers method, path, opt={}, headers, &block
2015-02-18 13:15:25 +03:00
before path do
check_headers *headers
2015-03-06 12:20:30 +03:00
protect!
2015-02-18 13:15:25 +03:00
end
2018-04-04 22:44:39 +03:00
route(method, path, opt, &block)
2015-02-18 13:15:25 +03:00
after path do
2015-09-17 13:15:19 +03:00
insert_statistic
2015-02-18 13:15:25 +03:00
end
end
2015-02-18 13:50:02 +03:00
2018-04-04 22:44:39 +03:00
def multi_routes path, hash={}
2015-02-18 13:50:02 +03:00
before path do
2018-04-04 22:44:39 +03:00
if request.get? or request.delete?
2015-02-19 11:27:56 +03:00
check_headers :accept
else
2018-04-04 22:44:39 +03:00
check_headers :accept, :content_type
2015-02-19 11:27:56 +03:00
end
2015-03-06 12:20:30 +03:00
protect!
2015-02-18 13:50:02 +03:00
end
hash.each do |method, block|
2018-04-04 22:44:39 +03:00
route(method, path, {}, &block)
2015-02-18 13:50:02 +03:00
end
after path do
2018-04-04 22:44:39 +03:00
insert_statistic unless request.get?
2015-02-18 13:50:02 +03:00
end
end
2015-08-13 17:43:08 +03:00
def routes
@routes
end
def routes_list
routes.each do |verb, signature|
signature.each do |s|
puts "#{verb}\t#{s[0]}"
end
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-20 18:59:26 +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