lib/herbert/Ajaxify.rb in herbert-0.0.1 vs lib/herbert/Ajaxify.rb in herbert-0.0.2
- old
+ new
@@ -1,15 +1,27 @@
module Herbert
+
+ # Enhances AJAJ/AJAX support
module Ajaxify
+
+ # Headers to send with each request
Headers = {
'Access-Control-Allow-Methods' => %w{POST GET PUT DELETE OPTIONS},
'Access-Control-Allow-Headers' => %w{Content-Type X-Requested-With},
'Access-Control-Allow-Origin' => %w{*},
'Access-Control-Expose-Header' => %w{Content-Type Content-Length X-Build},
'X-Build' => [Herbert::Utils.version]
}
+ # * Loads config/headers.rb
+ #
+ # * Enables header processing
+ #
+ # * Registers CORS proxy for external services
+ #
+ # @param [Sinatra::Base descendant]
+ #
def self.registered(app)
# Heeeaderzz!!! Gimme heaaaderzzz!!!
path = File.join(app.settings.root, 'config','headers.rb')
if File.exists?(path) then
log.h_debug("Loading additional headers from #{path}")
@@ -21,11 +33,11 @@
else
log.h_info("File #{path} doesn't exists; no addition headers loaded")
end
app.before do
- # Add the headers to the response
+ # Add the headers to the response
Headers.each {|name, value|
value = [value] unless value.is_a?(Array)
value.map! {|val|
(val.is_a?(Proc) ? val.call : val).to_s
}
@@ -33,18 +45,17 @@
}
end
# Proxy for not CORS enables services such as
# Google Maps
- # /proxy/<url to fetch>
- if app.get '/proxy/:url' do
- url = URI.parse(URI.decode(params[:url]))
- res = Net::HTTP.start(url.host, 80) {|http|
- http.get(url.path + (url.query ? '?' + url.query : ''))
- }
- response['content-type'] = res['content-type']
- res.body
- end
+ # /proxy/url?=
+ app.get '/proxy/' do
+ url = URI.parse(URI.encode(params[:url]))
+ res = Net::HTTP.start(url.host, 80) {|http|
+ http.get(url.path + '?' + url.query)
+ }
+ response['content-type'] = res['content-type']
+ res.body
+ end
end
- end
- end
+ end
end
\ No newline at end of file