Sha256: 07a234f224f1484821dee7a0dede64a7fc972ff70a468c0550bbb9787554e874

Contents?: true

Size: 1.11 KB

Versions: 20

Compression:

Stored size: 1.11 KB

Contents

# encoding: utf-8

module Hyperdrive
  module Middleware
    class CORS
      def initialize(app, options = {})
        @app = app
        @options = options
      end

      def call(env)
        status, headers, body = @app.call(env)
        allowed_methods = '*'
        unless env['hyperdrive.resource'].nil?
          allowed_methods = env['hyperdrive.resource'].allowed_methods
        end
        cors = cors_headers(allowed_methods)
        headers.merge!(cors)
        [status, headers, body]
      end

      private

      def cors_headers(allowed_methods = '*')
        {
          'Access-Control-Allow-Origin'      => Array(@options[:origins]).join(", "),
          'Access-Control-Allow-Methods'     => Array(allowed_methods).join(", "),
          'Access-Control-Allow-Headers'     => Array(@options[:allow_headers]).join(", "),
          'Access-Control-Allow-Credentials' => (@options[:credentials] || false).to_s,
          'Access-Control-Max-Age'           => @options[:max_age].to_i.to_s,
          'Access-Control-Expose-Headers'    => Array(@options[:expose_headers]).join(", ")
        }
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
hyperdrive-0.0.25 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.24 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.23 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.22 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.21 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.20 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.19 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.18 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.17 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.16 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.15 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.14 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.13 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.12 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.11 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.10 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.9 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.8 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.7 lib/hyperdrive/middleware/cors.rb
hyperdrive-0.0.6 lib/hyperdrive/middleware/cors.rb