jsender 1.1.0 → 2.0.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: 2e4c38c81ff6bb317e2715c8df12d0c983c83a96
4
- data.tar.gz: e6df08c2c1f7fefb0b45cb5e6f7b51f36157638c
3
+ metadata.gz: d54b8d2efe09a714ff7297af5afe424b569460ba
4
+ data.tar.gz: 2200fa05c6037642b3230290968326d35ea34ae7
5
5
  SHA512:
6
- metadata.gz: 8c72f304dd65b6eddd5fe69c4aa0649b972d6be0bb4f10def994f78e86054aca0125fdb8c6d33be030a5ecf998523efcc2d45b2bdce3f21ae7e4188a9b99ea34
7
- data.tar.gz: bc9f62329360394a99a6a5c71fc5119d6c7715d4f88e3cb3e554100c1b95588dee4e0fd01c1f5d3857c2bda5ac9330f49dd3247435952929c25fc493e142218f
6
+ metadata.gz: a12515fd684d1502c917f289a55d65e9fc0dc87f11f13127711fea0f099385059bfc4d5f3f253d166a559086c7c814c125d279fc4f56c2eee8cd792cd6aedd65
7
+ data.tar.gz: 4e47367fdc446fc5f3da1063580942b4fe82775e1ba3c32e8f127bc122c5654ddabe66014fa3ab77e4c3c30c6d0014a81ed0f14407dc25ba07a3eda46b0dafe5
data/README.md CHANGED
@@ -19,81 +19,81 @@ gem 'jsender'
19
19
 
20
20
  And then execute:
21
21
 
22
- $ bundle
22
+ ```bash
23
+ bundle
24
+ ```
23
25
 
24
26
  Or install it yourself as:
25
27
 
26
- $ gem install jsender
28
+ ```bash
29
+ gem install jsender
30
+ ```
27
31
 
28
32
  ## Usage
29
- ### Returns Ruby Hash
30
-
31
- ```
32
- Jsender.success
33
- => {"status"=>"success", "data"=>{"result"=>nil, "notifications"=>["success"]}}
33
+ ### Returns JSON
34
34
 
35
- Jsender.success('happy day')
36
- => {"status"=>"success", "data"=>{"result"=>nil, "notifications"=>["happy day"]}}
35
+ Basic usage without any parameters yielding default json encoded jsend format:
37
36
 
38
- result = Jsender.success_data({ 'a' => 'A', 'b' => 'B' })
39
- => {"status"=>"success", "data"=>{"a"=>"A", "b"=>"B", "notifications"=>["success"]}}
40
- Jsender.has_data?(result, 'b')
41
- => true
37
+ ```ruby
38
+ Jsender::Json.success
39
+ => "{\"status\":\"success\",\"data\":null}"
42
40
 
43
- result = Jsender.success('some data for you', ['d', 'a', 't', 'a'])
44
- => {"status"=>"success", "data"=>{"result"=>["d", "a", "t", "a"], "notifications"=>["some data for you"]}}
45
- Jsender.has_data?(result, 'result')
46
- => true
47
- Jsender.notifications_include?(result, 'ata fo')
48
- => true
41
+ Jsender::Json.failure
42
+ => "{\"status\":\"fail\",\"data\":{\"message\":\"A failure has occurred\"}}"
49
43
 
50
- Jsender.error
51
- => {"status"=>"error", "message"=>nil}
44
+ Jsender::Json.error
45
+ => "{\"status\":\"error\",\"message\":\"An error has occurred\"}"
46
+ ```
52
47
 
53
- Jsender.error('something went wrong')
54
- => {"status"=>"error", "message"=>"something went wrong"}
48
+ Or with parameters yielding the correct json encoded jsend format:
55
49
 
56
- Jsender.failure
57
- => {"status"=>"fail", "data"=>{"result"=>nil, "notifications"=>["fail"]}}
50
+ ```ruby
51
+ Jsender::Json.success(data: {'key' => 'value'})
52
+ => "{\"status\":\"success\",\"data\":{\"key\":\"value\"}}"
58
53
 
59
- Jsender.failure('a failure occurred')
60
- => {"status"=>"fail", "data"=>{"result"=>nil, "notifications"=>["a failure occurred"]}}
54
+ Jsender::Json.failure(message: 'custom message')
55
+ => "{\"status\":\"fail\",\"data\":{\"message\":\"custom message\"}}"
61
56
 
62
- Jsender.failure('a failure occurred', ['d', 'a', 't', 'a'])
63
- => {"status"=>"fail", "data"=>{"result"=>["d", "a", "t", "a"], "notifications"=>["a failure occurred"]}}
57
+ Jsender::Json.error(message: 'custom message')
58
+ => "{\"status\":\"error\",\"message\":\"custom message\"}"
64
59
  ```
65
60
 
66
- ### Returns JSON
67
-
68
- ```
69
- Jsender.success_json
70
- => "{\"status\":\"success\", \"data\": null}"
61
+ ### Returns Rack Response Tuple
71
62
 
72
- Jsender.success_json({:key1 => 'value1'})
73
- => "{\"status\":\"success\",\"data\":{\"key1\":\"value1\"}}"
63
+ Basic usage without any parameters yielding default json encoded jsend format in a Rack tuple:
74
64
 
75
- Jsender.fail_json
76
- => "{\"status\": \"fail\", \"data\": null}"
65
+ ```ruby
66
+ Jsender::Rack.success
67
+ => [200, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>nil}, "{\"status\":\"success\",\"data\":null}"]
77
68
 
78
- Jsender.fail_json({:key1 => "value1"})
79
- => "{\"status\":\"fail\",\"data\":{\"key1\":\"value1\"}}"
69
+ Jsender::Rack.failure
70
+ => [400, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>nil}, "{\"status\":\"fail\",\"data\":{\"message\":\"A failure has occurred\"}}"]
80
71
 
81
- Jsender.error_json
82
- => ArgumentError, 'Missing required argument message'
72
+ Jsender::Rack.error
73
+ => [500, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>nil}, "{\"status\":\"error\",\"message\":\"An error has occurred\"}"]
74
+ ```
83
75
 
84
- Jsender.error_json('My little error')
85
- => "{\"status\":\"error\", \"message\":\"My little error\"}"
76
+ Or with parameters yielding the correct json encoded jsend format in a Rack tuple for use in controllers (including Sinatra):
86
77
 
87
- Jsender.error_json('Another little error', 401)
88
- => "{\"status\":\"error\",\"message\":\"Another little error\",\"code\":401}"
78
+ ```ruby
79
+ Jsender::Rack.success(data: {'key' => 'value'}, code: 201, flow_id: '123')
80
+ => [201, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>"123"}, "{\"status\":\"success\",\"data\":{\"key\":\"value\"}}"]
89
81
 
90
- Jsender.error_json('Another little error', 401, {:key1 => 'cause of another little error'})
91
- => "{\"status\":\"error\",\"message\":\"Another little error\",\"code\":401,\"data\":{\"key1\":\"cause of another little error\"}}"
82
+ Jsender::Rack.failure(message: 'some custom failure message', code: 201, flow_id: '123')
83
+ => [201, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>"123"}, "{\"status\":\"fail\",\"data\":{\"message\":\"some custom failure message\"}}"]
92
84
 
93
- Jsender.error_json('Another little error', {:key1 => 'cause of another little error'})
94
- => "{\"status\":\"error\",\"message\":\"Another little error\",\"data\":{\"key1\":\"cause of another little error\"}}"
85
+ Jsender::Rack.error(message: 'some custom failure message', code: 201, flow_id: '123')
86
+ => [201, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>"123"}, "{\"status\":\"error\",\"message\":\"some custom failure message\"}"]
95
87
  ```
96
88
 
89
+ ### Returns Rack Response Tuple for Middlewares
90
+
91
+ Rack middlware responses require that the body of the response tuple is in an array. Enable this using the body_as_array parameter (false by default):
92
+
93
+ ```ruby
94
+ Jsender::Rack.error(body_as_array: true)
95
+ => [500, {"Content-Type"=>"application/json", "X-Flow-Identifier"=>nil}, ["{\"status\":\"error\",\"message\":\"An error has occurred\"}"]]
96
+ ```
97
97
 
98
98
  ## Development
99
99
 
data/jsender.gemspec CHANGED
@@ -6,11 +6,11 @@ require 'jsender/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "jsender"
8
8
  spec.version = Jsender::VERSION
9
- spec.authors = ["Ernst van Graan", "Charles Mulder"]
10
- spec.email = ["ernst.van.graan@hetzner.co.za", "charles.mulder@hetzner.co.za"]
9
+ spec.authors = ["Hetzner Seals Team"]
10
+ spec.email = ["seals@hetzner.co.za"]
11
11
 
12
12
  spec.summary = %q{JSender facilitates a simple jsend implementation for ruby}
13
- spec.description = %q{JSender facilitates a simple jsend implementation for ruby}
13
+ spec.description = %q{JSender facilitates a simple jsend implementation for ruby with json and rack helpers}
14
14
  spec.homepage = "https://github.com/hetznerZA/jsender"
15
15
  spec.license = "MIT"
16
16
 
data/lib/jsender.rb CHANGED
@@ -1,136 +1,6 @@
1
1
  require 'jsender/version'
2
+ require 'jsender/json'
3
+ require 'jsender/rack'
2
4
 
3
5
  module Jsender
4
- extend self
5
-
6
- def report(status, message, result = nil)
7
- return {'status' => 'error', 'message' => message} if status == 'error'
8
- data = compile_data(result)
9
- data['notifications'] = message.is_a?(Array) ? message : [message]
10
- {'status' => status, 'data' => data}
11
- end
12
-
13
- def error(message = nil)
14
- report('error', message)
15
- end
16
-
17
- def failure(message = nil, data = nil)
18
- message ||= 'fail'
19
- report('fail', message, data)
20
- end
21
-
22
- def fail_data(data = nil)
23
- failure(nil, data)
24
- end
25
-
26
- def success_data(data = nil)
27
- success(nil, data)
28
- end
29
-
30
- def success(message = nil, data = nil)
31
- message ||= 'success'
32
- report('success', message, data)
33
- end
34
-
35
- def has_data?(result, key = nil)
36
- return false if key.nil?
37
- return false if (result.nil?) or (result['data'].nil?)
38
- return false if (not key.nil?) and (result['data'][key].nil?)
39
- true
40
- end
41
-
42
- def notifications_include?(result, pattern)
43
- return false unless has_data?(result, 'notifications')
44
- result['data']['notifications'].to_s.include?(pattern)
45
- end
46
-
47
- ##
48
- # @param data optional [Hash]
49
- # @return [String] jsend json
50
- def success_json(data = nil)
51
- raise ArgumentError, 'Optional data argument should be of type Hash' if invalid_hash?(data)
52
- return JSON.generate({
53
- :status => 'success',
54
- :data => data
55
- })
56
- end
57
-
58
- ##
59
- # @param data optional [Hash]
60
- # @return [String] jsend json
61
- def fail_json(data = nil)
62
- raise ArgumentError, 'Optional data argument should be of type Hash' if invalid_hash?(data)
63
- return JSON.generate({
64
- :status => 'fail',
65
- :data => data
66
- })
67
- end
68
-
69
- ##
70
- # @param msg [String]
71
- # @param code optional [Integer]
72
- # @param data optional [Hash]
73
- # @return [String] jsend json
74
- def error_json(msg, code = nil, data = nil)
75
- code, data = validate_and_sanitize(msg, code, data)
76
- jsend = {
77
- :status => 'error',
78
- :message => msg
79
- }
80
- generate_error_json(jsend, code, data)
81
- end
82
-
83
- private
84
-
85
- def invalid_integer?(value)
86
- (not value.nil?) and (not value.is_a? Integer)
87
- end
88
-
89
- def invalid_hash?(data)
90
- if not data.nil?
91
- return true if not data.is_a? Hash
92
- end
93
- false
94
- end
95
-
96
- def generate_error_json(jsend, code, data)
97
- jsend['code'] = code unless code.nil?
98
- jsend['data'] = data unless data.nil?
99
- return JSON.generate(jsend)
100
- end
101
-
102
- def validate_and_sanitize(msg, code, data)
103
- validate_message(msg)
104
- code, data = set_code_and_data(code, data)
105
- validate_data(data)
106
- validate_code(code)
107
- return code, data
108
- end
109
-
110
- def set_code_and_data(code, data)
111
- code, data = nil, code if not code.is_a? Integer and code.is_a? Hash and data.nil?
112
- return code, data
113
- end
114
-
115
- def validate_message(msg)
116
- raise ArgumentError, 'Missing required message of type String' if msg.empty? or not msg.is_a? String
117
- end
118
-
119
- def validate_data(data)
120
- raise ArgumentError, 'Optional data argument should be of type Hash' if invalid_hash?(data)
121
- end
122
-
123
- def validate_code(code)
124
- raise ArgumentError, 'Optional code argument should be of type Integer' if invalid_integer?(code)
125
- end
126
-
127
- def compile_data(result)
128
- data ||= {}
129
- result = {'result' => result} unless result.is_a? Hash
130
- result.each do |key, value|
131
- data[key] = value
132
- end
133
- data
134
- end
135
-
136
6
  end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+
3
+ module Jsender
4
+ module Json
5
+ extend self
6
+
7
+ def error(message: "An error has occurred")
8
+ {
9
+ 'status' => 'error',
10
+ 'message' => message
11
+ }.to_json
12
+ end
13
+
14
+ def failure(message: "A failure has occurred")
15
+ {
16
+ 'status' => 'fail',
17
+ 'data' => {
18
+ 'message' => message
19
+ }
20
+ }.to_json
21
+ end
22
+
23
+ def success(data: nil)
24
+ {
25
+ 'status' => 'success',
26
+ 'data' => data
27
+ }.to_json
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ module Jsender
2
+ module Rack
3
+ extend self
4
+
5
+ def error(code: 500, flow_id: nil, message: "An error has occurred", body_as_array: false)
6
+ [
7
+ code,
8
+ headers(flow_id: flow_id),
9
+ body(data: Jsender::Json.error(message: message), body_as_array: body_as_array)
10
+ ]
11
+ end
12
+
13
+ def failure(code: 400, flow_id: nil, message: "A failure has occurred", body_as_array: false)
14
+ [
15
+ code,
16
+ headers(flow_id: flow_id),
17
+ body(data: Jsender::Json.failure(message: message), body_as_array: body_as_array)
18
+ ]
19
+ end
20
+
21
+ def success(code: 200, flow_id: nil, data: nil, body_as_array: false)
22
+ [
23
+ code,
24
+ headers(flow_id: flow_id),
25
+ body(data: Jsender::Json.success(data: data), body_as_array: body_as_array)
26
+ ]
27
+ end
28
+
29
+ private
30
+
31
+ def body(data:, body_as_array: false)
32
+ return [data] if body_as_array
33
+ data
34
+ end
35
+
36
+ def headers(flow_id:)
37
+ {
38
+ 'Content-Type' => 'application/json',
39
+ 'X-Flow-Identifier' => flow_id
40
+ }
41
+ end
42
+ end
43
+
44
+ end
@@ -1,3 +1,3 @@
1
1
  module Jsender
2
- VERSION = "1.1.0"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsender
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - Ernst van Graan
8
- - Charles Mulder
7
+ - Hetzner Seals Team
9
8
  autorequire:
10
9
  bindir: exe
11
10
  cert_chain: []
12
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2018-01-18 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
@@ -81,10 +80,10 @@ dependencies:
81
80
  - - ">="
82
81
  - !ruby/object:Gem::Version
83
82
  version: '0'
84
- description: JSender facilitates a simple jsend implementation for ruby
83
+ description: JSender facilitates a simple jsend implementation for ruby with json
84
+ and rack helpers
85
85
  email:
86
- - ernst.van.graan@hetzner.co.za
87
- - charles.mulder@hetzner.co.za
86
+ - seals@hetzner.co.za
88
87
  executables: []
89
88
  extensions: []
90
89
  extra_rdoc_files: []
@@ -92,7 +91,6 @@ files:
92
91
  - ".gitignore"
93
92
  - ".rspec"
94
93
  - ".travis.yml"
95
- - CODE_OF_CONDUCT.md
96
94
  - Gemfile
97
95
  - LICENSE.txt
98
96
  - README.md
@@ -101,6 +99,8 @@ files:
101
99
  - bin/setup
102
100
  - jsender.gemspec
103
101
  - lib/jsender.rb
102
+ - lib/jsender/json.rb
103
+ - lib/jsender/rack.rb
104
104
  - lib/jsender/version.rb
105
105
  homepage: https://github.com/hetznerZA/jsender
106
106
  licenses:
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  requirements: []
124
124
  rubyforge_project:
125
- rubygems_version: 2.6.11
125
+ rubygems_version: 2.6.13
126
126
  signing_key:
127
127
  specification_version: 4
128
128
  summary: JSender facilitates a simple jsend implementation for ruby
data/CODE_OF_CONDUCT.md DELETED
File without changes