hobby-json 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d7c3bf7ba50a139dc0c243eb7c4363d024a8a614
4
- data.tar.gz: 1f71511667ebdb36ef7175c67253cfff4d655d82
2
+ SHA256:
3
+ metadata.gz: 6681daa3660fe33bc169a0d8ee9eae5ff2b3724d3b8a4f7a9d66f61864eff0c9
4
+ data.tar.gz: 1d1c83b0529740a02b896bba002ab4c2f47b5c80ec3fe7353ea53dabe2f68e01
5
5
  SHA512:
6
- metadata.gz: 93b8392838c910474c3c2dcdd6765ea780753842235cefacb5c3140864ab73717eb9ad2fd984adbaa2360df81c6c1c1e01a7eb82925595b9437ee0a77bf86b01
7
- data.tar.gz: 4bc44b77be297b77488db23a91f633124a308521f0505d63464835aa12bb2649b55b0c4642ea45c36880b586d5b30097582aaf9f1b98ed9735af64447160bd94
6
+ metadata.gz: ac541875f2e93d174b2bc2f6be9068522028c130f10ab401e6d8c1a9ec93a5116a1255385218364481aba968783f83784f266f814eae954d6127c9518ae0d7f8
7
+ data.tar.gz: 02cee90a78e8fa2c19fa1811d7578c7dd1d30721681c10186d15aa61c27e0749c212bcfa6d26a17a9ec6af1d1003d0c85f2f66e508e38d62f04e607a28164814
@@ -0,0 +1,8 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ - 2.6
5
+ - 2.5
6
+ - 2.4
7
+ - 2.3
8
+ script: bundle exec rspec
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'hobby-devtools', '>=0.0.5'
3
+ gem 'hobby-devtools', '>=0.0.11'
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.
@@ -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.4'
5
- g.summary = 'A way to work with JSON in Hobby without too much verbosity.'
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'
@@ -16,6 +16,7 @@ module Hobby
16
16
 
17
17
  def json
18
18
  @json ||= begin
19
+ fail unless env.fetch('CONTENT_TYPE').start_with? 'application/json'
19
20
  ::JSON.parse request.body.read
20
21
  rescue
21
22
  response.status = 400
@@ -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
+ ----
@@ -1,6 +1,7 @@
1
1
  - get:
2
2
  path: /
3
- body: not very json
3
+ format: text
4
+ body: just plain text, not json
4
5
 
5
6
  response:
6
7
  status: 400
@@ -1,6 +1,7 @@
1
1
  - get:
2
2
  path: /access_key
3
- body: not very json
3
+ format: text
4
+ body: just plain text, not json
4
5
 
5
6
  response:
6
7
  status: 400
@@ -0,0 +1,10 @@
1
+ - get:
2
+ path: /
3
+ format: json
4
+ body:
5
+ valid: json
6
+ headers:
7
+ Content-Type: text/plain
8
+
9
+ response:
10
+ status: 400
@@ -1,11 +1,15 @@
1
1
  - get:
2
2
  path: /
3
+ format: json
3
4
  body:
4
5
  string: string
5
6
  number: 42
7
+ headers:
8
+ Content-Type: application/json
6
9
 
7
10
  response:
8
11
  status: 200
12
+ format: json
9
13
  body:
10
14
  string: string
11
15
  number: 42
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
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: 2016-12-27 00:00:00.000000000 Z
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
- rubyforge_project:
61
- rubygems_version: 2.5.2
64
+ rubygems_version: 3.0.3
62
65
  signing_key:
63
66
  specification_version: 4
64
- summary: A way to work with JSON in Hobby without too much verbosity.
67
+ summary: A Hobby extension for JSON requests and responses.
65
68
  test_files: []