pliny 1.1.0 → 1.2.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/errors.rb +1 -1
- data/lib/pliny/helpers/encode.rb +5 -1
- data/lib/pliny/helpers/params.rb +2 -2
- data/lib/pliny/middleware/versioning.rb +1 -1
- data/lib/pliny/templates/endpoint_acceptance_test.erb +2 -2
- data/lib/pliny/templates/endpoint_scaffold_acceptance_test.erb +2 -2
- data/lib/pliny/version.rb +1 -1
- data/lib/pliny.rb +1 -1
- data/lib/template/Gemfile +0 -2
- data/lib/template/spec/endpoints/health_spec.rb +2 -2
- data/spec/commands/generator/endpoint_spec.rb +5 -5
- data/spec/helpers/encode_spec.rb +3 -3
- data/spec/helpers/serialize_spec.rb +5 -5
- data/spec/middleware/rescue_errors_spec.rb +3 -3
- data/spec/middleware/versioning_spec.rb +7 -7
- metadata +8 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53b08362a2511749050f3ca5ea9690d2adeefec8662f0a74cf391c896b119fd8
|
4
|
+
data.tar.gz: 23da2c0e15bdb3bf93f719e440bf0c2ac2a093fc1de3e500850c47ce5b8d6851
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc1e1a5d4fda88d1a181e06e7aefb300d1cf6ee516ca8e6018173dfd5dcf079885ebfb8c74643db317aa889a82cd38b95ce9101a592b3126fd7039f4aa7b029c
|
7
|
+
data.tar.gz: 1120021ef6ade5c1470cb280504e9abad2f672610be35546d3ea2521164841e973cef2b1be2f2e1b9dd0614ac1c493e6dae818ab35cb8dff242fdd115228524c
|
data/lib/pliny/errors.rb
CHANGED
@@ -6,7 +6,7 @@ module Pliny
|
|
6
6
|
def self.render(error)
|
7
7
|
headers = { "content-type" => "application/json; charset=utf-8" }
|
8
8
|
data = { id: error.id, message: error.message }
|
9
|
-
[error.status, headers, [
|
9
|
+
[error.status, headers, [JSON.generate(data)]]
|
10
10
|
end
|
11
11
|
|
12
12
|
def initialize(message, id)
|
data/lib/pliny/helpers/encode.rb
CHANGED
@@ -2,7 +2,11 @@ module Pliny::Helpers
|
|
2
2
|
module Encode
|
3
3
|
def encode(object)
|
4
4
|
content_type :json, charset: 'utf-8'
|
5
|
-
|
5
|
+
if params[:pretty] == 'true' || Config.pretty_json
|
6
|
+
JSON.pretty_generate(object)
|
7
|
+
else
|
8
|
+
JSON.generate(object)
|
9
|
+
end
|
6
10
|
end
|
7
11
|
end
|
8
12
|
end
|
data/lib/pliny/helpers/params.rb
CHANGED
@@ -9,8 +9,8 @@ module Pliny::Helpers
|
|
9
9
|
def parse_body_params
|
10
10
|
if request.media_type == "application/json"
|
11
11
|
begin
|
12
|
-
decoded =
|
13
|
-
rescue
|
12
|
+
decoded = JSON.parse(request.body.read)
|
13
|
+
rescue JSON::ParserError => e
|
14
14
|
raise Pliny::Errors::BadRequest, e.message
|
15
15
|
end
|
16
16
|
request.body.rewind
|
@@ -27,7 +27,7 @@ module Pliny::Middleware
|
|
27
27
|
Please specify a version along with the MIME type. For example, `Accept: application/vnd.#{@app_name}+json; version=1`.
|
28
28
|
eos
|
29
29
|
return [400, { "content-type" => "application/json; charset=utf-8" },
|
30
|
-
[
|
30
|
+
[JSON.generate(error)]]
|
31
31
|
end
|
32
32
|
|
33
33
|
unless version
|
@@ -23,7 +23,7 @@ RSpec.describe Endpoints::<%= plural_class_name %> do
|
|
23
23
|
describe 'POST <%= url_path %>' do
|
24
24
|
it 'returns correct status code and conforms to schema' do
|
25
25
|
header "Content-Type", "application/json"
|
26
|
-
post '<%= url_path %>',
|
26
|
+
post '<%= url_path %>', JSON.generate({})
|
27
27
|
assert_equal 201, last_response.status
|
28
28
|
assert_schema_conform
|
29
29
|
end
|
@@ -40,7 +40,7 @@ RSpec.describe Endpoints::<%= plural_class_name %> do
|
|
40
40
|
describe 'PATCH <%= url_path %>/:id' do
|
41
41
|
it 'returns correct status code and conforms to schema' do
|
42
42
|
header "Content-Type", "application/json"
|
43
|
-
patch '<%= url_path %>/123',
|
43
|
+
patch '<%= url_path %>/123', JSON.generate({})
|
44
44
|
assert_equal 200, last_response.status
|
45
45
|
assert_schema_conform
|
46
46
|
end
|
@@ -32,7 +32,7 @@ RSpec.describe Endpoints::<%= plural_class_name %> do
|
|
32
32
|
describe 'POST <%= url_path %>' do
|
33
33
|
it 'returns correct status code and conforms to schema' do
|
34
34
|
header "Content-Type", "application/json"
|
35
|
-
post '<%= url_path %>',
|
35
|
+
post '<%= url_path %>', JSON.generate({})
|
36
36
|
assert_equal 201, last_response.status
|
37
37
|
assert_schema_conform
|
38
38
|
end
|
@@ -50,7 +50,7 @@ RSpec.describe Endpoints::<%= plural_class_name %> do
|
|
50
50
|
describe 'PATCH <%= url_path %>/:id' do
|
51
51
|
it 'returns correct status code and conforms to schema' do
|
52
52
|
header "Content-Type", "application/json"
|
53
|
-
patch "<%= url_path %>/#{@<%= field_name %>.id}",
|
53
|
+
patch "<%= url_path %>/#{@<%= field_name %>.id}", JSON.generate({})
|
54
54
|
assert_equal 200, last_response.status
|
55
55
|
assert_schema_conform
|
56
56
|
end
|
data/lib/pliny/version.rb
CHANGED
data/lib/pliny.rb
CHANGED
data/lib/template/Gemfile
CHANGED
@@ -13,7 +13,7 @@ RSpec.describe Endpoints::Health do
|
|
13
13
|
assert_equal(200, last_response.status)
|
14
14
|
assert_equal("application/json;charset=utf-8", last_response.headers["Content-Type"])
|
15
15
|
assert_equal(2, last_response.headers["Content-Length"].to_i)
|
16
|
-
assert_equal({},
|
16
|
+
assert_equal({}, JSON.parse(last_response.body))
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -49,7 +49,7 @@ RSpec.describe Endpoints::Health do
|
|
49
49
|
assert_equal(200, last_response.status)
|
50
50
|
assert_equal("application/json;charset=utf-8", last_response.headers["Content-Type"])
|
51
51
|
assert_equal(2, last_response.headers["Content-Length"].to_i)
|
52
|
-
assert_equal({},
|
52
|
+
assert_equal({}, JSON.parse(last_response.body))
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
@@ -26,31 +26,31 @@ describe Pliny::Commands::Generator::Endpoint do
|
|
26
26
|
it "defines a stub GET /" do
|
27
27
|
get "/artists"
|
28
28
|
assert_equal 200, last_response.status
|
29
|
-
assert_equal [],
|
29
|
+
assert_equal [], JSON.parse(last_response.body)
|
30
30
|
end
|
31
31
|
|
32
32
|
it "defines a stub POST /" do
|
33
33
|
post "/artists"
|
34
34
|
assert_equal 201, last_response.status
|
35
|
-
assert_equal Hash.new,
|
35
|
+
assert_equal Hash.new, JSON.parse(last_response.body)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "defines a stub GET /:id" do
|
39
39
|
get "/artists/123"
|
40
40
|
assert_equal 200, last_response.status
|
41
|
-
assert_equal Hash.new,
|
41
|
+
assert_equal Hash.new, JSON.parse(last_response.body)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "defines a stub PATCH /:id" do
|
45
45
|
patch "/artists/123"
|
46
46
|
assert_equal 200, last_response.status
|
47
|
-
assert_equal Hash.new,
|
47
|
+
assert_equal Hash.new, JSON.parse(last_response.body)
|
48
48
|
end
|
49
49
|
|
50
50
|
it "defines a stub DELETE /:id" do
|
51
51
|
delete "/artists/123"
|
52
52
|
assert_equal 200, last_response.status
|
53
|
-
assert_equal Hash.new,
|
53
|
+
assert_equal Hash.new, JSON.parse(last_response.body)
|
54
54
|
end
|
55
55
|
|
56
56
|
end
|
data/spec/helpers/encode_spec.rb
CHANGED
@@ -23,19 +23,19 @@ describe Pliny::Helpers::Encode do
|
|
23
23
|
it "encodes as json" do
|
24
24
|
payload = { "foo" => "bar" }
|
25
25
|
post "/", payload
|
26
|
-
assert_equal
|
26
|
+
assert_equal JSON.generate(payload), last_response.body
|
27
27
|
end
|
28
28
|
|
29
29
|
it "encodes in pretty mode when pretty=true" do
|
30
30
|
payload = { "foo" => "bar", "pretty" => "true" }
|
31
31
|
post "/", payload
|
32
|
-
assert_equal
|
32
|
+
assert_equal JSON.pretty_generate(payload), last_response.body
|
33
33
|
end
|
34
34
|
|
35
35
|
it "encodes in pretty mode when set by config" do
|
36
36
|
allow(Config).to receive(:pretty_json) { true }
|
37
37
|
payload = { "foo" => "bar" }
|
38
38
|
post "/", payload
|
39
|
-
assert_equal
|
39
|
+
assert_equal JSON.pretty_generate(payload), last_response.body
|
40
40
|
end
|
41
41
|
end
|
@@ -7,7 +7,7 @@ describe Pliny::Helpers::Serialize do
|
|
7
7
|
register Pliny::Helpers::Serialize
|
8
8
|
|
9
9
|
get "/" do
|
10
|
-
|
10
|
+
JSON.generate(serialize([]))
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -34,11 +34,11 @@ describe Pliny::Helpers::Serialize do
|
|
34
34
|
serializer Serializer
|
35
35
|
|
36
36
|
get "/" do
|
37
|
-
|
37
|
+
JSON.generate(serialize([]))
|
38
38
|
end
|
39
39
|
|
40
40
|
get "/env" do
|
41
|
-
|
41
|
+
JSON.generate(serialize(env))
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
@@ -46,13 +46,13 @@ describe Pliny::Helpers::Serialize do
|
|
46
46
|
it "encodes as json" do
|
47
47
|
get "/"
|
48
48
|
assert_equal 200, last_response.status
|
49
|
-
assert_equal
|
49
|
+
assert_equal JSON.generate([]), last_response.body
|
50
50
|
end
|
51
51
|
|
52
52
|
it "emits information for canonical log lines" do
|
53
53
|
get "/env"
|
54
54
|
assert_equal 200, last_response.status
|
55
|
-
body =
|
55
|
+
body = JSON.parse(last_response.body)
|
56
56
|
assert body["pliny.serializer_arity"] > 1
|
57
57
|
assert body["pliny.serializer_timing"] > 0
|
58
58
|
end
|
@@ -21,7 +21,7 @@ describe Pliny::Middleware::RescueErrors do
|
|
21
21
|
@app = new_rack_app
|
22
22
|
get "/api-error"
|
23
23
|
assert_equal 503, last_response.status
|
24
|
-
error_json =
|
24
|
+
error_json = JSON.parse(last_response.body)
|
25
25
|
assert_equal "service_unavailable", error_json["id"]
|
26
26
|
assert_equal "Service unavailable.", error_json["message"]
|
27
27
|
end
|
@@ -31,7 +31,7 @@ describe Pliny::Middleware::RescueErrors do
|
|
31
31
|
expect(Pliny::ErrorReporters).to receive(:notify)
|
32
32
|
get "/"
|
33
33
|
assert_equal 500, last_response.status
|
34
|
-
error_json =
|
34
|
+
error_json = JSON.parse(last_response.body)
|
35
35
|
assert_equal "internal_server_error", error_json["id"]
|
36
36
|
assert_equal "Internal server error.", error_json["message"]
|
37
37
|
end
|
@@ -48,7 +48,7 @@ describe Pliny::Middleware::RescueErrors do
|
|
48
48
|
allow(Pliny::ErrorReporters).to receive(:notify)
|
49
49
|
get "/"
|
50
50
|
assert_equal 500, last_response.status
|
51
|
-
error_json =
|
51
|
+
error_json = JSON.parse(last_response.body)
|
52
52
|
assert_equal "internal_server_error", error_json["id"]
|
53
53
|
assert_equal "Please stand by", error_json["message"]
|
54
54
|
end
|
@@ -13,7 +13,7 @@ describe Pliny::Middleware::Versioning do
|
|
13
13
|
use Pliny::Middleware::Versioning, default: '2', app_name: 'pliny'
|
14
14
|
run Sinatra.new {
|
15
15
|
get "/" do
|
16
|
-
|
16
|
+
JSON.generate env
|
17
17
|
end
|
18
18
|
}
|
19
19
|
end
|
@@ -21,7 +21,7 @@ describe Pliny::Middleware::Versioning do
|
|
21
21
|
|
22
22
|
it "produces default version on application/json" do
|
23
23
|
get '/', {}, {'HTTP_ACCEPT' => 'application/json'}
|
24
|
-
json =
|
24
|
+
json = JSON.parse(last_response.body)
|
25
25
|
assert_equal 'application/json', json['HTTP_ACCEPT']
|
26
26
|
assert_equal '2', json['HTTP_X_API_VERSION']
|
27
27
|
end
|
@@ -33,19 +33,19 @@ Please specify a version along with the MIME type. For example, `Accept: applica
|
|
33
33
|
eos
|
34
34
|
|
35
35
|
assert_equal 400, last_response.status
|
36
|
-
assert_equal
|
36
|
+
assert_equal JSON.generate(error), last_response.body
|
37
37
|
end
|
38
38
|
|
39
39
|
it "ignores a wrong app name" do
|
40
40
|
get '/', {}, {'HTTP_ACCEPT' => 'application/vnd.chuck_norris+json'}
|
41
|
-
json =
|
41
|
+
json = JSON.parse(last_response.body)
|
42
42
|
assert_equal 'application/vnd.chuck_norris+json', json['HTTP_ACCEPT']
|
43
43
|
assert_equal '2', json['HTTP_X_API_VERSION']
|
44
44
|
end
|
45
45
|
|
46
46
|
it "produces a version on application/vnd.pliny+json; version=3" do
|
47
47
|
get '/', {}, {'HTTP_ACCEPT' => 'application/vnd.pliny+json; version=3'}
|
48
|
-
json =
|
48
|
+
json = JSON.parse(last_response.body)
|
49
49
|
assert_equal 'application/json', json['HTTP_ACCEPT']
|
50
50
|
assert_equal '3', json['HTTP_X_API_VERSION']
|
51
51
|
end
|
@@ -53,14 +53,14 @@ Please specify a version along with the MIME type. For example, `Accept: applica
|
|
53
53
|
# this behavior is pretty sketchy, but a pretty extreme edge case
|
54
54
|
it "handles multiple MIME types" do
|
55
55
|
get '/', {}, {'HTTP_ACCEPT' => 'application/vnd.pliny+json; version=3; q=0.5, text/xml'}
|
56
|
-
json =
|
56
|
+
json = JSON.parse(last_response.body)
|
57
57
|
assert_equal 'text/xml, application/json; q=0.5', json['HTTP_ACCEPT']
|
58
58
|
assert_equal '3', json['HTTP_X_API_VERSION']
|
59
59
|
end
|
60
60
|
|
61
61
|
it "produces the priority version on multiple types" do
|
62
62
|
get '/', {}, {'HTTP_ACCEPT' => 'application/vnd.pliny+json; version=4; q=0.5, application/vnd.pliny+json; version=3'}
|
63
|
-
json =
|
63
|
+
json = JSON.parse(last_response.body)
|
64
64
|
assert_equal 'application/json, application/json; q=0.5', json['HTTP_ACCEPT']
|
65
65
|
assert_equal '3', json['HTTP_X_API_VERSION']
|
66
66
|
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: 1.
|
4
|
+
version: 1.2.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:
|
12
|
+
date: 2025-02-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -17,40 +17,20 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '7.0'
|
21
21
|
- - "<"
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: '
|
23
|
+
version: '9.0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
28
|
- - ">="
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: '
|
30
|
+
version: '7.0'
|
31
31
|
- - "<"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: multi_json
|
36
|
-
requirement: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.9'
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 1.9.3
|
44
|
-
type: :runtime
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: !ruby/object:Gem::Requirement
|
47
|
-
requirements:
|
48
|
-
- - "~>"
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version: '1.9'
|
51
|
-
- - ">="
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 1.9.3
|
33
|
+
version: '9.0'
|
54
34
|
- !ruby/object:Gem::Dependency
|
55
35
|
name: prmd
|
56
36
|
requirement: !ruby/object:Gem::Requirement
|
@@ -505,14 +485,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
505
485
|
requirements:
|
506
486
|
- - ">="
|
507
487
|
- !ruby/object:Gem::Version
|
508
|
-
version: '
|
488
|
+
version: '3.2'
|
509
489
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
510
490
|
requirements:
|
511
491
|
- - ">="
|
512
492
|
- !ruby/object:Gem::Version
|
513
493
|
version: '0'
|
514
494
|
requirements: []
|
515
|
-
rubygems_version: 3.5.
|
495
|
+
rubygems_version: 3.5.22
|
516
496
|
signing_key:
|
517
497
|
specification_version: 4
|
518
498
|
summary: Basic tooling to support API apps in Sinatra
|