lib/grape-dsl/headers.rb in grape-dsl-1.2.2 vs lib/grape-dsl/headers.rb in grape-dsl-1.3.0

- old
+ new

@@ -1,76 +1,78 @@ -# this is super if you want CORS with Grape -module Grape - class << self - # example in yaml format - # "header_name": - # - "header values" - # - "header value 2" - # - # - # real example for cors - # "Access-Control-Allow-Headers": - # - "X-Idp-Id" - # - "X-Token" - # - "Content-Type" - # "Access-Control-Allow-Origin": - # - "*" - # "Access-Control-Allow-Methods": - # - HEAD - # - OPTIONS - # - GET - # - POST - # - PUT - # - DELETE - # - # - # This will give headers to all call request response made after this - # make sure to load BEFORE every route call going to be made - def response_headers_to_new_calls(config_obj=nil) +module GrapeDSL + module Extend + module Headers - Grape::Endpoint.config_obj= config_obj unless config_obj.nil? - Grape::API.inject_singleton_method :inherited, add: "after" do |subclass| + # example in yaml format + # "header_name": + # - "header values" + # - "header value 2" + # + # + # real example for cors + # "Access-Control-Allow-Headers": + # - "X-Idp-Id" + # - "X-Token" + # - "Content-Type" + # "Access-Control-Allow-Origin": + # - "*" + # "Access-Control-Allow-Methods": + # - HEAD + # - OPTIONS + # - GET + # - POST + # - PUT + # - DELETE + # + # + # This will give headers to all call request response made after this + # make sure to load BEFORE every route call going to be made + def response_headers_to_new_calls(config_obj=nil) - subclass.class_eval do + Grape::Endpoint.config_obj= config_obj unless config_obj.nil? + Grape::API.inject_singleton_method :inherited, add: "after" do |subclass| - before do - Grape::Endpoint.header_config_obj.each do |header_key,header_value| - header header_key, header_value.join(', ') + subclass.class_eval do + + before do + Grape::Endpoint.header_config_obj.each do |header_key,header_value| + header header_key, header_value.join(', ') + end end + end end + return nil end - return nil - end + # same config obj format like to "response_headers_to_new_calls" + # this will create headers for the options call to ALL already made route + # make sure to load after every route has been made + def response_headers_to_routes_options_request(config_obj=nil) + Grape::Endpoint.header_config_obj= config_obj unless config_obj.nil? + Grape::API.subclasses.each do |rest_api_model| + rest_api_model.routes.map { |route| route.route_path.split('(.:format)')[0] }.uniq.each do |path| + rest_api_model.class_eval do - # same config obj format like to "response_headers_to_new_calls" - # this will create headers for the options call to ALL already made route - # make sure to load after every route has been made - def response_headers_to_routes_options_request(config_obj=nil) - - Grape::Endpoint.header_config_obj= config_obj unless config_obj.nil? - Grape::API.subclasses.each do |rest_api_model| - rest_api_model.routes.map { |route| route.route_path.split('(.:format)')[0] }.uniq.each do |path| - rest_api_model.class_eval do - - options path do - Grape::Endpoint.header_config_obj.each do |header_key,header_value| - header header_key, header_value.join(', ') + options path do + Grape::Endpoint.header_config_obj.each do |header_key,header_value| + header header_key, header_value.join(', ') + end end - end + end end + end + return nil end - return nil end - - end -end +end + +Grape.__send__ :extend, ::GrapeDSL::Extend::Headers \ No newline at end of file