| 
									
										
										
										
											2014-12-12 17:00:06 +03:00
										 |  |  | require 'sinatra/base' | 
					
						
							|  |  |  | require "sinatra/json" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "providers/provider_factory" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Sinatra | 
					
						
							|  |  |  |   module Version2_0 | 
					
						
							|  |  |  |     module Request | 
					
						
							|  |  |  |       module Helpers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-19 11:27:56 +03:00
										 |  |  | =begin | 
					
						
							| 
									
										
										
										
											2014-12-12 17:00:06 +03:00
										 |  |  |         # 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 | 
					
						
							|  |  |  |           logger.debug(request.accept) | 
					
						
							|  |  |  |           unless request.accept? 'application/json' | 
					
						
							|  |  |  |             response.headers['Accept'] = 'application/json' | 
					
						
							|  |  |  |             halt_response("Accept header should contains 'application/json' type", 406) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         rescue NoMethodError => e | 
					
						
							|  |  |  |           #error in sinatra 1.4.4 (https://github.com/sinatra/sinatra/issues/844, https://github.com/sinatra/sinatra/pull/805) | 
					
						
							|  |  |  |           response.headers['Accept'] = 'application/json' | 
					
						
							|  |  |  |           halt_response("Accept header should contains 'application/json' type", 406) | 
					
						
							|  |  |  |         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 | 
					
						
							| 
									
										
										
										
											2015-02-19 11:27:56 +03:00
										 |  |  | =end | 
					
						
							| 
									
										
										
										
											2014-12-12 17:00:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |