pliny 0.0.3 → 0.0.4

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: cfe2c266b812feb72f369c7f4948d9e598ee887e
4
- data.tar.gz: fb25f44da90206ceffa0b43830d6c4c076778bfc
3
+ metadata.gz: 337f0a1c9784e2c41009e2aa6001ff6e79b4d5be
4
+ data.tar.gz: 9d67fc07db66c537931d4ce40c1a072077a49bdd
5
5
  SHA512:
6
- metadata.gz: 780a91278cdd16c28c911f96be667b5f399b03ec13c2707d44fe823725e9fd170e1351af0b676a60efae270e45150987f9a4f2b7e5637d607ac6196f003ccb83
7
- data.tar.gz: 06e9501612f28d03c56cf03ff08057014626fa9229522378cc5c54bed5c2d4963b6a1da4f132e6fea15e59c5a734c2d673996abb090ffd4c850510f285ea058f
6
+ metadata.gz: 92788bde19907756871bb04f9205c2221be2dcaf557aa72ea323910790712fa5e9cec12fa5fba6503bd6ba243a9ef0f338bae7a35ffbdbdf21dfa2d939571d7b
7
+ data.tar.gz: ceaea90eaeb893e0ed855a2607b19c535234b24b9cb1877468359037f05b511d173ad423e6a1557a4ea646d01872327a8daa2496eafbb250fe8ec41504ab8a34
data/README.md CHANGED
@@ -17,6 +17,7 @@ And gems/helpers to tie these together and support operations:
17
17
  - [CORS middleware](lib/pliny/middleware/cors.rb) to allow JS developers to consume your API
18
18
  - [Honeybadger](https://www.honeybadger.io/) for tracking exceptions
19
19
  - [Log helper](test/log_test.rb) that logs in [data format](https://www.youtube.com/watch?v=rpmc-wHFUBs) [to stdout](https://adam.heroku.com/past/2011/4/1/logs_are_streams_not_files)
20
+ - [Mediators](http://brandur.org/mediator) to help encapsulate more complex interactions
20
21
  - [Rspec](https://github.com/rspec/rspec) for lean and fast testing
21
22
  - [Puma](http://puma.io/) as the web server, [configured for optimal performance on Heroku](config/puma.rb)
22
23
  - [Rack-test](https://github.com/brynary/rack-test) to test the API endpoints
@@ -1,5 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "pliny"
3
+ require "optparse"
4
+ require_relative "../lib/pliny"
4
5
 
5
- Pliny::Commands::Generator.run(ARGV.dup)
6
+ ARGV.options do |options|
7
+ opts = {}
8
+
9
+ options.on("--paranoid", "Generate paranoid support in models") do
10
+ opts[:paranoid] = true
11
+ end
12
+
13
+ options.parse!
14
+
15
+ Pliny::Commands::Generator.run(ARGV.dup, opts)
16
+ end
@@ -1,5 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "pliny"
3
+ require "optparse"
4
+ require_relative "../lib/pliny"
4
5
 
5
- Pliny::Commands::Creator.run(ARGV.dup)
6
+ ARGV.options do |options|
7
+ opts = {}
8
+
9
+ options.parse!
10
+
11
+ Pliny::Commands::Creator.run(ARGV.dup, opts)
12
+ end
@@ -1,21 +1,22 @@
1
1
  require "multi_json"
2
2
  require "sinatra/base"
3
3
 
4
- require "pliny/version"
5
- require "pliny/commands/creator"
6
- require "pliny/commands/generator"
7
- require "pliny/errors"
8
- require "pliny/extensions/instruments"
9
- require "pliny/log"
10
- require "pliny/request_store"
11
- require "pliny/router"
12
- require "pliny/utils"
13
- require "pliny/middleware/cors"
14
- require "pliny/middleware/request_id"
15
- require "pliny/middleware/request_store"
16
- require "pliny/middleware/rescue_errors"
17
- require "pliny/middleware/timeout"
18
- require "pliny/middleware/versioning"
4
+ require_relative "pliny/version"
5
+ require_relative "pliny/commands/creator"
6
+ require_relative "pliny/commands/generator"
7
+ require_relative "pliny/errors"
8
+ require_relative "pliny/extensions/instruments"
9
+ require_relative "pliny/helpers/params"
10
+ require_relative "pliny/log"
11
+ require_relative "pliny/request_store"
12
+ require_relative "pliny/router"
13
+ require_relative "pliny/utils"
14
+ require_relative "pliny/middleware/cors"
15
+ require_relative "pliny/middleware/request_id"
16
+ require_relative "pliny/middleware/request_store"
17
+ require_relative "pliny/middleware/rescue_errors"
18
+ require_relative "pliny/middleware/timeout"
19
+ require_relative "pliny/middleware/versioning"
19
20
 
20
21
  module Pliny
21
22
  extend Log
@@ -2,14 +2,15 @@ require "fileutils"
2
2
 
3
3
  module Pliny::Commands
4
4
  class Creator
5
- attr_accessor :args, :stream
5
+ attr_accessor :args, :opts, :stream
6
6
 
7
- def self.run(args, stream=$stdout)
8
- new(args, stream).run!
7
+ def self.run(args, opts={}, stream=$stdout)
8
+ new(args, opts, stream).run!
9
9
  end
10
10
 
11
- def initialize(args={}, stream=$stdout)
11
+ def initialize(args={}, opts={}, stream=$stdout)
12
12
  @args = args
13
+ @opts = opts
13
14
  @stream = stream
14
15
  end
15
16
 
@@ -20,12 +21,31 @@ module Pliny::Commands
20
21
 
21
22
  FileUtils.copy_entry template_dir, app_dir
22
23
  FileUtils.rm_rf("#{app_dir}/.git")
24
+ setup_database_urls
23
25
  display "Pliny app created. To start, run:"
24
26
  display "cd #{app_dir} && bin/setup"
25
27
  end
26
28
 
27
29
  protected
28
30
 
31
+ def setup_database_urls
32
+ db = URI.parse("postgres:///#{name}")
33
+ {
34
+ ".env.sample" => "development",
35
+ ".env.test" => "test"
36
+ }.each do |env_file, db_env_suffix|
37
+ env_path = "#{app_dir}/#{env_file}"
38
+ db.path = "/#{name}-#{db_env_suffix}"
39
+ env = File.read(env_path)
40
+ File.open(env_path, "w") do |f|
41
+ # ruby's URI#to_s renders foo:/bar when there's no host
42
+ # we want foo:///bar instead!
43
+ db_url = db.to_s.sub(":/", ":///")
44
+ f.puts env.sub(/DATABASE_URL=.*/, "DATABASE_URL=#{db_url}")
45
+ end
46
+ end
47
+ end
48
+
29
49
  def display(msg)
30
50
  stream.puts msg
31
51
  end
@@ -6,14 +6,15 @@ require "prmd"
6
6
 
7
7
  module Pliny::Commands
8
8
  class Generator
9
- attr_accessor :args, :stream
9
+ attr_accessor :args, :opts, :stream
10
10
 
11
- def self.run(args, stream=$stdout)
12
- new(args).run!
11
+ def self.run(args, opts={}, stream=$stdout)
12
+ new(args, opts).run!
13
13
  end
14
14
 
15
- def initialize(args={}, stream=$stdout)
15
+ def initialize(args={}, opts={}, stream=$stdout)
16
16
  @args = args
17
+ @opts = opts
17
18
  @stream = stream
18
19
  end
19
20
 
@@ -39,8 +40,6 @@ module Pliny::Commands
39
40
  create_model
40
41
  create_model_migration
41
42
  create_model_test
42
- create_serializer
43
- create_serializer_test
44
43
  when "scaffold"
45
44
  create_endpoint(scaffold: true)
46
45
  create_endpoint_test
@@ -71,6 +70,10 @@ module Pliny::Commands
71
70
  args[1]
72
71
  end
73
72
 
73
+ def paranoid
74
+ opts[:paranoid]
75
+ end
76
+
74
77
  def singular_class_name
75
78
  name.gsub(/-/, '_').singularize.camelize
76
79
  end
@@ -92,7 +95,7 @@ module Pliny::Commands
92
95
  end
93
96
 
94
97
  def create_endpoint(options = {})
95
- endpoint = "./lib/endpoints/#{name.pluralize}.rb"
98
+ endpoint = "./lib/endpoints/#{table_name}.rb"
96
99
  template = options[:scaffold] ? "endpoint_scaffold.erb" : "endpoint.erb"
97
100
  render_template(template, endpoint, {
98
101
  plural_class_name: plural_class_name,
@@ -106,7 +109,7 @@ module Pliny::Commands
106
109
  end
107
110
 
108
111
  def create_endpoint_test
109
- test = "./spec/endpoints/#{name.pluralize}_spec.rb"
112
+ test = "./spec/endpoints/#{table_name}_spec.rb"
110
113
  render_template("endpoint_test.erb", test, {
111
114
  plural_class_name: plural_class_name,
112
115
  singular_class_name: singular_class_name,
@@ -116,7 +119,7 @@ module Pliny::Commands
116
119
  end
117
120
 
118
121
  def create_endpoint_acceptance_test(options = {})
119
- test = "./spec/acceptance/#{name.pluralize}_spec.rb"
122
+ test = "./spec/acceptance/#{table_name}_spec.rb"
120
123
  template = options[:scaffold] ? "endpoint_scaffold_acceptance_test.erb" :
121
124
  "endpoint_acceptance_test.erb"
122
125
  render_template(template, test, {
@@ -129,13 +132,13 @@ module Pliny::Commands
129
132
  end
130
133
 
131
134
  def create_mediator
132
- mediator = "./lib/mediators/#{name}.rb"
135
+ mediator = "./lib/mediators/#{field_name}.rb"
133
136
  render_template("mediator.erb", mediator, plural_class_name: plural_class_name)
134
137
  display "created mediator file #{mediator}"
135
138
  end
136
139
 
137
140
  def create_mediator_test
138
- test = "./spec/mediators/#{name}_spec.rb"
141
+ test = "./spec/mediators/#{field_name}_spec.rb"
139
142
  render_template("mediator_test.erb", test, plural_class_name: plural_class_name)
140
143
  display "created test #{test}"
141
144
  end
@@ -147,26 +150,29 @@ module Pliny::Commands
147
150
  end
148
151
 
149
152
  def create_model
150
- model = "./lib/models/#{name}.rb"
151
- render_template("model.erb", model, singular_class_name: singular_class_name)
153
+ model = "./lib/models/#{field_name}.rb"
154
+ render_template("model.erb", model,
155
+ singular_class_name: singular_class_name,
156
+ paranoid: paranoid)
152
157
  display "created model file #{model}"
153
158
  end
154
159
 
155
160
  def create_model_migration
156
161
  migration = "./db/migrate/#{Time.now.to_i}_create_#{table_name}.rb"
157
162
  render_template("model_migration.erb", migration,
158
- table_name: table_name)
163
+ table_name: table_name,
164
+ paranoid: paranoid)
159
165
  display "created migration #{migration}"
160
166
  end
161
167
 
162
168
  def create_model_test
163
- test = "./spec/models/#{name}_spec.rb"
169
+ test = "./spec/models/#{field_name}_spec.rb"
164
170
  render_template("model_test.erb", test, singular_class_name: singular_class_name)
165
171
  display "created test #{test}"
166
172
  end
167
173
 
168
174
  def create_schema
169
- schema = "./docs/schema/schemata/#{name.singularize}.yaml"
175
+ schema = "./docs/schema/schemata/#{field_name}.yaml"
170
176
  write_file(schema) do
171
177
  Prmd.init(name.singularize, yaml: true)
172
178
  end
@@ -3,18 +3,28 @@ module Pliny
3
3
  def optional(*attrs)
4
4
  attrs.each do |attr|
5
5
  instance_eval "def #{attr}; @#{attr} ||= ENV['#{attr.upcase}'] end", __FILE__, __LINE__
6
+ ConfigHelpers.add_question_method(attr)
6
7
  end
7
8
  end
8
9
 
9
10
  def mandatory(*attrs)
10
11
  attrs.each do |attr|
11
12
  instance_eval "def #{attr}; @#{attr} ||= ENV['#{attr.upcase}'] || raise('missing=#{attr.upcase}') end", __FILE__, __LINE__
13
+ ConfigHelpers.add_question_method(attr)
12
14
  end
13
15
  end
14
16
 
15
17
  def override(attrs)
16
18
  attrs.each do |attr, value|
17
19
  instance_eval "def #{attr}; @#{attr} ||= ENV['#{attr.upcase}'] || '#{value}'.to_s end", __FILE__, __LINE__
20
+ ConfigHelpers.add_question_method(attr)
21
+ end
22
+ end
23
+
24
+ def self.add_question_method(attr)
25
+ define_method "#{attr}?" do
26
+ return false if send(attr).nil?
27
+ !!(send(attr) =~ /\Atrue|yes|on\z/i || send(attr).to_i > 0)
18
28
  end
19
29
  end
20
30
  end
@@ -0,0 +1,21 @@
1
+ module Pliny::Helpers
2
+ module Params
3
+ def body_params
4
+ @body_params ||= parse_body_params
5
+ end
6
+
7
+ private
8
+
9
+ def parse_body_params
10
+ if request.content_type == "application/json"
11
+ p = indifferent_params(MultiJson.decode(request.body.read))
12
+ request.body.rewind
13
+ p
14
+ elsif request.form_data?
15
+ indifferent_params(request.POST)
16
+ else
17
+ {}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,8 @@
1
1
  module Pliny::Middleware
2
2
  class RescueErrors
3
- def initialize(app)
3
+ def initialize(app, options = {})
4
4
  @app = app
5
+ @raise = options[:raise]
5
6
  end
6
7
 
7
8
  def call(env)
@@ -9,8 +10,12 @@ module Pliny::Middleware
9
10
  rescue Pliny::Errors::Error => e
10
11
  render(e, env)
11
12
  rescue Exception => e
12
- # Pliny.log_exception(e)
13
- render(Pliny::Errors::InternalServerError.new, env)
13
+ if @raise
14
+ raise
15
+ else
16
+ # Pliny.log_exception(e)
17
+ render(Pliny::Errors::InternalServerError.new, env)
18
+ end
14
19
  end
15
20
 
16
21
  private
@@ -36,12 +36,22 @@ namespace :db do
36
36
  end
37
37
  end
38
38
 
39
+ desc "Seed the database with data"
40
+ task :seed do
41
+ if File.exist?('./db/seeds.rb')
42
+ database_urls.each do |database_url|
43
+ Sequel.connect(database_url)
44
+ load 'db/seeds.rb'
45
+ end
46
+ end
47
+ end
48
+
39
49
  desc "Reset the database"
40
- task :reset => [:nuke, :migrate]
50
+ task :reset => [:nuke, :migrate, :seed]
41
51
 
42
52
  desc "Create the database"
43
53
  task :create do
44
- db = Sequel.connect("postgres://localhost/postgres")
54
+ db = Sequel.connect("postgres:///postgres")
45
55
  database_urls.each do |database_url|
46
56
  exists = false
47
57
  name = name_from_uri(database_url)
@@ -57,7 +67,7 @@ namespace :db do
57
67
 
58
68
  desc "Drop the database"
59
69
  task :drop do
60
- db = Sequel.connect("postgres://localhost/postgres")
70
+ db = Sequel.connect("postgres:///postgres")
61
71
  database_urls.each do |database_url|
62
72
  name = name_from_uri(database_url)
63
73
  db.run(%{DROP DATABASE IF EXISTS "#{name}"})
@@ -0,0 +1,8 @@
1
+ task :schema do
2
+ require "prmd"
3
+ schemata = "./docs/schema.json"
4
+ File.open(schemata, "w") do |f|
5
+ f.puts Prmd.combine("./docs/schema/schemata", meta: "./docs/schema/meta.json")
6
+ end
7
+ puts "rebuilt #{schemata}"
8
+ end
@@ -2,7 +2,7 @@ module Endpoints
2
2
  class <%= plural_class_name %> < Base
3
3
  namespace "<%= url_path %>" do
4
4
  before do
5
- content_type :json
5
+ content_type :json, charset: 'utf-8'
6
6
  end
7
7
 
8
8
  get do
@@ -2,7 +2,7 @@ module Endpoints
2
2
  class <%= plural_class_name %> < Base
3
3
  namespace "<%= url_path %>" do
4
4
  before do
5
- content_type :json
5
+ content_type :json, charset: 'utf-8'
6
6
  end
7
7
 
8
8
  get do
@@ -11,7 +11,7 @@ module Endpoints
11
11
 
12
12
  post do
13
13
  # warning: not safe
14
- <%= field_name %> = <%= singular_class_name %>.new(params)
14
+ <%= field_name %> = <%= singular_class_name %>.new(body_params)
15
15
  <%= field_name %>.save
16
16
  status 201
17
17
  MultiJson.encode serialize(<%= field_name %>)
@@ -25,7 +25,7 @@ module Endpoints
25
25
  patch "/:id" do |id|
26
26
  <%= field_name %> = <%= singular_class_name %>.first(uuid: id) || halt(404)
27
27
  # warning: not safe
28
- #<%= field_name %>.update(params)
28
+ #<%= field_name %>.update(body_params)
29
29
  MultiJson.encode serialize(<%= field_name %>)
30
30
  end
31
31
 
@@ -9,7 +9,7 @@ describe Endpoints::<%= plural_class_name %> do
9
9
  end
10
10
 
11
11
  def schema_path
12
- App.root + "/docs/schema.json"
12
+ "./docs/schema.json"
13
13
  end
14
14
 
15
15
  before do
@@ -1,5 +1,8 @@
1
1
  class <%= singular_class_name %> < Sequel::Model
2
2
 
3
3
  plugin :timestamps
4
+ <% if paranoid %>
5
+ plugin :paranoid
6
+ <% end %>
4
7
 
5
8
  end
@@ -4,7 +4,9 @@ Sequel.migration do
4
4
  uuid :uuid, default: Sequel.function(:uuid_generate_v4), primary_key: true
5
5
  timestamptz :created_at, default: Sequel.function(:now), null: false
6
6
  timestamptz :updated_at
7
+ <% if paranoid %>
7
8
  timestamptz :deleted_at
9
+ <% end %>
8
10
  end
9
11
  end
10
12
  end
@@ -8,7 +8,7 @@
8
8
  <% ident << " " %>
9
9
  <% end %>
10
10
  <%= ident %>class <%= modules.last %> < Serializers::Base
11
- <%= ident %> def structure(args) do |arg|
11
+ <%= ident %> structure(:default) do |arg|
12
12
  <%= ident %> {
13
13
  <%= ident %> }
14
14
  <%= ident %> end
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,3 +1,6 @@
1
1
  DATABASE_URL=postgres://localhost/pliny-development
2
2
  RACK_ENV=development
3
3
  TZ=UTC
4
+ RESCUE_ERRORS=false
5
+ FORCE_SSL=false
6
+ TIMEOUT=false
@@ -1,3 +1,5 @@
1
1
  DATABASE_URL=postgres://localhost/pliny-test
2
2
  RACK_ENV=test
3
3
  TZ=UTC
4
+ RESCUE_ERRORS=false
5
+ FORCE_SSL=false
@@ -1 +1 @@
1
- 2.1.1
1
+ 2.1.2
@@ -1,7 +1,7 @@
1
1
  source "https://rubygems.org"
2
- ruby "2.1.1"
2
+ ruby "2.1.2"
3
3
 
4
- gem "honeybadger"
4
+ gem "honeybadger", '~> 1.14'
5
5
  gem "multi_json"
6
6
  gem "oj"
7
7
  gem "pg"
@@ -10,6 +10,7 @@ gem "puma"
10
10
  gem "rack-ssl"
11
11
  gem "rake"
12
12
  gem "sequel"
13
+ gem "sequel-paranoid"
13
14
  gem "sequel_pg", require: "sequel"
14
15
  gem "sinatra", require: "sinatra/base"
15
16
  gem "sinatra-contrib", require: ["sinatra/namespace", "sinatra/reloader"]
@@ -18,7 +18,7 @@ GEM
18
18
  foreman (0.67.0)
19
19
  dotenv (~> 0.7.0)
20
20
  thor (~> 0.17.0)
21
- honeybadger (1.13.0)
21
+ honeybadger (1.15.0)
22
22
  json
23
23
  http_accept (0.1.5)
24
24
  i18n (0.6.9)
@@ -27,7 +27,7 @@ GEM
27
27
  multi_json (1.10.1)
28
28
  oj (2.9.0)
29
29
  pg (0.17.1)
30
- pliny (0.0.1)
30
+ pliny (0.0.3)
31
31
  activesupport (~> 4.1, >= 4.1.0)
32
32
  http_accept (~> 0.1, >= 0.1.5)
33
33
  multi_json (~> 1.9, >= 1.9.3)
@@ -52,7 +52,9 @@ GEM
52
52
  rspec-core (2.14.8)
53
53
  rspec-expectations (2.14.5)
54
54
  diff-lcs (>= 1.1.3, < 2.0)
55
- sequel (4.10.0)
55
+ sequel (4.11.0)
56
+ sequel-paranoid (0.4.3)
57
+ sequel
56
58
  sequel_pg (1.6.9)
57
59
  pg (>= 0.8.0)
58
60
  sequel (>= 3.39.0)
@@ -70,9 +72,9 @@ GEM
70
72
  sinatra-router (0.2.3)
71
73
  sinatra (~> 1.4)
72
74
  thor (0.17.0)
73
- thread_safe (0.3.3)
75
+ thread_safe (0.3.4)
74
76
  tilt (1.4.1)
75
- tzinfo (1.1.0)
77
+ tzinfo (1.2.1)
76
78
  thread_safe (~> 0.1)
77
79
 
78
80
  PLATFORMS
@@ -82,7 +84,7 @@ DEPENDENCIES
82
84
  committee
83
85
  database_cleaner
84
86
  foreman
85
- honeybadger
87
+ honeybadger (~> 1.14)
86
88
  multi_json
87
89
  oj
88
90
  pg
@@ -95,6 +97,7 @@ DEPENDENCIES
95
97
  rspec-core
96
98
  rspec-expectations
97
99
  sequel
100
+ sequel-paranoid
98
101
  sequel_pg
99
102
  sinatra
100
103
  sinatra-contrib
@@ -19,7 +19,10 @@ module Config
19
19
 
20
20
  # Optional -- value is returned or `nil` if it wasn't present.
21
21
  optional \
22
- :placeholder
22
+ :honeybadger_api_key,
23
+ :placeholder,
24
+ :versioning_default,
25
+ :versioning_app_name
23
26
 
24
27
  # Override -- value is returned or the set default. Remember to typecast.
25
28
  override \
@@ -28,5 +31,9 @@ module Config
28
31
  puma_max_threads: 16,
29
32
  puma_min_threads: 1,
30
33
  puma_workers: 3,
31
- rack_env: "development"
34
+ rack_env: 'development',
35
+ rescue_errors: 'true',
36
+ timeout: 45,
37
+ force_ssl: 'true',
38
+ versioning: 'false'
32
39
  end
@@ -0,0 +1 @@
1
+ Sequel.connect(Config.database_url, max_connections: Config.db_pool)
@@ -1,3 +1,3 @@
1
1
  Honeybadger.configure do |config|
2
- config.api_key = ENV["HONEYBADGER_API_KEY"]
2
+ config.api_key = Config.honeybadger_api_key
3
3
  end
@@ -0,0 +1,6 @@
1
+ # This file should contain all the record creation needed to seed the database
2
+ # with its default values.
3
+ # The data can then be loaded with the rake db:seed (or created alongside the
4
+ # db with db:setup).
5
+ #
6
+ # A Sequel database connection can be obtained via Sequel::Model.db
@@ -3,6 +3,7 @@ module Endpoints
3
3
  class Base < Sinatra::Base
4
4
  register Pliny::Extensions::Instruments
5
5
  register Sinatra::Namespace
6
+ helpers Pliny::Helpers::Params
6
7
 
7
8
  configure :development do
8
9
  register Sinatra::Reloader
@@ -1,29 +1,30 @@
1
1
  module Initializer
2
2
  def self.run
3
3
  require_config
4
- initialize_database
5
4
  require_lib
6
5
  require_initializers
6
+ require_models
7
7
  end
8
8
 
9
9
  def self.require_config
10
10
  require! "config/config"
11
11
  end
12
12
 
13
- def self.initialize_database
14
- Sequel.connect(Config.database_url, max_connections: Config.db_pool)
15
- end
16
-
17
13
  def self.require_lib
18
14
  require! %w(
19
15
  lib/endpoints/base
20
16
  lib/endpoints/**/*
21
17
  lib/mediators/base
22
18
  lib/mediators/**/*
23
- lib/models/**/*
24
19
  lib/routes
25
20
  lib/serializers/base
26
- lib/serializers/**/*.rb
21
+ lib/serializers/**/*
22
+ )
23
+ end
24
+
25
+ def self.require_models
26
+ require! %w(
27
+ lib/models/**/*
27
28
  )
28
29
  end
29
30
 
@@ -1,13 +1,16 @@
1
1
  Routes = Rack::Builder.new do
2
- use Pliny::Middleware::RescueErrors unless Config.rack_env == "development"
3
- use Honeybadger::Rack
2
+ use Pliny::Middleware::RescueErrors, raise: !Config.rescue_errors?
3
+ use Honeybadger::Rack::ErrorNotifier if Config.honeybadger_api_key
4
4
  use Pliny::Middleware::CORS
5
5
  use Pliny::Middleware::RequestID
6
6
  use Pliny::Middleware::RequestStore, store: Pliny::RequestStore
7
- use Pliny::Middleware::Timeout, timeout: 45
7
+ use Pliny::Middleware::Timeout, timeout: Config.timeout.to_i if Config.timeout.to_i > 0
8
+ use Pliny::Middleware::Versioning,
9
+ default: Config.versioning_default,
10
+ app_name: Config.versioning_app_name if Config.versioning.downcase == 'true'
8
11
  use Rack::Deflater
9
12
  use Rack::MethodOverride
10
- use Rack::SSL if Config.rack_env == "production"
13
+ use Rack::SSL if Config.force_ssl.downcase == 'true'
11
14
 
12
15
  use Pliny::Router do
13
16
  # mount all endpoints here
@@ -25,6 +25,10 @@ Pliny::Utils.require_glob("#{Initializer.root}/spec/support/**/*.rb")
25
25
  RSpec.configure do |config|
26
26
  config.mock_framework = :rr
27
27
 
28
+ config.before :all do
29
+ load('db/seeds.rb') if File.exist?('db/seeds.rb')
30
+ end
31
+
28
32
  config.before :each do
29
33
  DatabaseCleaner.start
30
34
  end
@@ -2,7 +2,7 @@ require "test_helper"
2
2
 
3
3
  describe Pliny::Commands::Creator do
4
4
  before do
5
- @gen = Pliny::Commands::Creator.new(["foobar"], StringIO.new)
5
+ @gen = Pliny::Commands::Creator.new(["foobar"], {}, StringIO.new)
6
6
  end
7
7
 
8
8
  describe "#run!" do
@@ -22,5 +22,21 @@ describe Pliny::Commands::Creator do
22
22
  @gen.run!
23
23
  refute File.exists?("./foobar/.git")
24
24
  end
25
+
26
+ it "changes DATABASE_URL in .env.sample to use the app name" do
27
+ @gen.run!
28
+ db_url = File.read("./foobar/.env.sample").split("\n").detect do |line|
29
+ line.include?("DATABASE_URL=")
30
+ end
31
+ assert_equal "DATABASE_URL=postgres:///foobar-development", db_url
32
+ end
33
+
34
+ it "changes DATABASE_URL in .env.test to use the app name" do
35
+ @gen.run!
36
+ db_url = File.read("./foobar/.env.test").split("\n").detect do |line|
37
+ line.include?("DATABASE_URL=")
38
+ end
39
+ assert_equal "DATABASE_URL=postgres:///foobar-test", db_url
40
+ end
25
41
  end
26
42
  end
@@ -2,7 +2,19 @@ require "test_helper"
2
2
 
3
3
  describe Pliny::Commands::Generator do
4
4
  before do
5
- @gen = Pliny::Commands::Generator.new({}, StringIO.new)
5
+ @gen = Pliny::Commands::Generator.new({}, {}, StringIO.new)
6
+ end
7
+
8
+ describe "#field_name" do
9
+ it "uses the singular form" do
10
+ @gen.args = ["model", "resource_histories"]
11
+ assert_equal "resource_history", @gen.field_name
12
+ end
13
+
14
+ it "handles hyphens as underscores" do
15
+ @gen.args = ["model", "resource-histories"]
16
+ assert_equal "resource_history", @gen.field_name
17
+ end
6
18
  end
7
19
 
8
20
  describe "#plural_class_name" do
@@ -104,14 +116,6 @@ describe Pliny::Commands::Generator do
104
116
  it "creates a test" do
105
117
  assert File.exists?("spec/models/artist_spec.rb")
106
118
  end
107
-
108
- it "creates a serializer" do
109
- assert File.exists?("lib/serializers/artist_serializer.rb")
110
- end
111
-
112
- it "creates a serializer test" do
113
- assert File.exists?("spec/serializers/artist_serializer_spec.rb")
114
- end
115
119
  end
116
120
 
117
121
  describe "generating scaffolds" do
@@ -14,14 +14,11 @@ describe Pliny::Middleware::RescueErrors do
14
14
  end
15
15
 
16
16
  def app
17
- Rack::Builder.new do
18
- use Rack::Lint
19
- use Pliny::Middleware::RescueErrors
20
- run BadMiddleware.new
21
- end
17
+ @app
22
18
  end
23
19
 
24
20
  it "intercepts Pliny errors and renders" do
21
+ @app = new_rack_app
25
22
  get "/api-error"
26
23
  assert_equal 503, last_response.status
27
24
  error_json = MultiJson.decode(last_response.body)
@@ -31,6 +28,7 @@ describe Pliny::Middleware::RescueErrors do
31
28
  end
32
29
 
33
30
  it "intercepts exceptions and renders" do
31
+ @app = new_rack_app
34
32
  get "/"
35
33
  assert_equal 500, last_response.status
36
34
  error_json = MultiJson.decode(last_response.body)
@@ -38,4 +36,21 @@ describe Pliny::Middleware::RescueErrors do
38
36
  assert_equal "Internal server error.", error_json["message"]
39
37
  assert_equal 500, error_json["status"]
40
38
  end
39
+
40
+ it "raises given the raise option" do
41
+ @app = new_rack_app(raise: true)
42
+ assert_raises(RuntimeError) do
43
+ get "/"
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def new_rack_app(options = {})
50
+ Rack::Builder.new do
51
+ use Rack::Lint
52
+ use Pliny::Middleware::RescueErrors, raise: options[:raise]
53
+ run BadMiddleware.new
54
+ end
55
+ end
41
56
  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.0.3
4
+ version: 0.0.4
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: 2014-06-03 00:00:00.000000000 Z
12
+ date: 2014-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -205,6 +205,7 @@ files:
205
205
  - lib/pliny/config_helpers.rb
206
206
  - lib/pliny/errors.rb
207
207
  - lib/pliny/extensions/instruments.rb
208
+ - lib/pliny/helpers/params.rb
208
209
  - lib/pliny/log.rb
209
210
  - lib/pliny/middleware/cors.rb
210
211
  - lib/pliny/middleware/request_id.rb
@@ -216,6 +217,7 @@ files:
216
217
  - lib/pliny/router.rb
217
218
  - lib/pliny/tasks.rb
218
219
  - lib/pliny/tasks/db.rake
220
+ - lib/pliny/tasks/schema.rake
219
221
  - lib/pliny/tasks/test.rake
220
222
  - lib/pliny/templates/endpoint.erb
221
223
  - lib/pliny/templates/endpoint_acceptance_test.erb
@@ -248,9 +250,11 @@ files:
248
250
  - template/bin/setup
249
251
  - template/config.ru
250
252
  - template/config/config.rb
253
+ - template/config/initializers/database.rb
251
254
  - template/config/initializers/honeybadger.rb
252
255
  - template/config/puma.rb
253
256
  - template/db/schema.sql
257
+ - template/db/seeds.rb
254
258
  - template/docs/schema/meta.json
255
259
  - template/docs/schema/schemata/.gitkeep
256
260
  - template/lib/endpoints/base.rb
@@ -296,8 +300,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
296
300
  version: '0'
297
301
  requirements: []
298
302
  rubyforge_project:
299
- rubygems_version: 2.2.1
303
+ rubygems_version: 2.2.2
300
304
  signing_key:
301
305
  specification_version: 4
302
306
  summary: Basic tooling to support API apps in Sinatra
303
307
  test_files: []
308
+ has_rdoc: