pliny 0.11.2 → 0.12.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: 1d49ad255131e44a6aaab69f54d50dbc385cbae6
4
- data.tar.gz: 75f4ffb2b579dc1964d248f090965fe90656cc3c
3
+ metadata.gz: 16aea0ec781e78a0b364f7f35e0a941a0e34d250
4
+ data.tar.gz: 99cdc6dd5bbb6aed6511befeb0cf1a17ca31694b
5
5
  SHA512:
6
- metadata.gz: 1de26c955153c5c7ef11f7ad434de3781a32ce17ae9d1cc494aa635ce1f934996d723fd0fd19006626fcfefc205f0327407b08b7070c55b06daf8507bdc08fbe
7
- data.tar.gz: ddab0b42c730b40d42bf1463173f36283023731b1c3860bfdba2e9933b6203576bea4bca48c25a42bc615563c1b6091c4825032768381b89080ffe6988951c64
6
+ metadata.gz: 52c1318c1d3dd7f46ce259b0aa4bce3666a2c01cdc98de92733b958df66b273562c4d39bcde0496654655fbe2af53cbf1e56c9e10d6967693db97476fc9f46c9
7
+ data.tar.gz: 66f0df7d564027d442f46f7bb5e249d4d99d75d8c38233a0b2ba18c161e5bc0972a3b16b198959dbf0875a3fa8252bcb9784b27003f6434a691dbce306880066
data/README.md CHANGED
@@ -128,6 +128,7 @@ Run tests:
128
128
 
129
129
  ```
130
130
  $ bundle install
131
+ $ createdb pliny-gem-test
131
132
  $ rake
132
133
  ```
133
134
 
data/lib/pliny.rb CHANGED
@@ -4,7 +4,6 @@ require "sinatra/base"
4
4
 
5
5
  require_relative "pliny/version"
6
6
  require_relative "pliny/errors"
7
- require_relative "pliny/extensions/instruments"
8
7
  require_relative "pliny/helpers/encode"
9
8
  require_relative "pliny/helpers/params"
10
9
  require_relative "pliny/log"
@@ -12,6 +11,7 @@ require_relative "pliny/request_store"
12
11
  require_relative "pliny/router"
13
12
  require_relative "pliny/utils"
14
13
  require_relative "pliny/middleware/cors"
14
+ require_relative "pliny/middleware/instruments"
15
15
  require_relative "pliny/middleware/request_id"
16
16
  require_relative "pliny/middleware/request_store"
17
17
  require_relative "pliny/middleware/rescue_errors"
@@ -10,7 +10,7 @@ module Pliny::Commands
10
10
  attr_reader :name, :stream, :options
11
11
 
12
12
  def initialize(name, options = {}, stream = $stdout)
13
- @name = normalize_name(name)
13
+ @name = name ? normalize_name(name) : nil
14
14
  @options = options
15
15
  @stream = stream
16
16
  end
@@ -1,6 +1,7 @@
1
1
  module Pliny::Helpers
2
2
  module Encode
3
3
  def encode(object)
4
+ content_type :json, charset: 'utf-8'
4
5
  MultiJson.encode(object, pretty: params[:pretty] == 'true' || Config.pretty_json)
5
6
  end
6
7
  end
@@ -0,0 +1,38 @@
1
+ module Pliny::Middleware
2
+ class Instruments
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ start = Time.now
9
+
10
+ request_ids = env["REQUEST_IDS"] ? env["REQUEST_IDS"].join(",") : nil
11
+
12
+ data = {
13
+ request_id: request_ids,
14
+ instrumentation: true,
15
+ method: env["REQUEST_METHOD"],
16
+ path: env["PATH_INFO"]
17
+ }
18
+
19
+ Pliny.log(data.merge(at: "start"))
20
+
21
+ status, headers, response = @app.call(env)
22
+
23
+ if route = env["sinatra.route"]
24
+ data.merge!(route_signature: route.split(" ").last)
25
+ end
26
+
27
+ elapsed = (Time.now - start).to_f
28
+ Pliny.log(data.merge(
29
+ at: "finish",
30
+ status: status,
31
+ length: headers["Content-Length"],
32
+ elapsed: elapsed
33
+ ))
34
+
35
+ [status, headers, response]
36
+ end
37
+ end
38
+ end
@@ -1,10 +1,6 @@
1
1
  module Endpoints
2
2
  class <%= plural_class_name %> < Base
3
3
  namespace "<%= url_path %>" do
4
- before do
5
- content_type :json, charset: 'utf-8'
6
- end
7
-
8
4
  get do
9
5
  encode []
10
6
  end
@@ -1,10 +1,6 @@
1
1
  module Endpoints
2
2
  class <%= plural_class_name %> < Base
3
3
  namespace "<%= url_path %>" do
4
- before do
5
- content_type :json, charset: 'utf-8'
6
- end
7
-
8
4
  get do
9
5
  encode serialize(<%= singular_class_name %>.all)
10
6
  end
data/lib/pliny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.11.2"
2
+ VERSION = "0.12.0"
3
3
  end
data/lib/template/Gemfile CHANGED
@@ -4,7 +4,7 @@ ruby "2.2.3"
4
4
  gem "multi_json"
5
5
  gem "oj"
6
6
  gem "pg"
7
- gem "pliny", "~> 0.11"
7
+ gem "pliny", "~> 0.12"
8
8
  gem "pry"
9
9
  gem "puma", "~> 2.10"
10
10
  gem "rack-ssl"
@@ -1,2 +1 @@
1
1
  web: bundle exec puma --config config/puma.rb config.ru
2
- console: bundle exec bin/console
@@ -18,19 +18,19 @@ module Config
18
18
  optional :versioning_app_name, string
19
19
 
20
20
  # Override -- value is returned or the set default.
21
+ override :database_timeout, 10, int
21
22
  override :db_pool, 5, int
22
23
  override :deployment, 'production', string
24
+ override :force_ssl, true, bool
25
+ override :pliny_env, 'development', string
23
26
  override :port, 5000, int
27
+ override :pretty_json, false, bool
24
28
  override :puma_max_threads, 16, int
25
29
  override :puma_min_threads, 1, int
26
30
  override :puma_workers, 3, int
27
31
  override :rack_env, 'development', string
28
- override :pliny_env, 'development', string
29
32
  override :raise_errors, false, bool
30
33
  override :root, File.expand_path("../../", __FILE__), string
31
34
  override :timeout, 10, int
32
- override :database_timeout, 10, int
33
- override :force_ssl, true, bool
34
35
  override :versioning, false, bool
35
- override :pretty_json, false, bool
36
36
  end
@@ -1,7 +1,6 @@
1
1
  module Endpoints
2
2
  # The base class for all Sinatra-based endpoints. Use sparingly.
3
3
  class Base < Sinatra::Base
4
- register Pliny::Extensions::Instruments
5
4
  register Sinatra::Namespace
6
5
 
7
6
  helpers Pliny::Helpers::Encode
@@ -14,7 +13,7 @@ module Endpoints
14
13
 
15
14
  configure :development do
16
15
  register Sinatra::Reloader
17
- also_reload '../**/*.rb'
16
+ also_reload "#{Config.root}/lib/**/*.rb"
18
17
  end
19
18
 
20
19
  error Sinatra::NotFound do
@@ -2,6 +2,7 @@ Routes = Rack::Builder.new do
2
2
  use Rollbar::Middleware::Sinatra
3
3
  use Pliny::Middleware::CORS
4
4
  use Pliny::Middleware::RequestID
5
+ use Pliny::Middleware::Instruments
5
6
  use Pliny::Middleware::RescueErrors, raise: Config.raise_errors?
6
7
  use Pliny::Middleware::RequestStore, store: Pliny::RequestStore
7
8
  use Rack::Timeout if Config.timeout > 0
@@ -8,31 +8,14 @@
8
8
  ENV["RACK_ENV"] = "test"
9
9
 
10
10
  require "bundler"
11
+ require "dotenv"
11
12
  Bundler.require(:default, :test)
12
-
13
- # setting ENV["CI"] configures simplecov for continuous integration output
14
- # setting ENV["COVERAGE"] generates a report when running tests locally
15
- if ENV["COVERAGE"] || ENV["CI"]
16
- require "simplecov"
17
- if ENV["CI"]
18
- SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter
19
- SimpleCov.at_exit do
20
- puts SimpleCov.result.format!
21
- end
22
- end
23
- SimpleCov.start do
24
- # without this the simple formatter won't do anything
25
- add_group "lib", "lib"
26
- end
27
- end
28
-
29
- require 'dotenv'
30
13
  Dotenv.load('.env.test')
31
14
 
32
15
  require_relative "../lib/initializer"
33
16
 
34
17
  # pull in test initializers
35
- Pliny::Utils.require_glob("#{Config.root}/spec/support/**/*.rb")
18
+ Pliny::Utils.require_glob("#{Config.root}/spec/spec_support/**/*.rb")
36
19
 
37
20
  RSpec.configure do |config|
38
21
  config.before :suite do
@@ -0,0 +1,15 @@
1
+ # setting ENV["CI"] configures simplecov for continuous integration output
2
+ # setting ENV["COVERAGE"] generates a report when running tests locally
3
+ if ENV["COVERAGE"] || ENV["CI"]
4
+ require "simplecov"
5
+ if ENV["CI"]
6
+ SimpleCov.formatter = SimpleCov::Formatter::SimpleFormatter
7
+ SimpleCov.at_exit do
8
+ puts SimpleCov.result.format!
9
+ end
10
+ end
11
+ SimpleCov.start do
12
+ # without this the simple formatter won't do anything
13
+ add_group "lib", "lib"
14
+ end
15
+ end
File without changes
@@ -1,3 +1,4 @@
1
+ require 'pliny/commands/creator'
1
2
  require 'pliny/commands/generator'
2
3
  require 'pliny/commands/generator/schema'
3
4
  require 'spec_helper'
@@ -15,6 +16,7 @@ describe Pliny::Commands::Generator::Schema do
15
16
  end
16
17
  end
17
18
 
19
+
18
20
  describe '#create' do
19
21
  context 'with new layout' do
20
22
  before do
@@ -44,6 +46,14 @@ describe Pliny::Commands::Generator::Schema do
44
46
  end
45
47
 
46
48
  describe '#rebuild' do
49
+ context 'with nil as the name argument (as used in schema.rake)' do
50
+ it 'rebuilds the schema with prmd' do
51
+ assert_output(/rebuilt/) do
52
+ Pliny::Commands::Generator::Schema.new(nil).rebuild
53
+ end
54
+ end
55
+ end
56
+
47
57
  context 'with new layout' do
48
58
  before do
49
59
  subject.rebuild
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe Pliny::Helpers::Encode do
4
+ def app
5
+ Sinatra.new do
6
+ helpers Pliny::Helpers::Encode
7
+ post "/" do
8
+ encode(params)
9
+ end
10
+ end
11
+ end
12
+
13
+ before do
14
+ stub(Config).pretty_json { false }
15
+ end
16
+
17
+ it "sets the Content-Type" do
18
+ post "/"
19
+ assert_equal "application/json;charset=utf-8",
20
+ last_response.headers["Content-Type"]
21
+ end
22
+
23
+ it "encodes as json" do
24
+ payload = { "foo" => "bar" }
25
+ post "/", payload
26
+ assert_equal MultiJson.encode(payload), last_response.body
27
+ end
28
+
29
+ it "encodes in pretty mode when pretty=true" do
30
+ payload = { "foo" => "bar", "pretty" => "true" }
31
+ post "/", payload
32
+ assert_equal MultiJson.encode(payload, pretty: true), last_response.body
33
+ end
34
+
35
+ it "encodes in pretty mode when set by config" do
36
+ stub(Config).pretty_json { true }
37
+ payload = { "foo" => "bar" }
38
+ post "/", payload
39
+ assert_equal MultiJson.encode(payload, pretty: true), last_response.body
40
+ end
41
+ end
@@ -1,10 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Pliny::Extensions::Instruments do
3
+ describe Pliny::Middleware::Instruments do
4
4
  def app
5
5
  Rack::Builder.new do
6
6
  run Sinatra.new {
7
- register Pliny::Extensions::Instruments
7
+ use Pliny::Middleware::Instruments
8
8
 
9
9
  error Pliny::Errors::Error do
10
10
  Pliny::Errors::Error.render(env["sinatra.error"])
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.11.2
4
+ version: 0.12.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-09-09 00:00:00.000000000 Z
12
+ date: 2015-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -369,11 +369,11 @@ files:
369
369
  - lib/pliny/config_helpers.rb
370
370
  - lib/pliny/db_support.rb
371
371
  - lib/pliny/errors.rb
372
- - lib/pliny/extensions/instruments.rb
373
372
  - lib/pliny/helpers/encode.rb
374
373
  - lib/pliny/helpers/params.rb
375
374
  - lib/pliny/log.rb
376
375
  - lib/pliny/middleware/cors.rb
376
+ - lib/pliny/middleware/instruments.rb
377
377
  - lib/pliny/middleware/request_id.rb
378
378
  - lib/pliny/middleware/request_store.rb
379
379
  - lib/pliny/middleware/rescue_errors.rb
@@ -431,8 +431,9 @@ files:
431
431
  - lib/template/schema/meta.json
432
432
  - lib/template/schema/schemata/.gitkeep
433
433
  - lib/template/spec/spec_helper.rb
434
- - lib/template/spec/support/auto_define_rack_app.rb
435
- - lib/template/spec/support/log.rb
434
+ - lib/template/spec/spec_support/auto_define_rack_app.rb
435
+ - lib/template/spec/spec_support/coverage.rb
436
+ - lib/template/spec/spec_support/log.rb
436
437
  - spec/commands/creator_spec.rb
437
438
  - spec/commands/generator/base_spec.rb
438
439
  - spec/commands/generator/endpoint_spec.rb
@@ -441,10 +442,11 @@ files:
441
442
  - spec/commands/updater_spec.rb
442
443
  - spec/db_support_spec.rb
443
444
  - spec/errors_spec.rb
444
- - spec/extensions/instruments_spec.rb
445
+ - spec/helpers/encode_spec.rb
445
446
  - spec/integration_spec.rb
446
447
  - spec/log_spec.rb
447
448
  - spec/middleware/cors_spec.rb
449
+ - spec/middleware/instruments_spec.rb
448
450
  - spec/middleware/request_id_spec.rb
449
451
  - spec/middleware/request_store_spec.rb
450
452
  - spec/middleware/rescue_errors_spec.rb
@@ -1,38 +0,0 @@
1
- module Pliny::Extensions
2
- module Instruments
3
- def self.registered(app)
4
- app.before do
5
- @request_start = Time.now
6
- Pliny.log(
7
- instrumentation: true,
8
- at: "start",
9
- method: request.request_method,
10
- path: request.path_info,
11
- )
12
- end
13
-
14
- app.after do
15
- Pliny.log(
16
- instrumentation: true,
17
- at: "finish",
18
- method: request.request_method,
19
- path: request.path_info,
20
- route_signature: route_signature,
21
- status: status,
22
- elapsed: (Time.now - @request_start).to_f
23
- )
24
- end
25
-
26
- app.helpers do
27
- def route_signature
28
- env["ROUTE_SIGNATURE"]
29
- end
30
- end
31
- end
32
-
33
- def route(verb, path, *)
34
- condition { env["ROUTE_SIGNATURE"] = path.to_s }
35
- super
36
- end
37
- end
38
- end