hobby-json 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +8 -0
- data/Gemfile +1 -1
- data/LICENSE +12 -0
- data/hobby-json.gemspec +2 -2
- data/lib/hobby/json.rb +1 -0
- data/readme.adoc +84 -0
- data/spec/http/bad_request.yml +2 -1
- data/spec/http/bad_request_when_json_key_accessed.yml +2 -1
- data/spec/http/bad_request_when_wrong_content_type.yml +10 -0
- data/spec/http/echo.yml +4 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6681daa3660fe33bc169a0d8ee9eae5ff2b3724d3b8a4f7a9d66f61864eff0c9
|
4
|
+
data.tar.gz: 1d1c83b0529740a02b896bba002ab4c2f47b5c80ec3fe7353ea53dabe2f68e01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac541875f2e93d174b2bc2f6be9068522028c130f10ab401e6d8c1a9ec93a5116a1255385218364481aba968783f83784f266f814eae954d6127c9518ae0d7f8
|
7
|
+
data.tar.gz: 02cee90a78e8fa2c19fa1811d7578c7dd1d30721681c10186d15aa61c27e0749c212bcfa6d26a17a9ec6af1d1003d0c85f2f66e508e38d62f04e607a28164814
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
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,8 +1,8 @@
|
|
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
|
4
|
+
g.version = '0.0.5'
|
5
|
+
g.summary = 'A Hobby extension for JSON requests and responses.'
|
6
6
|
g.authors = ['Anatoly Chernow']
|
7
7
|
|
8
8
|
g.add_dependency 'rack'
|
data/lib/hobby/json.rb
CHANGED
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/http/bad_request.yml
CHANGED
data/spec/http/echo.yml
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anatoly Chernow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -31,12 +31,16 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- ".gitignore"
|
34
|
+
- ".travis.yml"
|
34
35
|
- Gemfile
|
36
|
+
- LICENSE
|
35
37
|
- hobby-json.gemspec
|
36
38
|
- lib/hobby/json.rb
|
39
|
+
- readme.adoc
|
37
40
|
- spec/helper.rb
|
38
41
|
- spec/http/bad_request.yml
|
39
42
|
- spec/http/bad_request_when_json_key_accessed.yml
|
43
|
+
- spec/http/bad_request_when_wrong_content_type.yml
|
40
44
|
- spec/http/echo.yml
|
41
45
|
- spec/http_spec.rb
|
42
46
|
homepage:
|
@@ -57,9 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
61
|
- !ruby/object:Gem::Version
|
58
62
|
version: '0'
|
59
63
|
requirements: []
|
60
|
-
|
61
|
-
rubygems_version: 2.5.2
|
64
|
+
rubygems_version: 3.0.3
|
62
65
|
signing_key:
|
63
66
|
specification_version: 4
|
64
|
-
summary: A
|
67
|
+
summary: A Hobby extension for JSON requests and responses.
|
65
68
|
test_files: []
|