hobby-json 0.0.1 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/LICENSE +12 -0
- data/hobby-json.gemspec +5 -3
- data/lib/hobby/json.rb +8 -1
- data/readme.adoc +84 -0
- data/spec/helper.rb +3 -0
- data/spec/http/access_key.yml +14 -0
- data/spec/http/bad_request.yml +7 -0
- data/spec/http/bad_request_when_json_key_accessed.yml +7 -0
- data/spec/http/bad_request_when_wrong_content_type.yml +10 -0
- data/spec/http/echo.yml +17 -0
- data/spec/http_spec.rb +12 -0
- metadata +32 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 04bef8239f05fe122b05e258dbc95febb39e9ce6b2e73df1e721d035af989f95
|
4
|
+
data.tar.gz: 4defa4ae325347cdd8e98a7ee200276df901034b600bcec40bf577646fe6f5f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e05c326717b42f847c14146b8de6be799d998b657a9a346d354fa327bee843437638f2365cc8cc91788b71dea85de81ae599992398861c0ee1bd10795d4daabb
|
7
|
+
data.tar.gz: fee3dfd8b473f628e178be95e903e40f91718a662b3242e258370cf649dc9e133bf1794095fa1905e7cf3df34574ec790c5538a0bf7531cfc0b9ccd67c9695ac
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Copyright (C) 2017 by Anatoly Chernow <chertoly@gmail.com>
|
2
|
+
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
purpose with or without fee is hereby granted.
|
5
|
+
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
7
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
8
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
9
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
10
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
11
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
12
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
data/hobby-json.gemspec
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
Gem::Specification.new do |g|
|
2
2
|
g.name = 'hobby-json'
|
3
3
|
g.files = `git ls-files`.split($/)
|
4
|
-
g.version = '0.0.
|
5
|
-
g.summary = 'A
|
6
|
-
g.authors = ['Anatoly
|
4
|
+
g.version = '0.0.6'
|
5
|
+
g.summary = 'A Hobby extension for JSON requests and responses.'
|
6
|
+
g.authors = ['Anatoly Chernov']
|
7
|
+
|
8
|
+
g.add_dependency 'hobby', '~> 0.2.2'
|
7
9
|
end
|
data/lib/hobby/json.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'rack'
|
2
3
|
|
3
4
|
module Hobby
|
4
5
|
module JSON
|
@@ -14,7 +15,13 @@ module Hobby
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def json
|
17
|
-
@json ||=
|
18
|
+
@json ||= begin
|
19
|
+
fail unless env.fetch('CONTENT_TYPE').start_with? 'application/json'
|
20
|
+
::JSON.parse request.body.read
|
21
|
+
rescue
|
22
|
+
response.status = 400
|
23
|
+
halt
|
24
|
+
end
|
18
25
|
end
|
19
26
|
end
|
20
27
|
end
|
data/readme.adoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= Hobby-JSON
|
2
|
+
|
3
|
+
This repository provides a https://github.com/ch1c0t/hobby[Hobby] extension
|
4
|
+
for JSON requests and responses.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
[source,bash]
|
9
|
+
----
|
10
|
+
gem install hobby-json
|
11
|
+
----
|
12
|
+
|
13
|
+
== Introduction
|
14
|
+
|
15
|
+
It tries to parse `request.body` as JSON.
|
16
|
+
If succeeded, it will store a parsed Ruby object in `json`.
|
17
|
+
If `request.body` happened not to have a valid JSON,
|
18
|
+
it will halt the request with https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400[400 status code].
|
19
|
+
|
20
|
+
A response will be converted to JSON by calling `.to_json` on the latest action object.
|
21
|
+
|
22
|
+
Here is an echo service(it returns the same JSON posted by a request):
|
23
|
+
|
24
|
+
[source,ruby]
|
25
|
+
----
|
26
|
+
require 'hobby'
|
27
|
+
require 'hobby/json'
|
28
|
+
|
29
|
+
class Echo
|
30
|
+
include Hobby
|
31
|
+
include JSON
|
32
|
+
|
33
|
+
post { json }
|
34
|
+
end
|
35
|
+
----
|
36
|
+
|
37
|
+
Another example could be an adding service.
|
38
|
+
It expects to get an array of numbers in the `numbers` field, after which it sums them up,
|
39
|
+
and returns the result in the `result` field.
|
40
|
+
|
41
|
+
[source,ruby]
|
42
|
+
----
|
43
|
+
require 'hobby'
|
44
|
+
require 'hobby/json'
|
45
|
+
|
46
|
+
class Adder
|
47
|
+
include Hobby
|
48
|
+
include JSON
|
49
|
+
|
50
|
+
post do
|
51
|
+
{ result: json['numbers'].sum }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
----
|
55
|
+
|
56
|
+
You can put this in `config.ru`:
|
57
|
+
|
58
|
+
[source,ruby]
|
59
|
+
----
|
60
|
+
run Adder.new
|
61
|
+
----
|
62
|
+
|
63
|
+
and run it with `rackup`. Then, the service can be accessed like so:
|
64
|
+
|
65
|
+
[source,bash]
|
66
|
+
----
|
67
|
+
➜ ~ curl -H "Content-Type: application/json" -X POST -d '{"numbers":[1,2,3]}' http://localhost:9292
|
68
|
+
{"result":6}%
|
69
|
+
----
|
70
|
+
|
71
|
+
== Development
|
72
|
+
|
73
|
+
To run the specs:
|
74
|
+
|
75
|
+
[source,bash]
|
76
|
+
----
|
77
|
+
bundle exec rspec
|
78
|
+
----
|
79
|
+
|
80
|
+
To perform mutation analysis:
|
81
|
+
[source,bash]
|
82
|
+
----
|
83
|
+
bundle exec mutant --use rspec 'Hobby::JSON*' --include lib --require hobby/json
|
84
|
+
----
|
data/spec/helper.rb
ADDED
data/spec/http/echo.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
- get:
|
2
|
+
path: /
|
3
|
+
format: json
|
4
|
+
body:
|
5
|
+
string: string
|
6
|
+
number: 42
|
7
|
+
headers:
|
8
|
+
Content-Type: application/json
|
9
|
+
|
10
|
+
response:
|
11
|
+
status: 200
|
12
|
+
format: json
|
13
|
+
body:
|
14
|
+
string: string
|
15
|
+
number: 42
|
16
|
+
headers.>:
|
17
|
+
Content-Type: application/json
|
data/spec/http_spec.rb
ADDED
metadata
CHANGED
@@ -1,23 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hobby-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Anatoly
|
7
|
+
- Anatoly Chernov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2021-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hobby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.2
|
13
27
|
description:
|
14
28
|
email:
|
15
29
|
executables: []
|
16
30
|
extensions: []
|
17
31
|
extra_rdoc_files: []
|
18
32
|
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- ".travis.yml"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
19
37
|
- hobby-json.gemspec
|
20
38
|
- lib/hobby/json.rb
|
39
|
+
- readme.adoc
|
40
|
+
- spec/helper.rb
|
41
|
+
- spec/http/access_key.yml
|
42
|
+
- spec/http/bad_request.yml
|
43
|
+
- spec/http/bad_request_when_json_key_accessed.yml
|
44
|
+
- spec/http/bad_request_when_wrong_content_type.yml
|
45
|
+
- spec/http/echo.yml
|
46
|
+
- spec/http_spec.rb
|
21
47
|
homepage:
|
22
48
|
licenses: []
|
23
49
|
metadata: {}
|
@@ -36,9 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
62
|
- !ruby/object:Gem::Version
|
37
63
|
version: '0'
|
38
64
|
requirements: []
|
39
|
-
|
40
|
-
rubygems_version: 2.5.2
|
65
|
+
rubygems_version: 3.1.4
|
41
66
|
signing_key:
|
42
67
|
specification_version: 4
|
43
|
-
summary: A
|
68
|
+
summary: A Hobby extension for JSON requests and responses.
|
44
69
|
test_files: []
|