Sha256: 192cd0d314dcf389d533ca928fcf219fbd6d1832f24f3c9898a41f097df275bc

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Herbert
  module Ajaxify
    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]
    }

    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}")
        custom = eval(File.open(path).read)
        custom.each {|name, value|
          value = [value] unless value.is_a?(Array)
          Headers[name] = (Headers[name] || []) | value
        }
      else
        log.h_info("File #{path} doesn't exists; no addition headers loaded")
      end

      app.before do
      # 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
          }
          response[name] = value.join(', ')
        }
      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
    end
   end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
herbert-0.0.1 lib/herbert/Ajaxify.rb