Skip to content

Commit

Permalink
Update for Sinatra 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesrosiers committed Jul 26, 2017
1 parent edf0d26 commit e6ac680
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"

gem 'guard-rspec', '~> 4.7', '>= 4.7.3'
gem 'rack-test', '~> 0.6.3'
gem 'sinatra', '~> 1.4', '>= 1.4.8'
gem 'rack-test', '~> 0.7.0'
gem 'sinatra', '~> 2.0'
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,36 @@ Quick Start
-----------
The following is an example of how to create a CORS enabled route with some typical default configuration.

**IMPORTANT:** The plugin handles OPTIONS requests automatically, but if you have reason to add OPTIONS routes for some or all of your routes manually, you will need to put the `register Sinatra::Cors` line after the OPTIONS routes that you create.

**IMPORTANT:** The CORS settings must come after the `register Sinatra::Cors` line.

```ruby
require "sinatra"
require "sinatra/cors"

set :allow_origin, "http://example.com http://foo.com"
set :allow_methods, "GET HEAD POST"
set :allow_headers, "content-type"

get "/foo" do
"foo"
end
```

register Sinatra::Cors
Or, for a modular style application.

set :allow_origin, "http://example.com http://foo.com"
set :allow_methods, "GET HEAD POST"
set :allow_headers, "content-type"
```ruby
require "sinatra"
require "sinatra/cors"

class Foo < Sinatra::Base
register Sinatra::Cors

set :allow_origin, "http://example.com http://foo.com"
set :allow_methods, "GET HEAD POST"
set :allow_headers, "content-type"

get "/foo" do
"foo"
end
end
```

Settings
Expand Down
4 changes: 3 additions & 1 deletion lib/sinatra/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def allowed_methods
matches = []
settings.routes.each do |method, routes|
routes.each do |route|
process_route(route[0], route[1], route[2]) do |application, pattern|
process_route(route[0], route[1]) do |application, pattern|
matches << method
end
end
Expand Down Expand Up @@ -118,4 +118,6 @@ def self.registered(app)
end
end
end

register Cors
end
4 changes: 2 additions & 2 deletions sinatra-cors.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = "sinatra-cors"
s.version = "0.2.0"
s.date = "2017-06-28"
s.version = "1.0.0"
s.date = "2017-07-25"
s.summary = "CORS support for Sinatra applications"
s.description = <<-EOT
This Sinatra plugin supports the full CORS spec including automatic support for CORS preflight (OPTIONS) requests. It uses CORS security best practices. The plugin logs to the default logger to guide you in setting things up properly. It will tell you why a CORS request failed and tell you how to fix it.
Expand Down
2 changes: 1 addition & 1 deletion spec/cors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def app
end

it "should have an Allow header build from existing routes" do
expect(last_response["Allow"]).to eq("GET HEAD DELETE OPTIONS")
expect(last_response["Allow"]).to eq("OPTIONS GET HEAD DELETE")
end

it "should have an Access-Control-Allow-Methods header that includes only the method requested" do
Expand Down
10 changes: 4 additions & 6 deletions spec/fixture.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "sinatra"
require "./lib/sinatra/cors"

set :allow_origin, "http://example.com http://foo.com"
set :allow_methods, "GET HEAD POST"
set :allow_headers, "content-type if-modified-since"

get "/foo/:id" do
"foo"
end
Expand All @@ -12,9 +16,3 @@
post "/bar/:id" do
"bar"
end

register Sinatra::Cors

set :allow_origin, "http://example.com http://foo.com"
set :allow_methods, "GET HEAD POST"
set :allow_headers, "content-type if-modified-since"

0 comments on commit e6ac680

Please sign in to comment.