grape-app 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/Gemfile.lock +14 -11
- data/LICENSE +20 -0
- data/README.md +0 -24
- data/grape-app.gemspec +2 -2
- data/lib/grape/app.rb +17 -8
- data/lib/grape/app/configuration.rb +25 -0
- data/lib/grape/app/templates/config/environments/production.rb +16 -2
- data/spec/grape/app_spec.rb +31 -5
- data/spec/scenario/app/api.rb +10 -0
- data/spec/scenario/app/api/v1.rb +17 -0
- data/spec/scenario/config/environments/test.rb +10 -1
- data/spec/scenario/lib/my_lib.rb +16 -0
- data/spec/spec_helper.rb +1 -0
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d769186af82c987daacf3e75c394b06204bc16c2
|
4
|
+
data.tar.gz: f770e69d47c41f0592d8cf2e7d148152a860966f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d41db62c0ff28452f9182e779c2f54cf78475d489d50ad761bd128a779e304d6a8b38aabab3dae6f44b71de804aef051ccbbd57eda0f1b0c67ac8a9024fc38e4
|
7
|
+
data.tar.gz: ad80c70d20a27d4abeb444ab29cec562c72117d5567ec22aa7f072c81ee65de21fbfed7598834183abd5b476dfcd71450ba021a92c0e329e4e675f35ede9936d
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grape-app (0.
|
4
|
+
grape-app (0.5.0)
|
5
5
|
activesupport
|
6
6
|
grape
|
7
7
|
grape-entity
|
@@ -36,35 +36,37 @@ GEM
|
|
36
36
|
thread_safe (~> 0.3, >= 0.3.1)
|
37
37
|
diff-lcs (1.3)
|
38
38
|
equalizer (0.0.11)
|
39
|
-
grape (1.0.
|
39
|
+
grape (1.0.2)
|
40
40
|
activesupport
|
41
41
|
builder
|
42
42
|
mustermann-grape (~> 1.0.0)
|
43
43
|
rack (>= 1.3.0)
|
44
44
|
rack-accept
|
45
45
|
virtus (>= 1.0.0)
|
46
|
-
grape-entity (0.
|
47
|
-
activesupport (>=
|
46
|
+
grape-entity (0.7.1)
|
47
|
+
activesupport (>= 4.0)
|
48
48
|
multi_json (>= 1.3.2)
|
49
|
-
i18n (0.9.
|
49
|
+
i18n (0.9.3)
|
50
50
|
concurrent-ruby (~> 1.0)
|
51
51
|
ice_nine (0.11.2)
|
52
|
-
minitest (5.
|
53
|
-
multi_json (1.
|
52
|
+
minitest (5.11.3)
|
53
|
+
multi_json (1.13.1)
|
54
54
|
mustermann (1.0.1)
|
55
55
|
mustermann-grape (1.0.0)
|
56
56
|
mustermann (~> 1.0.0)
|
57
|
-
rack (2.0.
|
57
|
+
rack (2.0.4)
|
58
58
|
rack-accept (0.4.5)
|
59
59
|
rack (>= 0.4)
|
60
60
|
rack-cors (1.0.2)
|
61
61
|
rack-ssl-enforcer (0.2.9)
|
62
|
+
rack-test (0.8.2)
|
63
|
+
rack (>= 1.0, < 3)
|
62
64
|
rake (12.3.0)
|
63
65
|
rspec (3.7.0)
|
64
66
|
rspec-core (~> 3.7.0)
|
65
67
|
rspec-expectations (~> 3.7.0)
|
66
68
|
rspec-mocks (~> 3.7.0)
|
67
|
-
rspec-core (3.7.
|
69
|
+
rspec-core (3.7.1)
|
68
70
|
rspec-support (~> 3.7.0)
|
69
71
|
rspec-expectations (3.7.0)
|
70
72
|
diff-lcs (>= 1.2.0, < 2.0)
|
@@ -72,7 +74,7 @@ GEM
|
|
72
74
|
rspec-mocks (3.7.0)
|
73
75
|
diff-lcs (>= 1.2.0, < 2.0)
|
74
76
|
rspec-support (~> 3.7.0)
|
75
|
-
rspec-support (3.7.
|
77
|
+
rspec-support (3.7.1)
|
76
78
|
sqlite3 (1.3.13)
|
77
79
|
thor (0.20.0)
|
78
80
|
thread_safe (0.3.6)
|
@@ -91,9 +93,10 @@ DEPENDENCIES
|
|
91
93
|
activerecord
|
92
94
|
bundler
|
93
95
|
grape-app!
|
96
|
+
rack-test
|
94
97
|
rake
|
95
98
|
rspec
|
96
99
|
sqlite3
|
97
100
|
|
98
101
|
BUNDLED WITH
|
99
|
-
1.16.
|
102
|
+
1.16.1
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2018 Black Square Media
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -29,27 +29,3 @@ $ bundle install
|
|
29
29
|
Use `bundle show [gemname]` to see where a bundled gem is installed.
|
30
30
|
```
|
31
31
|
|
32
|
-
## Licence
|
33
|
-
|
34
|
-
```
|
35
|
-
Copyright (c) 2015 Black Square Media
|
36
|
-
|
37
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
38
|
-
a copy of this software and associated documentation files (the
|
39
|
-
"Software"), to deal in the Software without restriction, including
|
40
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
41
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
42
|
-
permit persons to whom the Software is furnished to do so, subject to
|
43
|
-
the following conditions:
|
44
|
-
|
45
|
-
The above copyright notice and this permission notice shall be
|
46
|
-
included in all copies or substantial portions of the Software.
|
47
|
-
|
48
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
49
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
50
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
51
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
52
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
53
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
54
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
55
|
-
```
|
data/grape-app.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'grape-app'
|
5
|
-
s.version = '0.
|
5
|
+
s.version = '0.5.0'
|
6
6
|
s.authors = ['Black Square Media Ltd']
|
7
7
|
s.email = ['info@blacksquaremedia.com']
|
8
8
|
s.summary = %{Stanalone Grape API apps}
|
@@ -28,6 +28,6 @@ Gem::Specification.new do |s|
|
|
28
28
|
s.add_development_dependency 'rspec'
|
29
29
|
s.add_development_dependency 'activerecord'
|
30
30
|
s.add_development_dependency 'sqlite3'
|
31
|
-
|
31
|
+
s.add_development_dependency 'rack-test'
|
32
32
|
end
|
33
33
|
|
data/lib/grape/app.rb
CHANGED
@@ -9,8 +9,6 @@ require 'rack/cors'
|
|
9
9
|
require 'rack/ssl-enforcer'
|
10
10
|
|
11
11
|
class Grape::App < Grape::API
|
12
|
-
include ActiveSupport::Configurable
|
13
|
-
|
14
12
|
class << self
|
15
13
|
|
16
14
|
# Run initializers
|
@@ -36,6 +34,20 @@ class Grape::App < Grape::API
|
|
36
34
|
require_one 'app', 'api'
|
37
35
|
end
|
38
36
|
|
37
|
+
# @return [Grape::App::Configuration] the configuration
|
38
|
+
def config
|
39
|
+
@_config ||= if respond_to?(:superclass) && superclass.respond_to?(:config)
|
40
|
+
superclass.config.inheritable_copy
|
41
|
+
else
|
42
|
+
Class.new(Grape::App::Configuration).new
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Configure the app
|
47
|
+
def configure
|
48
|
+
yield config
|
49
|
+
end
|
50
|
+
|
39
51
|
# @return [Pathname] root path
|
40
52
|
def root
|
41
53
|
@root ||= Bundler.root.dup
|
@@ -55,13 +67,9 @@ class Grape::App < Grape::API
|
|
55
67
|
def middleware
|
56
68
|
config = self.config
|
57
69
|
@middleware ||= Rack::Builder.new do
|
70
|
+
use Rack::Cors, &config.cors if config.cors
|
58
71
|
use Rack::SslEnforcer if config.force_ssl
|
59
|
-
|
60
|
-
allow do
|
61
|
-
origins *Array.wrap(config.cors_allow_origins)
|
62
|
-
resource '*', headers: :any, methods: [:get, :post, :options, :delete, :put]
|
63
|
-
end
|
64
|
-
end if config.cors_allow_origins
|
72
|
+
instance_eval(&config.middleware) if config.middleware
|
65
73
|
|
66
74
|
run Grape::App
|
67
75
|
end
|
@@ -82,4 +90,5 @@ class Grape::App < Grape::API
|
|
82
90
|
end
|
83
91
|
end
|
84
92
|
|
93
|
+
require 'grape/app/configuration'
|
85
94
|
require 'grape/app/helpers'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Grape::App::Configuration < ActiveSupport::InheritableOptions
|
2
|
+
|
3
|
+
def middleware(&block)
|
4
|
+
self.middleware = block if block
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
def cors(&block)
|
9
|
+
self.cors = block if block
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def cors_allow_origins=(value)
|
14
|
+
warn "[DEPRECATION] setting `config.cors_allow_origins` is deprecated. Please use `config.cors` with a block instead. [#{caller[0]}]"
|
15
|
+
|
16
|
+
value = Array.wrap(value)
|
17
|
+
self.cors do
|
18
|
+
allow do
|
19
|
+
origins *value
|
20
|
+
resource '*', headers: :any, methods: [:get, :post, :options, :delete, :put]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -3,8 +3,22 @@ Grape::App.configure do |config|
|
|
3
3
|
# Force SSL
|
4
4
|
# config.force_ssl = true
|
5
5
|
|
6
|
-
# CORS is disabled by default,
|
7
|
-
#
|
6
|
+
# CORS is disabled by default, please see https://github.com/cyu/rack-cors
|
7
|
+
# for configuration options.
|
8
|
+
#
|
9
|
+
# config.cors do
|
10
|
+
# allow do
|
11
|
+
# origins '*'
|
12
|
+
# resource '*', headers: :any, methods: [:get, :post, :options]
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
|
16
|
+
# Enable custom middleware:
|
17
|
+
#
|
18
|
+
# config.middleware do
|
19
|
+
# use Rack::ETag
|
20
|
+
# insert_before Rack::Cors, Rack::ContentLength
|
21
|
+
# end
|
8
22
|
|
9
23
|
# Don't raise errors on missing translations
|
10
24
|
# config.raise_on_missing_translations = false
|
data/spec/grape/app_spec.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Grape::App do
|
4
|
+
include Rack::Test::Methods
|
4
5
|
|
5
6
|
subject { described_class }
|
6
7
|
before { subject.init! File.expand_path('../../scenario', __FILE__) }
|
7
8
|
|
9
|
+
def app
|
10
|
+
subject.middleware
|
11
|
+
end
|
12
|
+
|
8
13
|
it 'should have an env' do
|
9
14
|
expect(subject.env).to be_instance_of(ActiveSupport::StringInquirer)
|
10
15
|
expect(subject.env).to eq("test")
|
@@ -30,17 +35,38 @@ RSpec.describe Grape::App do
|
|
30
35
|
end
|
31
36
|
|
32
37
|
it 'should read env specific initializers' do
|
33
|
-
expect(subject.config).to
|
34
|
-
test_specific
|
35
|
-
raise_on_missing_translations
|
36
|
-
|
38
|
+
expect(subject.config).to include(
|
39
|
+
:test_specific,
|
40
|
+
:raise_on_missing_translations,
|
41
|
+
:cors,
|
42
|
+
:middleware,
|
37
43
|
)
|
38
44
|
end
|
39
45
|
|
40
46
|
it 'should prepare middleware' do
|
41
47
|
expect(subject.middleware).to be_instance_of(Rack::Builder)
|
42
|
-
expect(subject.middleware.send(:instance_variable_get, :@use).size).to eq(
|
48
|
+
expect(subject.middleware.send(:instance_variable_get, :@use).size).to eq(2)
|
43
49
|
expect(subject.middleware.send(:instance_variable_get, :@run)).to eq(subject)
|
44
50
|
end
|
45
51
|
|
52
|
+
it 'should apply middleware' do
|
53
|
+
header 'Origin', 'test.host'
|
54
|
+
get '/v1/ok'
|
55
|
+
expect(last_response).to be_ok
|
56
|
+
expect(last_response.body).to eq(%({"status":"OK"}))
|
57
|
+
expect(last_response.headers).to include(
|
58
|
+
"Access-Control-Allow-Origin" => "*",
|
59
|
+
"X-MyApp" => "true",
|
60
|
+
)
|
61
|
+
|
62
|
+
header 'Origin', 'test.host'
|
63
|
+
get '/v1/failing'
|
64
|
+
expect(last_response).to be_bad_request
|
65
|
+
expect(last_response.body).to eq(%({"error":"bad request"}))
|
66
|
+
expect(last_response.headers).to include(
|
67
|
+
"Access-Control-Allow-Origin" => "*",
|
68
|
+
"X-MyApp" => "true",
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
46
72
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class API::V1 < Grape::API
|
2
|
+
version 'v1'
|
3
|
+
format :json
|
4
|
+
|
5
|
+
rescue_from MyLib::BadRequest do |e|
|
6
|
+
error!({error: e}, 400)
|
7
|
+
end
|
8
|
+
|
9
|
+
get '/ok' do
|
10
|
+
{status: 'OK'}
|
11
|
+
end
|
12
|
+
|
13
|
+
get '/failing' do
|
14
|
+
raise MyLib::BadRequest, "bad request"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -1,5 +1,14 @@
|
|
1
1
|
Grape::App.configure do |c|
|
2
2
|
c.test_specific = true
|
3
3
|
c.raise_on_missing_translations = true
|
4
|
-
c.cors_allow_origins =
|
4
|
+
c.cors_allow_origins = "x"
|
5
|
+
c.cors do
|
6
|
+
allow do
|
7
|
+
origins '*'
|
8
|
+
resource '*', headers: :any, methods: [:get, :post, :options]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
c.middleware do
|
12
|
+
use MyLib::Middleware
|
13
|
+
end
|
5
14
|
end
|
data/spec/scenario/lib/my_lib.rb
CHANGED
@@ -1,2 +1,18 @@
|
|
1
1
|
class MyLib
|
2
|
+
class BadRequest < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class Middleware
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
status, headers, body = @app.call(env)
|
13
|
+
headers['X-MyApp'] = 'true'
|
14
|
+
[status, headers, body]
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
2
18
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Black Square Media Ltd
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -164,6 +164,20 @@ dependencies:
|
|
164
164
|
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: rack-test
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
167
181
|
description: ''
|
168
182
|
email:
|
169
183
|
- info@blacksquaremedia.com
|
@@ -177,6 +191,7 @@ files:
|
|
177
191
|
- ".travis.yml"
|
178
192
|
- Gemfile
|
179
193
|
- Gemfile.lock
|
194
|
+
- LICENSE
|
180
195
|
- README.md
|
181
196
|
- Rakefile
|
182
197
|
- bin/grape-app
|
@@ -184,6 +199,7 @@ files:
|
|
184
199
|
- lib/grape-app.rb
|
185
200
|
- lib/grape/app.rb
|
186
201
|
- lib/grape/app/cli.rb
|
202
|
+
- lib/grape/app/configuration.rb
|
187
203
|
- lib/grape/app/helpers.rb
|
188
204
|
- lib/grape/app/helpers/params.rb
|
189
205
|
- lib/grape/app/helpers/respond_with.rb
|
@@ -210,6 +226,8 @@ files:
|
|
210
226
|
- lib/grape_app.rb
|
211
227
|
- spec/grape/app_spec.rb
|
212
228
|
- spec/scenario/Gemfile
|
229
|
+
- spec/scenario/app/api.rb
|
230
|
+
- spec/scenario/app/api/v1.rb
|
213
231
|
- spec/scenario/config/database.yml
|
214
232
|
- spec/scenario/config/environments/test.rb
|
215
233
|
- spec/scenario/config/locales/en.yml
|
@@ -242,6 +260,8 @@ summary: Stanalone Grape API apps
|
|
242
260
|
test_files:
|
243
261
|
- spec/grape/app_spec.rb
|
244
262
|
- spec/scenario/Gemfile
|
263
|
+
- spec/scenario/app/api.rb
|
264
|
+
- spec/scenario/app/api/v1.rb
|
245
265
|
- spec/scenario/config/database.yml
|
246
266
|
- spec/scenario/config/environments/test.rb
|
247
267
|
- spec/scenario/config/locales/en.yml
|