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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48a24aea604685a3fb76c52cf1f6019cdadb389a
4
- data.tar.gz: b67a25fcf927be13c8c223f8c6ba090a93362b43
3
+ metadata.gz: 496e99cb519fa1bf2b5ecbafae836cf88fa0e26b
4
+ data.tar.gz: 7571df6685a3ec6a9a13c8fb1bfdb4e23c1a5b08
5
5
  SHA512:
6
- metadata.gz: c743117d694c26fc792db548da9230e0b769655cbd3d4a9da672dffb6211fc327ac73e230320ac8946be1e0334c219a675fb8c81d6be25d3ee7d0013705c96fd
7
- data.tar.gz: a92aea7a99f0bc6173f591609601d4ce6c4143e3c51a2d95a7ac4e30bbd295366756c0b81b4b05efba4ea4a81cf4f1e08d7c96f574aed83ca7135117c146d825
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
- setup_environments
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 setup_environments
33
- db = URI.parse("postgres:///#{name}")
34
- {
35
- '.env.sample' => 'development',
36
- '.env.test' => 'test'
37
- }.each do |env_file, db_env_suffix|
38
- env_path = "#{app_dir}/#{env_file}"
39
- db.path = "/#{name}-#{db_env_suffix}"
40
- env = File.read(env_path)
41
- File.open(env_path, 'w') do |f|
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
@@ -1,3 +1,3 @@
1
1
  module Pliny
2
- VERSION = "0.14.2"
2
+ VERSION = "0.15.0"
3
3
  end
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"
@@ -1,5 +1,5 @@
1
- APP_NAME=pliny
2
- DATABASE_URL=postgres://localhost/pliny-development
1
+ APP_NAME=<%= app_name %>
2
+ DATABASE_URL=postgres:///<%= app_name %>-development
3
3
  DEPLOYMENT=development
4
4
  PLINY_ENV=development
5
5
  TZ=UTC
@@ -1,5 +1,5 @@
1
- APP_NAME=pliny
2
- DATABASE_URL=postgres://localhost/pliny-test
1
+ APP_NAME=<%= app_name %>
2
+ DATABASE_URL=postgres:///<%= app_name %>-test
3
3
  DEPLOYMENT=test
4
4
  PLINY_ENV=test
5
5
  TZ=UTC
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.14"
7
+ gem "pliny", "~> 0.15"
8
8
  gem "pry"
9
9
  gem "puma", "~> 2.10"
10
10
  gem "rack-ssl"
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "<%= app_name %>",
3
+ "description": "A new instance of <%= app_name %>",
4
+
5
+ "env": {
6
+ "APP_NAME": "<%= app_name %>",
7
+ "DEPLOYMENT": "production",
8
+ "PLINY_ENV": "production"
9
+ },
10
+
11
+ "addons": [
12
+ "heroku-postgresql"
13
+ ]
14
+ }
@@ -5,6 +5,7 @@ module Endpoints
5
5
 
6
6
  helpers Pliny::Helpers::Encode
7
7
  helpers Pliny::Helpers::Params
8
+ helpers Pliny::Helpers::Serialize
8
9
 
9
10
  set :dump_errors, false
10
11
  set :raise_errors, true
@@ -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.14.2
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-01-27 00:00:00.000000000 Z
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.4.5.1
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