pliny 0.14.2 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pliny/commands/creator.rb +14 -16
- data/lib/pliny/helpers/serialize.rb +35 -0
- data/lib/pliny/templates/endpoint_scaffold.erb +3 -6
- data/lib/pliny/version.rb +1 -1
- data/lib/pliny.rb +1 -0
- data/lib/template/{.env.sample → .env.sample.erb} +2 -2
- data/lib/template/{.env.test → .env.test.erb} +2 -2
- data/lib/template/Gemfile +1 -1
- data/lib/template/app.json.erb +14 -0
- data/lib/template/lib/endpoints/base.rb +1 -0
- data/lib/template/lib/initializer.rb +2 -2
- data/spec/commands/creator_spec.rb +13 -0
- data/spec/helpers/serialize_spec.rb +48 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 496e99cb519fa1bf2b5ecbafae836cf88fa0e26b
|
4
|
+
data.tar.gz: 7571df6685a3ec6a9a13c8fb1bfdb4e23c1a5b08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7d1b9eb9510d0f0b37dcd1353a9309447cababbd30ed913b4d291ac1b6f5170f9ec2ccaa51b087368ea6f7e157b94bda63e45af5d4461018724f0aaa89ab1d6
|
7
|
+
data.tar.gz: 7a41e25b27d471f24454dba7c60b58c853611fe6da9f49ce1ac7f83a42b0c0c89a70dd719e12e18a4c1ba6b7143db369ef5126623da08b2e79b9efe677d222ff
|
@@ -2,6 +2,8 @@ require 'fileutils'
|
|
2
2
|
require 'pathname'
|
3
3
|
require 'pliny/version'
|
4
4
|
require 'uri'
|
5
|
+
require 'erb'
|
6
|
+
require 'ostruct'
|
5
7
|
|
6
8
|
module Pliny::Commands
|
7
9
|
class Creator
|
@@ -22,29 +24,25 @@ module Pliny::Commands
|
|
22
24
|
|
23
25
|
FileUtils.copy_entry template_dir, app_dir
|
24
26
|
FileUtils.rm_rf("#{app_dir}/.git")
|
25
|
-
|
27
|
+
parse_erb_files
|
26
28
|
display 'Pliny app created. To start, run:'
|
27
29
|
display "cd #{app_dir} && bin/setup"
|
28
30
|
end
|
29
31
|
|
30
32
|
protected
|
31
33
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
# ruby's URI#to_s renders foo:/bar when there's no host
|
43
|
-
# we want foo:///bar instead!
|
44
|
-
db_url = db.to_s.sub(':/', ':///')
|
45
|
-
f.puts env.sub(/APP_NAME=.*/, "APP_NAME=#{name}")
|
46
|
-
.sub(/DATABASE_URL=.*/, "DATABASE_URL=#{db_url}")
|
34
|
+
def parse_erb_files
|
35
|
+
Dir.glob("#{app_dir}/{*,.*}.erb").each do |file|
|
36
|
+
static_file = file.gsub(/\.erb$/, '')
|
37
|
+
|
38
|
+
template = ERB.new(File.read(file), 0)
|
39
|
+
context = OpenStruct.new(app_name: name)
|
40
|
+
content = template.result(context.instance_eval { binding })
|
41
|
+
|
42
|
+
File.open(static_file, "w") do |f|
|
43
|
+
f.write content
|
47
44
|
end
|
45
|
+
FileUtils.rm(file)
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Pliny::Helpers
|
2
|
+
module Serialize
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
def serialize(data, structure = :default)
|
8
|
+
if self.class.serializer_class.nil?
|
9
|
+
raise <<-eos.strip
|
10
|
+
No serializer has been specified for this endpoint. Please specify one with
|
11
|
+
`serializer Serializers::ModelName` in the endpoint.
|
12
|
+
eos
|
13
|
+
end
|
14
|
+
|
15
|
+
self.class.serializer_class.new(structure).serialize(data)
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
# Provide a way to specify endpoint serializer class.
|
20
|
+
#
|
21
|
+
# class Endpoints::User < Base
|
22
|
+
# serializer Serializers::User
|
23
|
+
#
|
24
|
+
# get do
|
25
|
+
# encode serialize(User.all)
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
def serializer(serializer_class)
|
29
|
+
@serializer_class = serializer_class
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :serializer_class
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,6 +1,9 @@
|
|
1
1
|
module Endpoints
|
2
2
|
class <%= plural_class_name %> < Base
|
3
3
|
namespace "<%= url_path %>" do
|
4
|
+
serializer Serializers::<%= singular_class_name %>
|
5
|
+
|
6
|
+
|
4
7
|
get do
|
5
8
|
encode serialize(<%= singular_class_name %>.all)
|
6
9
|
end
|
@@ -30,12 +33,6 @@ module Endpoints
|
|
30
33
|
<%= field_name %>.destroy
|
31
34
|
encode serialize(<%= field_name %>)
|
32
35
|
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def serialize(data, structure = :default)
|
37
|
-
Serializers::<%= singular_class_name %>.new(structure).serialize(data)
|
38
|
-
end
|
39
36
|
end
|
40
37
|
end
|
41
38
|
end
|
data/lib/pliny/version.rb
CHANGED
data/lib/pliny.rb
CHANGED
@@ -5,6 +5,7 @@ require_relative "pliny/version"
|
|
5
5
|
require_relative "pliny/errors"
|
6
6
|
require_relative "pliny/helpers/encode"
|
7
7
|
require_relative "pliny/helpers/params"
|
8
|
+
require_relative "pliny/helpers/serialize"
|
8
9
|
require_relative "pliny/log"
|
9
10
|
require_relative "pliny/request_store"
|
10
11
|
require_relative "pliny/router"
|
data/lib/template/Gemfile
CHANGED
@@ -12,13 +12,13 @@ module Initializer
|
|
12
12
|
|
13
13
|
def self.require_lib
|
14
14
|
require! %w(
|
15
|
+
lib/serializers/base
|
16
|
+
lib/serializers/**/*
|
15
17
|
lib/endpoints/base
|
16
18
|
lib/endpoints/**/*
|
17
19
|
lib/mediators/base
|
18
20
|
lib/mediators/**/*
|
19
21
|
lib/routes
|
20
|
-
lib/serializers/base
|
21
|
-
lib/serializers/**/*
|
22
22
|
)
|
23
23
|
end
|
24
24
|
|
@@ -24,6 +24,11 @@ describe Pliny::Commands::Creator do
|
|
24
24
|
refute File.exists?("./foobar/.git")
|
25
25
|
end
|
26
26
|
|
27
|
+
it "deletes the .erb files from it" do
|
28
|
+
@gen.run!
|
29
|
+
assert_equal 0, Dir.glob("./foobar/**/{*,.*}.erb").length
|
30
|
+
end
|
31
|
+
|
27
32
|
it "changes DATABASE_URL in .env.sample to use the app name" do
|
28
33
|
@gen.run!
|
29
34
|
db_url = File.read("./foobar/.env.sample").split("\n").detect do |line|
|
@@ -39,5 +44,13 @@ describe Pliny::Commands::Creator do
|
|
39
44
|
end
|
40
45
|
assert_equal "DATABASE_URL=postgres:///foobar-test", db_url
|
41
46
|
end
|
47
|
+
|
48
|
+
it "changes APP_NAME in app.json to use the app name" do
|
49
|
+
@gen.run!
|
50
|
+
db_url = File.read("./foobar/app.json").split("\n").detect do |line|
|
51
|
+
line.include?("\"name\":")
|
52
|
+
end
|
53
|
+
assert_equal " \"name\": \"foobar\",", db_url
|
54
|
+
end
|
42
55
|
end
|
43
56
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Pliny::Helpers::Serialize do
|
4
|
+
context "without a serializer" do
|
5
|
+
def app
|
6
|
+
Sinatra.new do
|
7
|
+
helpers Pliny::Helpers::Serialize
|
8
|
+
|
9
|
+
get "/" do
|
10
|
+
MultiJson.encode(serialize([]))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "sets the Content-Type" do
|
16
|
+
assert_raises(RuntimeError) do
|
17
|
+
get "/"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "with a serializer" do
|
23
|
+
class Serializer
|
24
|
+
def initialize(opts); end
|
25
|
+
def serialize(data)
|
26
|
+
data
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def app
|
31
|
+
Sinatra.new do
|
32
|
+
helpers Pliny::Helpers::Serialize
|
33
|
+
|
34
|
+
serializer Serializer
|
35
|
+
|
36
|
+
get "/" do
|
37
|
+
MultiJson.encode(serialize([]))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "encodes as json" do
|
43
|
+
get "/"
|
44
|
+
assert_equal 200, last_response.status
|
45
|
+
assert_equal MultiJson.encode([]), last_response.body
|
46
|
+
end
|
47
|
+
end
|
48
|
+
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.
|
4
|
+
version: 0.15.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: 2016-
|
12
|
+
date: 2016-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -351,6 +351,7 @@ files:
|
|
351
351
|
- lib/pliny/errors.rb
|
352
352
|
- lib/pliny/helpers/encode.rb
|
353
353
|
- lib/pliny/helpers/params.rb
|
354
|
+
- lib/pliny/helpers/serialize.rb
|
354
355
|
- lib/pliny/log.rb
|
355
356
|
- lib/pliny/middleware/cors.rb
|
356
357
|
- lib/pliny/middleware/instruments.rb
|
@@ -378,8 +379,8 @@ files:
|
|
378
379
|
- lib/pliny/templates/serializer_test.erb
|
379
380
|
- lib/pliny/utils.rb
|
380
381
|
- lib/pliny/version.rb
|
381
|
-
- lib/template/.env.sample
|
382
|
-
- lib/template/.env.test
|
382
|
+
- lib/template/.env.sample.erb
|
383
|
+
- lib/template/.env.test.erb
|
383
384
|
- lib/template/.gitignore
|
384
385
|
- lib/template/.rspec
|
385
386
|
- lib/template/.ruby-version
|
@@ -388,6 +389,7 @@ files:
|
|
388
389
|
- lib/template/Procfile
|
389
390
|
- lib/template/README.md
|
390
391
|
- lib/template/Rakefile
|
392
|
+
- lib/template/app.json.erb
|
391
393
|
- lib/template/bin/console
|
392
394
|
- lib/template/bin/run
|
393
395
|
- lib/template/bin/setup
|
@@ -432,6 +434,7 @@ files:
|
|
432
434
|
- spec/db_support_spec.rb
|
433
435
|
- spec/errors_spec.rb
|
434
436
|
- spec/helpers/encode_spec.rb
|
437
|
+
- spec/helpers/serialize_spec.rb
|
435
438
|
- spec/integration_spec.rb
|
436
439
|
- spec/log_spec.rb
|
437
440
|
- spec/middleware/cors_spec.rb
|
@@ -465,7 +468,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
465
468
|
version: '0'
|
466
469
|
requirements: []
|
467
470
|
rubyforge_project:
|
468
|
-
rubygems_version: 2.
|
471
|
+
rubygems_version: 2.5.1
|
469
472
|
signing_key:
|
470
473
|
specification_version: 4
|
471
474
|
summary: Basic tooling to support API apps in Sinatra
|