pliny 0.10.0 → 0.11.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4dbcb16bc8dfd3e1ccf6631213e387368cd0a924
4
- data.tar.gz: db1bdaa2c246b5fe2a3209c73810113223fcb336
3
+ metadata.gz: 4e0f67945b6d0c56fbc12eeeb1f05c27552d34ec
4
+ data.tar.gz: 0561658c246936f0e5aea4d84bb9594bf547901d
5
5
  SHA512:
6
- metadata.gz: 04092bcc6e1883e1eddfe41ae889011ccd51adfb586d0e67529a439ffbced16f0eb59c8a38b143bd1804e6cdef25c96be6fce27d045f78866207dcd254318eed
7
- data.tar.gz: af57c21f2048c3fdec9ea622179f6ea1be80ad32878c43131323449e034385777465db789ee47b4f6251084dbf7cc56372240945605fef1dc80f80987e5ef367
6
+ metadata.gz: fc4d4d529b7f99a29fad0261059bea8e038cb5e516fa1f6145d5c549b3e63d19801cbae9889ad30bf25eeec6073fcc35becf4fd190df4e05725e5db8cb3bfa28
7
+ data.tar.gz: b49455e9fe4a7d1064df6fa29903b35f6be59a297263ef6c3d90f3078a73603184ef1f2053cff12a28ea59a0f200989fde44b3384ff66e29891e7cc44f99c88d
data/bin/pliny-new CHANGED
@@ -3,10 +3,19 @@
3
3
  require "optparse"
4
4
  require_relative '../lib/pliny/commands/creator'
5
5
 
6
- ARGV.options do |options|
6
+ OptionParser.new do |options|
7
7
  opts = {}
8
+ options.banner = "Usage: pliny-new app-name [options]"
8
9
 
9
- options.parse!
10
-
11
- Pliny::Commands::Creator.run(ARGV.dup, opts)
10
+ begin
11
+ options.parse!
12
+ if ARGV.empty?
13
+ puts options.banner
14
+ else
15
+ Pliny::Commands::Creator.run(ARGV.dup, opts)
16
+ end
17
+ rescue OptionParser::InvalidOption => e
18
+ puts e
19
+ puts options.banner
20
+ end
12
21
  end
@@ -59,9 +59,9 @@ module Pliny::Commands
59
59
  # take a diff of changes that happened to the template app in Pliny
60
60
  diff = `cd #{repo_dir} && git diff v#{curr}..v#{target} lib/template/`
61
61
 
62
- # remove lib/template from the path of files in the patch so that we can
62
+ # remove /lib/template from the path of files in the patch so that we can
63
63
  # apply these to the current folder
64
- diff.gsub!(/^(\-\-\-|\+\+\+) (\w)\/lib\/template/, '\1 \2')
64
+ diff.gsub!(/(\w)\/lib\/template/, '\1')
65
65
 
66
66
  File.open(patch_file, "w") { |f| f.puts diff }
67
67
  end
@@ -35,6 +35,20 @@ module Pliny
35
35
  ->(v) { v.to_sym }
36
36
  end
37
37
 
38
+ # optional :accronyms, array(string)
39
+ # => ['a', 'b']
40
+ # optional :numbers, array(int)
41
+ # => [1, 2]
42
+ # optional :notype, array
43
+ # => ['a', 'b']
44
+ def array(method = nil)
45
+ -> (v) do
46
+ if v
47
+ v.split(",").map{|a| cast(a, method) }
48
+ end
49
+ end
50
+ end
51
+
38
52
  private
39
53
 
40
54
  def cast(value, method)
@@ -4,27 +4,46 @@ require "sequel/extensions/migration"
4
4
 
5
5
  module Pliny
6
6
  class DbSupport
7
- @@logger = nil
7
+ def self.admin_url(database_url)
8
+ uri = URI.parse(database_url)
9
+ uri.path = "/postgres"
10
+ uri.to_s
11
+ end
8
12
 
9
- def self.logger=(logger)
10
- @@logger=logger
13
+ def self.setup?(database_url)
14
+ @db = Sequel.connect(database_url)
15
+ @db.test_connection
16
+ @db.disconnect
17
+ return true
18
+ rescue Sequel::DatabaseConnectionError
19
+ return false
11
20
  end
12
21
 
13
- def self.run(url)
14
- instance = new(url)
22
+ def self.run(url, sequel_log_io=StringIO.new)
23
+ logger = Logger.new(sequel_log_io)
24
+ instance = new(url, logger)
15
25
  yield instance
16
26
  instance.disconnect
17
27
  end
18
28
 
19
29
  attr_accessor :db
20
30
 
21
- def initialize(url)
31
+ def initialize(url, sequel_logger)
22
32
  @db = Sequel.connect(url)
23
- if @@logger
24
- @db.loggers << @@logger
33
+ if sequel_logger
34
+ @db.loggers << sequel_logger
25
35
  end
26
36
  end
27
37
 
38
+ def exists?(name)
39
+ res = db.fetch("SELECT 1 FROM pg_database WHERE datname = ?", name)
40
+ return res.count > 0
41
+ end
42
+
43
+ def create(name)
44
+ db.run(%{CREATE DATABASE "#{name}"})
45
+ end
46
+
28
47
  def migrate(target=nil)
29
48
  Sequel::Migrator.apply(db, "./db/migrate", target)
30
49
  end
data/lib/pliny/errors.rb CHANGED
@@ -5,7 +5,7 @@ module Pliny
5
5
 
6
6
  def self.render(error)
7
7
  headers = { "Content-Type" => "application/json; charset=utf-8" }
8
- data = { id: error.id, message: error.message, status: error.status }
8
+ data = { id: error.id, message: error.message }
9
9
  [error.status, headers, [MultiJson.encode(data)]]
10
10
  end
11
11
 
@@ -6,14 +6,12 @@ require "uri"
6
6
  require "pliny/db_support"
7
7
  require "pliny/utils"
8
8
 
9
- Pliny::DbSupport.logger = Logger.new($stdout)
10
-
11
9
  namespace :db do
12
10
  desc "Run database migrations"
13
11
  task :migrate do
14
12
  next if Dir["./db/migrate/*.rb"].empty?
15
13
  database_urls.each do |database_url|
16
- Pliny::DbSupport.run(database_url) do |helper|
14
+ Pliny::DbSupport.run(database_url, $stdout) do |helper|
17
15
  helper.migrate
18
16
  puts "Migrated `#{name_from_uri(database_url)}`"
19
17
  end
@@ -24,7 +22,7 @@ namespace :db do
24
22
  task :rollback do
25
23
  next if Dir["./db/migrate/*.rb"].empty?
26
24
  database_urls.each do |database_url|
27
- Pliny::DbSupport.run(database_url) do |helper|
25
+ Pliny::DbSupport.run(database_url, $stdout) do |helper|
28
26
  helper.rollback
29
27
  puts "Rolled back `#{name_from_uri(database_url)}`"
30
28
  end
@@ -45,16 +43,16 @@ namespace :db do
45
43
  desc "Create the database"
46
44
  task :create do
47
45
  database_urls.each do |database_url|
48
- db = Sequel.connect("#{postgres_location_from_uri(database_url)}/postgres")
49
- exists = false
50
- name = name_from_uri(database_url)
51
- begin
52
- db.run(%{CREATE DATABASE "#{name}"})
53
- rescue Sequel::DatabaseError
54
- raise unless $!.message =~ /already exists/
55
- exists = true
46
+ db_name = name_from_uri(database_url)
47
+ if Pliny::DbSupport.setup?(database_url)
48
+ puts "Skipping `#{db_name}`, already exists"
49
+ else
50
+ admin_url = Pliny::DbSupport.admin_url(database_url)
51
+ Pliny::DbSupport.run(admin_url) do |helper|
52
+ helper.create(db_name)
53
+ puts "Created `#{db_name}`"
54
+ end
56
55
  end
57
- puts "Created `#{name}`" if !exists
58
56
  end
59
57
  disconnect
60
58
  end
@@ -62,7 +60,8 @@ namespace :db do
62
60
  desc "Drop the database"
63
61
  task :drop do
64
62
  database_urls.each do |database_url|
65
- db = Sequel.connect("#{postgres_location_from_uri(database_url)}/postgres")
63
+ admin_url = Pliny::DbSupport.admin_url(database_url)
64
+ db = Sequel.connect(admin_url)
66
65
  name = name_from_uri(database_url)
67
66
  db.run(%{DROP DATABASE IF EXISTS "#{name}"})
68
67
  puts "Dropped `#{name}`"
@@ -146,10 +145,4 @@ namespace :db do
146
145
  def name_from_uri(uri)
147
146
  URI.parse(uri).path[1..-1]
148
147
  end
149
-
150
- def postgres_location_from_uri(uri)
151
- p = URI.parse(uri)
152
- p.path = ""
153
- p.to_s
154
- end
155
148
  end
data/lib/pliny/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
data/lib/template/Gemfile CHANGED
@@ -4,7 +4,7 @@ ruby "2.2.2"
4
4
  gem "multi_json"
5
5
  gem "oj"
6
6
  gem "pg"
7
- gem "pliny", "~> 0.10"
7
+ gem "pliny", "~> 0.11"
8
8
  gem "pry"
9
9
  gem "puma", "~> 2.10"
10
10
  gem "rack-ssl"
@@ -3,4 +3,7 @@
3
3
  # The data can then be loaded with the rake db:seed (or created alongside the
4
4
  # db with db:setup).
5
5
  #
6
- # A Sequel database connection can be obtained via Sequel::Model.db
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`.
@@ -18,9 +18,7 @@ module Endpoints
18
18
  end
19
19
 
20
20
  error Sinatra::NotFound do
21
- content_type :json
22
- status 404
23
- "{}"
21
+ raise Pliny::Errors::NotFound
24
22
  end
25
23
  end
26
24
  end
@@ -2,9 +2,11 @@ require "spec_helper"
2
2
  require "pliny/db_support"
3
3
 
4
4
  describe Pliny::DbSupport do
5
- let(:support) { Pliny::DbSupport.new(ENV["TEST_DATABASE_URL"]) }
5
+ let(:url) { ENV["TEST_DATABASE_URL"] }
6
+ let(:logger) { Logger.new(StringIO.new) }
7
+ let(:support) { Pliny::DbSupport.new(url, logger) }
6
8
 
7
- before(:all) do
9
+ before do
8
10
  @path = "/tmp/pliny-test"
9
11
  end
10
12
 
@@ -15,6 +17,20 @@ describe Pliny::DbSupport do
15
17
  Dir.chdir(@path)
16
18
  end
17
19
 
20
+ describe ".admin_url" do
21
+ it "connects to the postgres system's db" do
22
+ assert_equal "postgres://1.2.3.4/postgres",
23
+ Pliny::DbSupport.admin_url("postgres://1.2.3.4/my-db")
24
+ end
25
+ end
26
+
27
+ describe ".setup?" do
28
+ it "checks if the database is responsive" do
29
+ assert_equal true, Pliny::DbSupport.setup?(url)
30
+ assert_equal false, Pliny::DbSupport.setup?("postgres://localhost/does-not-exist")
31
+ end
32
+ end
33
+
18
34
  describe "#migrate" do
19
35
  before do
20
36
  File.open("#{@path}/db/migrate/#{Time.now.to_i}_create_foo.rb", "w") do |f|
@@ -16,14 +16,26 @@ describe "Pliny integration test" do
16
16
  end
17
17
  end
18
18
 
19
- describe "pliny-generate model" do
19
+ describe "pliny-generate scaffold" do
20
20
  before(:all) do
21
- bash_app "pliny-generate model artist"
21
+ bash_app "pliny-generate scaffold artist"
22
22
  end
23
23
 
24
24
  it "creates the model file" do
25
25
  assert File.exists?("./myapp/lib/models/artist.rb")
26
26
  end
27
+
28
+ it "creates the endpoint file" do
29
+ assert File.exists?("./myapp/lib/endpoints/artists.rb")
30
+ end
31
+
32
+ it "creates the serializer file" do
33
+ assert File.exists?("./myapp/lib/serializers/artist.rb")
34
+ end
35
+
36
+ it "creates the schema file" do
37
+ assert File.exists?("./myapp/schema/schemata/artist.yaml")
38
+ end
27
39
  end
28
40
 
29
41
  def bash(cmd)
@@ -38,4 +50,4 @@ describe "Pliny integration test" do
38
50
  def bash_app(cmd)
39
51
  bash "cd myapp && #{cmd}"
40
52
  end
41
- end
53
+ end
@@ -24,7 +24,6 @@ describe Pliny::Middleware::RescueErrors do
24
24
  error_json = MultiJson.decode(last_response.body)
25
25
  assert_equal "service_unavailable", error_json["id"]
26
26
  assert_equal "Service unavailable.", error_json["message"]
27
- assert_equal 503, error_json["status"]
28
27
  end
29
28
 
30
29
  it "intercepts exceptions and renders" do
@@ -34,7 +33,6 @@ describe Pliny::Middleware::RescueErrors do
34
33
  error_json = MultiJson.decode(last_response.body)
35
34
  assert_equal "internal_server_error", error_json["id"]
36
35
  assert_equal "Internal server error.", error_json["message"]
37
- assert_equal 500, error_json["status"]
38
36
  end
39
37
 
40
38
  it "raises given the raise option" do
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.10.0
4
+ version: 0.11.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-07-06 00:00:00.000000000 Z
12
+ date: 2015-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -474,7 +474,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
474
474
  version: '0'
475
475
  requirements: []
476
476
  rubyforge_project:
477
- rubygems_version: 2.4.5
477
+ rubygems_version: 2.4.3
478
478
  signing_key:
479
479
  specification_version: 4
480
480
  summary: Basic tooling to support API apps in Sinatra