pliny 0.13.1 → 0.14.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
  SHA1:
3
- metadata.gz: 6f4e78e4099e7a811247f8cf6bbb1d2ee8850716
4
- data.tar.gz: a7051ec4fe9bfa69c7c817b4fa5f271be119dc1e
3
+ metadata.gz: 13b0d6020bd07cb9d55632ccc06ee95bf4114fbd
4
+ data.tar.gz: 40fd92d34a2c2c75e124a5893454335b14ffdd1b
5
5
  SHA512:
6
- metadata.gz: a116d663a3409d028f39906dd30308ae5a3cf55b8b60334af914001c39f6c5281e5b329056cbd37b968132335a08566371f73a0df0ddf050f46fbda9daab45e0
7
- data.tar.gz: d5a2a86e30b5cb2146d0c684f374d85b3d794abe212962d041ac91be861b0006cb691fe29f430b6d61e825f847860e9481df29fae57e2929c012951fdd5bc279
6
+ metadata.gz: c240442d80aea58d7327885bb1c573152ed5d831420da2b6f0e07e0c7838101c8d5aeeed1e44a8ebdb344fe36dfba0b8679ccd7e1c4b82b773c1af7685dac509
7
+ data.tar.gz: dfc01de138d783f33cc28b640382155311e363e4137e4791fb9e15a07b93df2a72350d5ec983b81face3c9fb5d8912fe116d6ce4a1971c9c29d51a7afc885000
data/lib/pliny.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "multi_json"
2
- require "rack-timeout"
3
2
  require "sinatra/base"
4
3
 
5
4
  require_relative "pliny/version"
@@ -34,7 +34,9 @@ begin
34
34
  task :seed do
35
35
  if File.exist?('./db/seeds.rb')
36
36
  database_urls.each do |database_url|
37
- Sequel.connect(database_url)
37
+ # make a DB instance available to the seeds file
38
+ self.class.send(:remove_const, 'DB') if self.class.const_defined?('DB')
39
+ self.class.const_set('DB', Sequel.connect(database_url))
38
40
  load 'db/seeds.rb'
39
41
  end
40
42
  disconnect
data/lib/pliny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.13.1"
2
+ VERSION = "0.14.0"
3
3
  end
data/lib/template/Gemfile CHANGED
@@ -4,10 +4,11 @@ ruby "2.2.3"
4
4
  gem "multi_json"
5
5
  gem "oj"
6
6
  gem "pg"
7
- gem "pliny", "~> 0.13"
7
+ gem "pliny", "~> 0.14"
8
8
  gem "pry"
9
9
  gem "puma", "~> 2.10"
10
10
  gem "rack-ssl"
11
+ gem "rack-timeout", "~> 0.3"
11
12
  gem "rake"
12
13
  gem "rollbar", require: "rollbar/middleware/sinatra"
13
14
  gem "sequel", "~> 4.16"
@@ -5,6 +5,6 @@ database_setup_proc = lambda do |conn|
5
5
  conn.execute "SET application_name = '#{process_identifier}'"
6
6
  end
7
7
 
8
- Sequel.connect(Config.database_url,
8
+ DB = Sequel.connect(Config.database_url,
9
9
  max_connections: Config.db_pool,
10
10
  after_connect: database_setup_proc)
@@ -4,6 +4,5 @@
4
4
  # db with db:setup).
5
5
  #
6
6
  # Seeding can occur multiple times during the execution of a single Rake task
7
- # because seeding should occur in all environments (development, testing,
8
- # etc.). The currently connected database can be accessed via
9
- # `Sequel::DATABASES.last`.
7
+ # because it applies to all your app environments (eg: test and development).
8
+ # An instance of the current database connection is available as `DB`.
@@ -0,0 +1,29 @@
1
+ module Endpoints
2
+ class Health < Base
3
+ namespace '/health' do
4
+ get do
5
+ encode({})
6
+ end
7
+
8
+ get '/db' do
9
+ database?
10
+ database_available?
11
+ encode({})
12
+ end
13
+
14
+ private
15
+
16
+ def database?
17
+ fail Pliny::Errors::NotFound if DB.nil?
18
+ end
19
+
20
+ def database_available?
21
+ fail Pliny::Errors::ServiceUnavailable unless DB.test_connection
22
+ rescue Sequel::Error => e
23
+ message = e.message.strip
24
+ Pliny.log(db: true, health: true, at: 'exception', message: message)
25
+ raise Pliny::Errors::ServiceUnavailable
26
+ end
27
+ end
28
+ end
29
+ end
@@ -15,6 +15,7 @@ Routes = Rack::Builder.new do
15
15
 
16
16
  use Pliny::Router do
17
17
  # mount all endpoints here
18
+ mount Endpoints::Health
18
19
  mount Endpoints::Schema
19
20
  end
20
21
 
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Endpoints::Health do
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ Endpoints::Health
8
+ end
9
+
10
+ describe 'GET /health' do
11
+ it 'returns a 200' do
12
+ get '/health'
13
+ assert_equal(200, last_response.status)
14
+ assert_equal('application/json;charset=utf-8', last_response.headers['Content-Type'])
15
+ assert_equal(2, last_response.headers['Content-Length'].to_i)
16
+ assert_equal({}, MultiJson.decode(last_response.body))
17
+ end
18
+ end
19
+
20
+ describe 'GET /health/db' do
21
+ it 'raises a 404 when no database is available' do
22
+ allow(DB).to receive(:nil?).and_return(true)
23
+
24
+ assert_raises Pliny::Errors::NotFound do
25
+ get '/health/db'
26
+ end
27
+ end
28
+
29
+ it 'raises a 503 on Sequel exceptions' do
30
+ allow(DB).to receive(:test_connection).and_raise(Sequel::Error)
31
+
32
+ assert_raises Pliny::Errors::ServiceUnavailable do
33
+ get '/health/db'
34
+ end
35
+ end
36
+
37
+ it 'raises a 503 when connection testing fails' do
38
+ allow(DB).to receive(:test_connection).and_return(false)
39
+
40
+ assert_raises Pliny::Errors::ServiceUnavailable do
41
+ get '/health/db'
42
+ end
43
+ end
44
+
45
+ it 'returns a 200' do
46
+ allow(DB).to receive(:test_connection).and_return(true)
47
+
48
+ get '/health/db'
49
+ assert_equal(200, last_response.status)
50
+ assert_equal('application/json;charset=utf-8', last_response.headers['Content-Type'])
51
+ assert_equal(2, last_response.headers['Content-Length'].to_i)
52
+ assert_equal({}, MultiJson.decode(last_response.body))
53
+ end
54
+ end
55
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pliny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.1
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur Leach
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-11-12 00:00:00.000000000 Z
12
+ date: 2015-12-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -65,26 +65,6 @@ dependencies:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
67
  version: 0.7.0
68
- - !ruby/object:Gem::Dependency
69
- name: rack-timeout
70
- requirement: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '0.3'
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: 0.3.1
78
- type: :runtime
79
- prerelease: false
80
- version_requirements: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - "~>"
83
- - !ruby/object:Gem::Version
84
- version: '0.3'
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: 0.3.1
88
68
  - !ruby/object:Gem::Dependency
89
69
  name: sinatra
90
70
  requirement: !ruby/object:Gem::Requirement
@@ -422,6 +402,7 @@ files:
422
402
  - lib/template/db/seeds.rb
423
403
  - lib/template/lib/application.rb
424
404
  - lib/template/lib/endpoints/base.rb
405
+ - lib/template/lib/endpoints/health.rb
425
406
  - lib/template/lib/endpoints/root.rb
426
407
  - lib/template/lib/endpoints/schema.rb
427
408
  - lib/template/lib/initializer.rb
@@ -431,6 +412,7 @@ files:
431
412
  - lib/template/lib/tasks/spec.rake
432
413
  - lib/template/schema/meta.json
433
414
  - lib/template/schema/schemata/.gitkeep
415
+ - lib/template/spec/endpoints/health_spec.rb
434
416
  - lib/template/spec/endpoints/schema_spec.rb
435
417
  - lib/template/spec/spec_helper.rb
436
418
  - lib/template/spec/spec_support/auto_define_rack_app.rb
@@ -482,7 +464,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
482
464
  version: '0'
483
465
  requirements: []
484
466
  rubyforge_project:
485
- rubygems_version: 2.4.5
467
+ rubygems_version: 2.4.3
486
468
  signing_key:
487
469
  specification_version: 4
488
470
  summary: Basic tooling to support API apps in Sinatra