grape-app 0.8.5 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c4a05ef1c03ec244f8f99896b795bde1b89ce280af80c46558fb9dc09a26b40
4
- data.tar.gz: 3bfca4ffb992d3c0b9407215ae3cf08c538be22c6337d86125dcc1d2cf65bec4
3
+ metadata.gz: 28f6b7de85ab1f92ac5b216e20c087d3ec2bf42d2a1248eb29080b7857b8a84d
4
+ data.tar.gz: 9881ddbbf2a478bb08aea7fe96d4b495c40530b50e2d4809e8275d6b354e3b62
5
5
  SHA512:
6
- metadata.gz: ee63d58b3fcea0b099e749a82bf945753f50077c41ddefb833ea2c40b5aaedd6243b52296846b85117836f2c985e4eaa33aa402fe5ffe99e862106c1f99c25ba
7
- data.tar.gz: 3e8a010d81aaf298110440a18da811d3829ebe6a6a915db16b05c0d96fabe7b53cbc79b3be227943f39b58185e3a81983d153087071f3170c16b41277969357b
6
+ metadata.gz: 7825c7dfca1769b0e8248ed3e573a3269680f2d223dd33d00e64c955e4d45533baf4892329e9684b31f1ca1657ec45ffe12823908d3feb164cef140b9caeac42
7
+ data.tar.gz: 418375f83c0020d081dc40a05bfb92272a1173d06e53a70c14360c6b44ffc045d105c928e65530864add0853b90fac060c7ffa238e800870f47024a2b59e5350
@@ -0,0 +1,21 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ["2.7", "3.0", "3.1"]
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby-version }}
20
+ bundler-cache: true
21
+ - run: bundle exec rake
data/.rubocop.yml CHANGED
@@ -1,8 +1,14 @@
1
- inherit_from:
2
- - https://gitlab.com/bsm/misc/raw/master/rubocop/default.yml
1
+ inherit_gem:
2
+ rubocop-bsm:
3
+ - default.yml
4
+ inherit_mode:
5
+ merge:
6
+ - Exclude
3
7
 
4
8
  AllCops:
5
- TargetRubyVersion: "2.5"
9
+ TargetRubyVersion: "2.7"
6
10
  Metrics/ParameterLists:
7
11
  Exclude:
8
12
  - lib/grape/app/helpers/caching.rb
13
+ Rake/Desc:
14
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape-app (0.8.5)
4
+ grape-app (0.9.0)
5
5
  activesupport
6
6
  grape (>= 1.2)
7
7
  grape-entity
@@ -13,76 +13,60 @@ PATH
13
13
  GEM
14
14
  remote: https://rubygems.org/
15
15
  specs:
16
- activemodel (6.0.2.1)
17
- activesupport (= 6.0.2.1)
18
- activerecord (6.0.2.1)
19
- activemodel (= 6.0.2.1)
20
- activesupport (= 6.0.2.1)
21
- activesupport (6.0.2.1)
16
+ activemodel (7.0.1)
17
+ activesupport (= 7.0.1)
18
+ activerecord (7.0.1)
19
+ activemodel (= 7.0.1)
20
+ activesupport (= 7.0.1)
21
+ activesupport (7.0.1)
22
22
  concurrent-ruby (~> 1.0, >= 1.0.2)
23
- i18n (>= 0.7, < 2)
24
- minitest (~> 5.1)
25
- tzinfo (~> 1.1)
26
- zeitwerk (~> 2.2)
27
- ast (2.4.0)
28
- axiom-types (0.1.1)
29
- descendants_tracker (~> 0.0.4)
30
- ice_nine (~> 0.11.0)
31
- thread_safe (~> 0.3, >= 0.3.1)
23
+ i18n (>= 1.6, < 2)
24
+ minitest (>= 5.1)
25
+ tzinfo (~> 2.0)
26
+ ast (2.4.2)
32
27
  builder (3.2.4)
33
- coercible (1.0.0)
34
- descendants_tracker (~> 0.0.1)
35
- concurrent-ruby (1.1.6)
36
- descendants_tracker (0.0.4)
37
- thread_safe (~> 0.3, >= 0.3.1)
38
- diff-lcs (1.3)
39
- dry-configurable (0.11.3)
28
+ concurrent-ruby (1.1.9)
29
+ diff-lcs (1.5.0)
30
+ dry-configurable (0.14.0)
40
31
  concurrent-ruby (~> 1.0)
41
- dry-core (~> 0.4, >= 0.4.7)
42
- dry-equalizer (~> 0.2)
43
- dry-container (0.7.2)
32
+ dry-core (~> 0.6)
33
+ dry-container (0.9.0)
44
34
  concurrent-ruby (~> 1.0)
45
- dry-configurable (~> 0.1, >= 0.1.3)
46
- dry-core (0.4.9)
35
+ dry-configurable (~> 0.13, >= 0.13.0)
36
+ dry-core (0.7.1)
47
37
  concurrent-ruby (~> 1.0)
48
- dry-equalizer (0.3.0)
49
- dry-inflector (0.2.0)
50
- dry-logic (1.0.6)
38
+ dry-inflector (0.2.1)
39
+ dry-logic (1.2.0)
51
40
  concurrent-ruby (~> 1.0)
52
- dry-core (~> 0.2)
53
- dry-equalizer (~> 0.2)
54
- dry-types (1.3.1)
41
+ dry-core (~> 0.5, >= 0.5)
42
+ dry-types (1.5.1)
55
43
  concurrent-ruby (~> 1.0)
56
44
  dry-container (~> 0.3)
57
- dry-core (~> 0.4, >= 0.4.4)
58
- dry-equalizer (~> 0.3)
45
+ dry-core (~> 0.5, >= 0.5)
59
46
  dry-inflector (~> 0.1, >= 0.1.2)
60
47
  dry-logic (~> 1.0, >= 1.0.2)
61
- equalizer (0.0.11)
62
- grape (1.3.0)
48
+ grape (1.6.2)
63
49
  activesupport
64
50
  builder
65
51
  dry-types (>= 1.1)
66
52
  mustermann-grape (~> 1.0.0)
67
53
  rack (>= 1.3.0)
68
54
  rack-accept
69
- grape-entity (0.8.0)
55
+ grape-entity (0.10.1)
70
56
  activesupport (>= 3.0.0)
71
57
  multi_json (>= 1.3.2)
72
- i18n (1.8.2)
58
+ i18n (1.9.0)
73
59
  concurrent-ruby (~> 1.0)
74
- ice_nine (0.11.2)
75
- jaro_winkler (1.5.4)
76
- minitest (5.14.0)
77
- multi_json (1.14.1)
60
+ minitest (5.15.0)
61
+ multi_json (1.15.0)
78
62
  mustermann (1.1.1)
79
63
  ruby2_keywords (~> 0.0.1)
80
64
  mustermann-grape (1.0.1)
81
65
  mustermann (>= 1.0.0)
82
- parallel (1.19.1)
83
- parser (2.7.0.4)
84
- ast (~> 2.4.0)
85
- rack (2.2.2)
66
+ parallel (1.21.0)
67
+ parser (3.1.0.0)
68
+ ast (~> 2.4.1)
69
+ rack (2.2.3)
86
70
  rack-accept (0.4.5)
87
71
  rack (>= 0.4)
88
72
  rack-cors (1.1.1)
@@ -90,44 +74,54 @@ GEM
90
74
  rack-ssl-enforcer (0.2.9)
91
75
  rack-test (1.1.0)
92
76
  rack (>= 1.0, < 3)
93
- rainbow (3.0.0)
94
- rake (13.0.1)
95
- rexml (3.2.4)
96
- rspec (3.9.0)
97
- rspec-core (~> 3.9.0)
98
- rspec-expectations (~> 3.9.0)
99
- rspec-mocks (~> 3.9.0)
100
- rspec-core (3.9.1)
101
- rspec-support (~> 3.9.1)
102
- rspec-expectations (3.9.0)
77
+ rainbow (3.1.1)
78
+ rake (13.0.6)
79
+ regexp_parser (2.2.0)
80
+ rexml (3.2.5)
81
+ rspec (3.10.0)
82
+ rspec-core (~> 3.10.0)
83
+ rspec-expectations (~> 3.10.0)
84
+ rspec-mocks (~> 3.10.0)
85
+ rspec-core (3.10.1)
86
+ rspec-support (~> 3.10.0)
87
+ rspec-expectations (3.10.2)
103
88
  diff-lcs (>= 1.2.0, < 2.0)
104
- rspec-support (~> 3.9.0)
105
- rspec-mocks (3.9.1)
89
+ rspec-support (~> 3.10.0)
90
+ rspec-mocks (3.10.2)
106
91
  diff-lcs (>= 1.2.0, < 2.0)
107
- rspec-support (~> 3.9.0)
108
- rspec-support (3.9.2)
109
- rubocop (0.80.1)
110
- jaro_winkler (~> 1.5.1)
92
+ rspec-support (~> 3.10.0)
93
+ rspec-support (3.10.3)
94
+ rubocop (1.25.0)
111
95
  parallel (~> 1.10)
112
- parser (>= 2.7.0.1)
96
+ parser (>= 3.1.0.0)
113
97
  rainbow (>= 2.2.2, < 4.0)
98
+ regexp_parser (>= 1.8, < 3.0)
114
99
  rexml
100
+ rubocop-ast (>= 1.15.1, < 2.0)
115
101
  ruby-progressbar (~> 1.7)
116
- unicode-display_width (>= 1.4.0, < 1.7)
117
- ruby-progressbar (1.10.1)
118
- ruby2_keywords (0.0.2)
102
+ unicode-display_width (>= 1.4.0, < 3.0)
103
+ rubocop-ast (1.15.1)
104
+ parser (>= 3.0.1.1)
105
+ rubocop-bsm (0.6.0)
106
+ rubocop (~> 1.0)
107
+ rubocop-performance
108
+ rubocop-rake
109
+ rubocop-rspec
110
+ rubocop-performance (1.13.2)
111
+ rubocop (>= 1.7.0, < 2.0)
112
+ rubocop-ast (>= 0.4.0)
113
+ rubocop-rake (0.6.0)
114
+ rubocop (~> 1.0)
115
+ rubocop-rspec (2.8.0)
116
+ rubocop (~> 1.19)
117
+ ruby-progressbar (1.11.0)
118
+ ruby2_keywords (0.0.5)
119
119
  sqlite3 (1.4.2)
120
- thor (1.0.1)
121
- thread_safe (0.3.6)
122
- tzinfo (1.2.6)
123
- thread_safe (~> 0.1)
124
- unicode-display_width (1.6.1)
125
- virtus (1.0.5)
126
- axiom-types (~> 0.1)
127
- coercible (~> 1.0)
128
- descendants_tracker (~> 0.0, >= 0.0.3)
129
- equalizer (~> 0.0, >= 0.0.9)
130
- zeitwerk (2.2.2)
120
+ thor (1.2.1)
121
+ tzinfo (2.0.4)
122
+ concurrent-ruby (~> 1.0)
123
+ unicode-display_width (2.1.0)
124
+ zeitwerk (2.5.3)
131
125
 
132
126
  PLATFORMS
133
127
  ruby
@@ -139,9 +133,8 @@ DEPENDENCIES
139
133
  rack-test
140
134
  rake
141
135
  rspec
142
- rubocop
136
+ rubocop-bsm
143
137
  sqlite3
144
- virtus
145
138
 
146
139
  BUNDLED WITH
147
- 2.1.2
140
+ 2.2.5
data/grape-app.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'grape-app'
3
- s.version = '0.8.5'
3
+ s.version = '0.9.0'
4
4
  s.authors = ['Black Square Media Ltd']
5
5
  s.email = ['info@blacksquaremedia.com']
6
6
  s.summary = %(Standalone Grape API apps)
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.test_files = `git ls-files -z -- spec/*`.split("\x0")
13
13
  s.executables = ['grape-app']
14
14
  s.require_paths = ['lib']
15
- s.required_ruby_version = '>= 2.5'
15
+ s.required_ruby_version = '>= 2.7'
16
16
 
17
17
  s.add_dependency 'activesupport'
18
18
  s.add_dependency 'grape', '>= 1.2'
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency 'rack-test'
28
28
  s.add_development_dependency 'rake'
29
29
  s.add_development_dependency 'rspec'
30
- s.add_development_dependency 'rubocop'
30
+ s.add_development_dependency 'rubocop-bsm'
31
31
  s.add_development_dependency 'sqlite3'
32
- s.add_development_dependency 'virtus'
32
+ s.metadata['rubygems_mfa_required'] = 'true'
33
33
  end
@@ -13,8 +13,12 @@ if defined?(ActiveRecord)
13
13
  configurations[Grape::App.env.to_s]['url'] ||= ENV['DATABASE_URL']
14
14
  end
15
15
 
16
+ if ActiveRecord.respond_to?(:default_timezone=)
17
+ ActiveRecord.default_timezone = :utc
18
+ else
19
+ ActiveRecord::Base.default_timezone = :utc
20
+ end
16
21
  ActiveRecord::Base.configurations = configurations
17
- ActiveRecord::Base.default_timezone = :utc
18
22
  ActiveRecord::Base.establish_connection(Grape::App.env.to_sym)
19
23
 
20
24
  Grape::App.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement if defined?(ActiveRecord::ConnectionAdapters::ConnectionManagement)
@@ -0,0 +1,12 @@
1
+ class Grape::App::Middleware::ConnectionManagement
2
+ def initialize(app)
3
+ @app = app
4
+ end
5
+
6
+ def call(env)
7
+ @app.call(env)
8
+ rescue ::ActiveRecord::StatementInvalid
9
+ ::ActiveRecord::Base.clear_active_connections!
10
+ raise
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Grape::App::Middleware
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :ConnectionManagement
5
+ end
@@ -33,12 +33,12 @@ namespace :db do
33
33
  ar_version = [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR].join('.')
34
34
 
35
35
  FileUtils.mkdir_p(migrations_path)
36
- File.write path, <<-MIGRATION.strip_heredoc
36
+ File.write path, <<~RUBY
37
37
  class #{name.camelize} < ActiveRecord::Migration[#{ar_version}]
38
38
  def change
39
39
  end
40
40
  end
41
- MIGRATION
41
+ RUBY
42
42
  puts path
43
43
  end
44
44
 
@@ -9,6 +9,7 @@ class API::V1 < Grape::API
9
9
  # error_response message: e.message, status: 404
10
10
  # end
11
11
 
12
- # Mount components
12
+ # Mount components:
13
+ #
13
14
  # mount API::Posts
14
15
  end
@@ -1,6 +1,16 @@
1
1
  Grape::App.configure do |config|
2
- # Force SSL
3
- # config.force_ssl = true
2
+ # Eager load code on boot. This eager loads most of your application in
3
+ # memory, allowing both threaded web servers and those relying on copy on
4
+ # write to perform better.
5
+ config.eager_load = true
6
+
7
+ # Don't raise errors on missing translations
8
+ # config.raise_on_missing_translations = false
9
+
10
+ # Force SSL, please see https://github.com/tobmatth/rack-ssl-enforcer
11
+ # for configuration options.
12
+ #
13
+ # config.force_ssl = { strict: true }
4
14
 
5
15
  # CORS is disabled by default, please see https://github.com/cyu/rack-cors
6
16
  # for configuration options.
@@ -18,7 +28,4 @@ Grape::App.configure do |config|
18
28
  # use Rack::ETag
19
29
  # insert_before Rack::Cors, Rack::ContentLength
20
30
  # end
21
-
22
- # Don't raise errors on missing translations
23
- # config.raise_on_missing_translations = false
24
31
  end
@@ -12,7 +12,7 @@ RSpec.configure do |config|
12
12
  DatabaseCleaner.strategy = :transaction
13
13
  DatabaseCleaner.clean_with :truncation
14
14
  end
15
- config.around :each do |example|
15
+ config.around do |example|
16
16
  DatabaseCleaner.cleaning { example.run }
17
17
  end
18
18
 
data/lib/grape/app.rb CHANGED
@@ -35,6 +35,7 @@ class Grape::App < Grape::API
35
35
 
36
36
  # Load app
37
37
  require_one 'app', 'api'
38
+ Zeitwerk::Loader.eager_load_all if Grape::App.config.eager_load
38
39
  end
39
40
 
40
41
  # @return [Grape::App::Configuration] the configuration
@@ -72,11 +73,19 @@ class Grape::App < Grape::API
72
73
  config = self.config
73
74
  @middleware ||= Rack::Builder.new do
74
75
  use Rack::Cors, &config.cors if config.cors
75
- use Rack::SslEnforcer if config.force_ssl
76
+
77
+ if config.force_ssl.is_a?(Hash)
78
+ use Rack::SslEnforcer, **config.force_ssl
79
+ elsif config.force_ssl
80
+ use Rack::SslEnforcer
81
+ end
82
+
76
83
  config.middleware.each do |block|
77
84
  instance_eval(&block)
78
85
  end
79
86
 
87
+ use Grape::App::Middleware::ConnectionManagement if defined?(ActiveRecord)
88
+
80
89
  run Grape::App
81
90
  end
82
91
  end
@@ -102,3 +111,4 @@ end
102
111
  require 'grape/app/configuration'
103
112
  require 'grape/app/helpers'
104
113
  require 'grape/app/inflector'
114
+ require 'grape/app/middleware'
@@ -3,14 +3,46 @@ require 'spec_helper'
3
3
  RSpec.describe Grape::App::Helpers::Caching do
4
4
  include Rack::Test::Methods
5
5
 
6
- let(:app) { TestAPI }
6
+ let :app do
7
+ helper = described_class
8
+ Class.new(Grape::API) do
9
+ format :json
7
10
 
8
- it 'should handle fresh-when' do
11
+ helpers helper
12
+
13
+ get '/articles' do
14
+ scope = Article.order(:id)
15
+ opts = params[:public] ? { public: params[:public] } : {}
16
+ fresh_when(scope, **opts)
17
+ scope.to_a
18
+ end
19
+
20
+ get '/articles/never_updated' do
21
+ article = Article.first
22
+ article.updated_at = nil
23
+
24
+ fresh_when(article, last_modified_field: :created_at)
25
+ end
26
+
27
+ get '/articles/:id' do
28
+ article = Article.first
29
+ article if stale?(article, stale_if_error: 5, extras: { a: 1, b: 2 })
30
+ end
31
+ end
32
+ end
33
+ let(:created_at) { Time.at(1515151500).utc }
34
+
35
+ before do
36
+ Article.create! title: 'Welcome', created_at: created_at, updated_at: created_at + 10
37
+ Article.create! title: 'Bye', created_at: created_at, updated_at: created_at + 20
38
+ end
39
+
40
+ it 'handles fresh-when' do
9
41
  get '/articles'
10
42
  expect(last_response.status).to eq(200)
11
43
  expect(last_response.headers).to include(
12
44
  'Content-Type' => 'application/json',
13
- 'ETag' => '975ca8804565c1a569450d61090b2743',
45
+ 'ETag' => 'a5f6c4b024510c9835d8d70cbd3ed00c',
14
46
  'Last-Modified' => 'Fri, 05 Jan 2018 11:25:20 GMT',
15
47
  )
16
48
  expect(JSON.parse(last_response.body).size).to eq(2)
@@ -30,7 +62,7 @@ RSpec.describe Grape::App::Helpers::Caching do
30
62
  expect(last_response.status).to eq(200)
31
63
  end
32
64
 
33
- it 'should handle fresh_when for records that were never updated' do
65
+ it 'handles fresh_when for records that were never updated' do
34
66
  get '/articles/never_updated'
35
67
  expect(last_response.status).to eq(200)
36
68
  expect(last_response.headers).to include(
@@ -38,7 +70,7 @@ RSpec.describe Grape::App::Helpers::Caching do
38
70
  )
39
71
  end
40
72
 
41
- it 'should support cache-control' do
73
+ it 'supports cache-control' do
42
74
  get '/articles?public=true'
43
75
  expect(last_response.status).to eq(200)
44
76
  expect(last_response.headers).to include(
@@ -46,27 +78,27 @@ RSpec.describe Grape::App::Helpers::Caching do
46
78
  )
47
79
  end
48
80
 
49
- it 'should handle stale? (with cache-control)' do
81
+ it 'handles stale? (with cache-control)' do
50
82
  get '/articles/1'
51
83
  expect(last_response.status).to eq(200)
52
84
  expect(last_response.headers).to include(
53
85
  'Cache-Control' => 'private, stale-if-error=5, a=1, b=2',
54
86
  'Content-Type' => 'application/json',
55
- 'ETag' => 'c4ca4238a0b923820dcc509a6f75849b',
87
+ 'ETag' => '0154407bafc97186a494a05e0652ff61',
56
88
  'Last-Modified' => 'Fri, 05 Jan 2018 11:25:10 GMT',
57
89
  )
58
90
  expect(JSON.parse(last_response.body)).to eq(
59
91
  'id' => 1,
60
92
  'title' => 'Welcome',
61
- 'updated_at' => '2018-01-05 11:25:10 UTC',
62
- 'created_at' => '2018-01-05 11:25:00 UTC',
93
+ 'updated_at' => '2018-01-05T11:25:10.000Z',
94
+ 'created_at' => '2018-01-05T11:25:00.000Z',
63
95
  )
64
96
 
65
97
  get '/articles/1', {}, 'HTTP_IF_NONE_MATCH' => last_response.headers['ETag']
66
98
  expect(last_response.status).to eq(304)
67
99
  expect(last_response.headers).to include(
68
100
  'Cache-Control' => 'private, stale-if-error=5, a=1, b=2',
69
- 'ETag' => 'c4ca4238a0b923820dcc509a6f75849b',
101
+ 'ETag' => '0154407bafc97186a494a05e0652ff61',
70
102
  'Last-Modified' => 'Fri, 05 Jan 2018 11:25:10 GMT',
71
103
  )
72
104
  end
@@ -3,16 +3,32 @@ require 'spec_helper'
3
3
  RSpec.describe Grape::App::Helpers::Params do
4
4
  include Rack::Test::Methods
5
5
 
6
- let(:app) { TestAPI }
6
+ let :app do
7
+ helper = described_class
8
+ Class.new(Grape::API) do
9
+ format :json
7
10
 
8
- it 'should limit params' do
9
- post '/articles', title: 'Today', fresh: true, id: 1234, updated_at: Time.now
11
+ helpers helper
12
+
13
+ params do
14
+ optional :title
15
+ end
16
+ post '/articles' do
17
+ attrs = { id: 9, updated_at: Time.at(1515151515).utc }
18
+ attrs.update(declared_params)
19
+ Article.new(attrs)
20
+ end
21
+ end
22
+ end
23
+
24
+ it 'limits params' do
25
+ post '/articles', title: 'Today', id: 1234, updated_at: Time.now
10
26
  expect(last_response.status).to eq(201)
11
27
  expect(JSON.parse(last_response.body)).to eq(
12
28
  'created_at' => nil,
13
29
  'id' => 9,
14
30
  'title' => 'Today',
15
- 'updated_at' => '2018-01-05 11:25:15 UTC',
31
+ 'updated_at' => '2018-01-05T11:25:15.000Z',
16
32
  )
17
33
  end
18
34
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Grape::App::Middleware::ConnectionManagement do
4
+ include Rack::Test::Methods
5
+
6
+ let :app do
7
+ failing = ->(_) { raise(ActiveRecord::StatementInvalid) }
8
+ middleware = described_class
9
+ Rack::Builder.new do
10
+ use middleware
11
+ run failing
12
+ end
13
+ end
14
+
15
+ it 'clears active connections' do
16
+ ActiveRecord::Base.connection
17
+ expect(ActiveRecord::Base.connection_handler).to be_active_connections
18
+
19
+ expect { get '/' }.to raise_error(ActiveRecord::StatementInvalid)
20
+ expect(ActiveRecord::Base.connection_handler).not_to be_active_connections
21
+ end
22
+ end
@@ -4,37 +4,38 @@ RSpec.describe Grape::App do
4
4
  include Rack::Test::Methods
5
5
 
6
6
  subject { described_class }
7
+
7
8
  before { subject.init! File.expand_path('../scenario', __dir__) }
8
9
 
9
10
  def app
10
11
  subject.middleware
11
12
  end
12
13
 
13
- it 'should have an env' do
14
+ it 'has an env' do
14
15
  expect(subject.env).to be_instance_of(ActiveSupport::StringInquirer)
15
16
  expect(subject.env).to eq('test')
16
17
  end
17
18
 
18
- it 'should have an root' do
19
+ it 'has an root' do
19
20
  expect(subject.root).to be_instance_of(Pathname)
20
21
  end
21
22
 
22
- it 'should be an API instance' do
23
+ it 'is an API instance' do
23
24
  expect(subject).to be < Grape::API
24
25
  end
25
26
 
26
- it 'should init with default time zone' do
27
+ it 'inits with default time zone' do
27
28
  expect(Time.zone.name).to eq('UTC')
28
29
  expect(Thread.new { Time.zone }.value.name).to eq('UTC')
29
30
  end
30
31
 
31
- it 'should configure i18n' do
32
+ it 'configures i18n' do
32
33
  expect(I18n.load_path).to include(subject.root.join('config', 'locales', 'en.yml').to_s)
33
34
  expect(I18n.default_locale).to eq(:en)
34
35
  expect(I18n.exception_handler).to be_instance_of(Proc)
35
36
  end
36
37
 
37
- it 'should read env specific initializers' do
38
+ it 'reads env specific initializers' do
38
39
  expect(subject.config).to include(
39
40
  :test_specific,
40
41
  :raise_on_missing_translations,
@@ -43,13 +44,13 @@ RSpec.describe Grape::App do
43
44
  )
44
45
  end
45
46
 
46
- it 'should prepare middleware' do
47
+ it 'prepares middleware' do
47
48
  expect(subject.middleware).to be_instance_of(Rack::Builder)
48
- expect(subject.middleware.send(:instance_variable_get, :@use).size).to eq(2)
49
+ expect(subject.middleware.send(:instance_variable_get, :@use).size).to eq(3)
49
50
  expect(subject.middleware.send(:instance_variable_get, :@run)).to be(subject)
50
51
  end
51
52
 
52
- it 'should apply middleware' do
53
+ it 'applies middleware' do
53
54
  header 'Origin', 'test.host'
54
55
  get '/v1/ok'
55
56
  expect(last_response).to be_ok
@@ -1,6 +1,7 @@
1
1
  require 'my_lib'
2
2
 
3
3
  Grape::App.configure do |c|
4
+ c.eager_load = true
4
5
  c.test_specific = true
5
6
  c.raise_on_missing_translations = true
6
7
  c.cors do
data/spec/spec_helper.rb CHANGED
@@ -1,70 +1,16 @@
1
1
  ENV['RACK_ENV'] ||= 'test'
2
2
  require 'grape-app'
3
3
  require 'rack/test'
4
- require 'virtus'
4
+ require 'active_record'
5
5
 
6
- class Article
7
- include Virtus.model
8
-
9
- class Scope
10
- include Enumerable
11
-
12
- def maximum(*)
13
- map(&:updated_at).max
14
- end
15
-
16
- def each
17
- yield Article.new(id: 1, title: 'Welcome', updated_at: Time.at(1515151510).utc, created_at: Time.at(1515151500).utc)
18
- yield Article.new(id: 2, title: 'Bye', updated_at: Time.at(1515151520).utc, created_at: Time.at(1515151500).utc)
19
- end
20
- end
21
-
22
- def self.all
23
- Scope.new
24
- end
25
-
26
- attribute :id
27
- attribute :title
28
- attribute :updated_at
29
- attribute :created_at
30
-
31
- def to_param
32
- id.to_s
6
+ ActiveRecord::Base.configurations = { 'test' => { 'adapter' => 'sqlite3', 'database' => ':memory:' } }
7
+ ActiveRecord::Base.establish_connection :test
8
+ ActiveRecord::Base.connection.instance_eval do
9
+ create_table :articles do |t|
10
+ t.string :title
11
+ t.timestamps
33
12
  end
34
13
  end
35
14
 
36
- class TestAPI < Grape::API
37
- format :json
38
-
39
- helpers Grape::App::Helpers::Caching
40
- helpers Grape::App::Helpers::Params
41
-
42
- get '/articles' do
43
- scope = Article.all
44
- opts = params[:public] ? { public: params[:public] } : {}
45
- fresh_when(scope, **opts)
46
- scope.map(&:to_hash)
47
- end
48
-
49
- get '/articles/never_updated' do
50
- article = Article.all.first
51
- article.updated_at = nil
52
-
53
- fresh_when(article, last_modified_field: :created_at)
54
- end
55
-
56
- get '/articles/:id' do
57
- article = Article.all.first
58
- article.to_hash if stale?(article, stale_if_error: 5, extras: { a: 1, b: 2 })
59
- end
60
-
61
- params do
62
- requires :title
63
- optional :fresh
64
- end
65
- post '/articles' do
66
- attrs = { id: 9, updated_at: Time.at(1515151515).utc }
67
- attrs.update(declared_params)
68
- Article.new(attrs).to_hash
69
- end
15
+ class Article < ActiveRecord::Base
70
16
  end
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.8.5
4
+ version: 0.9.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: 2020-03-02 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -179,7 +179,7 @@ dependencies:
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
- name: rubocop
182
+ name: rubocop-bsm
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
@@ -206,20 +206,6 @@ dependencies:
206
206
  - - ">="
207
207
  - !ruby/object:Gem::Version
208
208
  version: '0'
209
- - !ruby/object:Gem::Dependency
210
- name: virtus
211
- requirement: !ruby/object:Gem::Requirement
212
- requirements:
213
- - - ">="
214
- - !ruby/object:Gem::Version
215
- version: '0'
216
- type: :development
217
- prerelease: false
218
- version_requirements: !ruby/object:Gem::Requirement
219
- requirements:
220
- - - ">="
221
- - !ruby/object:Gem::Version
222
- version: '0'
223
209
  description: ''
224
210
  email:
225
211
  - info@blacksquaremedia.com
@@ -229,9 +215,9 @@ extensions: []
229
215
  extra_rdoc_files: []
230
216
  files:
231
217
  - ".editorconfig"
218
+ - ".github/workflows/test.yml"
232
219
  - ".gitignore"
233
220
  - ".rubocop.yml"
234
- - ".travis.yml"
235
221
  - Gemfile
236
222
  - Gemfile.lock
237
223
  - LICENSE
@@ -250,6 +236,8 @@ files:
250
236
  - lib/grape/app/inflector.rb
251
237
  - lib/grape/app/initializers/post.rb
252
238
  - lib/grape/app/initializers/pre.rb
239
+ - lib/grape/app/middleware.rb
240
+ - lib/grape/app/middleware/connection_management.rb
253
241
  - lib/grape/app/tasks.rb
254
242
  - lib/grape/app/tasks/core.rake
255
243
  - lib/grape/app/tasks/databases.rake
@@ -269,6 +257,7 @@ files:
269
257
  - lib/grape_app.rb
270
258
  - spec/grape/app/helpers/caching_spec.rb
271
259
  - spec/grape/app/helpers/params_spec.rb
260
+ - spec/grape/app/middleware/connection_management_spec.rb
272
261
  - spec/grape/app_spec.rb
273
262
  - spec/scenario/Gemfile
274
263
  - spec/scenario/app/api.rb
@@ -282,7 +271,8 @@ files:
282
271
  homepage: https://github.com/bsm/grape-app
283
272
  licenses:
284
273
  - MIT
285
- metadata: {}
274
+ metadata:
275
+ rubygems_mfa_required: 'true'
286
276
  post_install_message:
287
277
  rdoc_options: []
288
278
  require_paths:
@@ -291,20 +281,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
291
281
  requirements:
292
282
  - - ">="
293
283
  - !ruby/object:Gem::Version
294
- version: '2.5'
284
+ version: '2.7'
295
285
  required_rubygems_version: !ruby/object:Gem::Requirement
296
286
  requirements:
297
287
  - - ">="
298
288
  - !ruby/object:Gem::Version
299
289
  version: '0'
300
290
  requirements: []
301
- rubygems_version: 3.1.2
291
+ rubygems_version: 3.3.3
302
292
  signing_key:
303
293
  specification_version: 4
304
294
  summary: Standalone Grape API apps
305
295
  test_files:
306
296
  - spec/grape/app/helpers/caching_spec.rb
307
297
  - spec/grape/app/helpers/params_spec.rb
298
+ - spec/grape/app/middleware/connection_management_spec.rb
308
299
  - spec/grape/app_spec.rb
309
300
  - spec/scenario/Gemfile
310
301
  - spec/scenario/app/api.rb
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.5
4
- - 2.6
5
- before_install:
6
- - gem install bundler