jsender 0.2.2 → 1.1.0
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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +3 -1
- data/README.md +35 -44
- data/Rakefile +5 -5
- data/jsender.gemspec +5 -4
- data/lib/jsender.rb +63 -24
- data/lib/jsender/version.rb +1 -1
- metadata +41 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e4c38c81ff6bb317e2715c8df12d0c983c83a96
|
4
|
+
data.tar.gz: e6df08c2c1f7fefb0b45cb5e6f7b51f36157638c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c72f304dd65b6eddd5fe69c4aa0649b972d6be0bb4f10def994f78e86054aca0125fdb8c6d33be030a5ecf998523efcc2d45b2bdce3f21ae7e4188a9b99ea34
|
7
|
+
data.tar.gz: bc9f62329360394a99a6a5c71fc5119d6c7715d4f88e3cb3e554100c1b95588dee4e0fd01c1f5d3857c2bda5ac9330f49dd3247435952929c25fc493e142218f
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
# Jsender
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/jsender)
|
4
|
+
[](https://travis-ci.org/hetznerZA/jsender)
|
5
|
+
[](https://coveralls.io/github/hetznerZA/jsender?branch=master)
|
6
|
+
[](https://www.codacy.com/app/ernst-van-graan/jsender?utm_source=github.com&utm_medium=referral&utm_content=hetznerZA/jsender&utm_campaign=Badge_Grade)
|
4
7
|
|
5
8
|
JSender facilitates a simple jsend implementation for ruby. You can report success, error, fail, success with data, fail with data. The jsend response contains 'status' and 'data'. 'data' contains what-ever you put in it, as well as a 'notifications' array. Helpers are provided to check whether a notification is present and whether a specific data key is present. On error the response contains 'message'.
|
6
9
|
|
10
|
+
For more info about JSend refer to https://labs.omniti.com/labs/jsend
|
11
|
+
|
7
12
|
## Installation
|
8
13
|
|
9
14
|
Add this line to your application's Gemfile:
|
@@ -21,84 +26,71 @@ Or install it yourself as:
|
|
21
26
|
$ gem install jsender
|
22
27
|
|
23
28
|
## Usage
|
24
|
-
|
25
|
-
```
|
26
|
-
require 'jsender'
|
27
|
-
|
28
|
-
class CodeClass
|
29
|
-
include Jsender
|
30
|
-
end
|
31
|
-
```
|
32
|
-
|
33
|
-
```
|
34
|
-
iut = CodeClass.new
|
35
|
-
```
|
36
|
-
|
37
29
|
### Returns Ruby Hash
|
38
30
|
|
39
31
|
```
|
40
|
-
|
41
|
-
=> {"status"=>"success", "data"=>{"result"=>nil, "notifications"=>["success"]}}
|
32
|
+
Jsender.success
|
33
|
+
=> {"status"=>"success", "data"=>{"result"=>nil, "notifications"=>["success"]}}
|
42
34
|
|
43
|
-
|
44
|
-
=> {"status"=>"success", "data"=>{"result"=>nil, "notifications"=>["happy day"]}}
|
35
|
+
Jsender.success('happy day')
|
36
|
+
=> {"status"=>"success", "data"=>{"result"=>nil, "notifications"=>["happy day"]}}
|
45
37
|
|
46
|
-
result =
|
47
|
-
=> {"status"=>"success", "data"=>{"a"=>"A", "b"=>"B", "notifications"=>["success"]}}
|
48
|
-
|
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')
|
49
41
|
=> true
|
50
42
|
|
51
|
-
result =
|
52
|
-
=> {"status"=>"success", "data"=>{"result"=>["d", "a", "t", "a"], "notifications"=>["some data for you"]}}
|
53
|
-
|
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')
|
54
46
|
=> true
|
55
|
-
|
47
|
+
Jsender.notifications_include?(result, 'ata fo')
|
56
48
|
=> true
|
57
49
|
|
58
|
-
|
59
|
-
=> {"status"=>"error", "message"=>nil}
|
50
|
+
Jsender.error
|
51
|
+
=> {"status"=>"error", "message"=>nil}
|
60
52
|
|
61
|
-
|
62
|
-
=> {"status"=>"error", "message"=>"something went wrong"}
|
53
|
+
Jsender.error('something went wrong')
|
54
|
+
=> {"status"=>"error", "message"=>"something went wrong"}
|
63
55
|
|
64
|
-
|
65
|
-
=> {"status"=>"fail", "data"=>{"result"=>nil, "notifications"=>["fail"]}}
|
56
|
+
Jsender.failure
|
57
|
+
=> {"status"=>"fail", "data"=>{"result"=>nil, "notifications"=>["fail"]}}
|
66
58
|
|
67
|
-
|
68
|
-
=> {"status"=>"fail", "data"=>{"result"=>nil, "notifications"=>["a failure occurred"]}}
|
59
|
+
Jsender.failure('a failure occurred')
|
60
|
+
=> {"status"=>"fail", "data"=>{"result"=>nil, "notifications"=>["a failure occurred"]}}
|
69
61
|
|
70
|
-
|
71
|
-
=> {"status"=>"fail", "data"=>{"result"=>["d", "a", "t", "a"], "notifications"=>["a failure occurred"]}}
|
62
|
+
Jsender.failure('a failure occurred', ['d', 'a', 't', 'a'])
|
63
|
+
=> {"status"=>"fail", "data"=>{"result"=>["d", "a", "t", "a"], "notifications"=>["a failure occurred"]}}
|
72
64
|
```
|
73
65
|
|
74
66
|
### Returns JSON
|
75
67
|
|
76
68
|
```
|
77
|
-
|
69
|
+
Jsender.success_json
|
78
70
|
=> "{\"status\":\"success\", \"data\": null}"
|
79
71
|
|
80
|
-
|
72
|
+
Jsender.success_json({:key1 => 'value1'})
|
81
73
|
=> "{\"status\":\"success\",\"data\":{\"key1\":\"value1\"}}"
|
82
74
|
|
83
|
-
|
75
|
+
Jsender.fail_json
|
84
76
|
=> "{\"status\": \"fail\", \"data\": null}"
|
85
77
|
|
86
|
-
|
78
|
+
Jsender.fail_json({:key1 => "value1"})
|
87
79
|
=> "{\"status\":\"fail\",\"data\":{\"key1\":\"value1\"}}"
|
88
80
|
|
89
|
-
|
81
|
+
Jsender.error_json
|
90
82
|
=> ArgumentError, 'Missing required argument message'
|
91
83
|
|
92
|
-
|
84
|
+
Jsender.error_json('My little error')
|
93
85
|
=> "{\"status\":\"error\", \"message\":\"My little error\"}"
|
94
86
|
|
95
|
-
|
87
|
+
Jsender.error_json('Another little error', 401)
|
96
88
|
=> "{\"status\":\"error\",\"message\":\"Another little error\",\"code\":401}"
|
97
89
|
|
98
|
-
|
90
|
+
Jsender.error_json('Another little error', 401, {:key1 => 'cause of another little error'})
|
99
91
|
=> "{\"status\":\"error\",\"message\":\"Another little error\",\"code\":401,\"data\":{\"key1\":\"cause of another little error\"}}"
|
100
92
|
|
101
|
-
|
93
|
+
Jsender.error_json('Another little error', {:key1 => 'cause of another little error'})
|
102
94
|
=> "{\"status\":\"error\",\"message\":\"Another little error\",\"data\":{\"key1\":\"cause of another little error\"}}"
|
103
95
|
```
|
104
96
|
|
@@ -116,4 +108,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/hetzne
|
|
116
108
|
## License
|
117
109
|
|
118
110
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
119
|
-
|
data/Rakefile
CHANGED
data/jsender.gemspec
CHANGED
@@ -20,8 +20,9 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
spec.required_ruby_version = ['>=2.0.0']
|
22
22
|
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.
|
24
|
-
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "rspec"
|
26
|
-
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency 'coveralls'
|
27
|
+
spec.add_development_dependency 'byebug'
|
27
28
|
end
|
data/lib/jsender.rb
CHANGED
@@ -1,25 +1,26 @@
|
|
1
1
|
require 'jsender/version'
|
2
2
|
|
3
3
|
module Jsender
|
4
|
+
extend self
|
4
5
|
|
5
6
|
def report(status, message, result = nil)
|
6
|
-
return {
|
7
|
+
return {'status' => 'error', 'message' => message} if status == 'error'
|
7
8
|
data = compile_data(result)
|
8
|
-
data['notifications'] = message.is_a?(Array) ? message : [
|
9
|
-
{
|
9
|
+
data['notifications'] = message.is_a?(Array) ? message : [message]
|
10
|
+
{'status' => status, 'data' => data}
|
10
11
|
end
|
11
12
|
|
12
13
|
def error(message = nil)
|
13
14
|
report('error', message)
|
14
15
|
end
|
15
16
|
|
16
|
-
def
|
17
|
+
def failure(message = nil, data = nil)
|
17
18
|
message ||= 'fail'
|
18
19
|
report('fail', message, data)
|
19
20
|
end
|
20
21
|
|
21
22
|
def fail_data(data = nil)
|
22
|
-
|
23
|
+
failure(nil, data)
|
23
24
|
end
|
24
25
|
|
25
26
|
def success_data(data = nil)
|
@@ -39,7 +40,7 @@ module Jsender
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def notifications_include?(result, pattern)
|
42
|
-
return false
|
43
|
+
return false unless has_data?(result, 'notifications')
|
43
44
|
result['data']['notifications'].to_s.include?(pattern)
|
44
45
|
end
|
45
46
|
|
@@ -47,22 +48,22 @@ module Jsender
|
|
47
48
|
# @param data optional [Hash]
|
48
49
|
# @return [String] jsend json
|
49
50
|
def success_json(data = nil)
|
50
|
-
raise ArgumentError, 'Optional data argument should be of type Hash' if
|
51
|
+
raise ArgumentError, 'Optional data argument should be of type Hash' if invalid_hash?(data)
|
51
52
|
return JSON.generate({
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
:status => 'success',
|
54
|
+
:data => data
|
55
|
+
})
|
55
56
|
end
|
56
57
|
|
57
58
|
##
|
58
59
|
# @param data optional [Hash]
|
59
60
|
# @return [String] jsend json
|
60
61
|
def fail_json(data = nil)
|
61
|
-
raise ArgumentError, 'Optional data argument should be of type Hash' if
|
62
|
+
raise ArgumentError, 'Optional data argument should be of type Hash' if invalid_hash?(data)
|
62
63
|
return JSON.generate({
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
:status => 'fail',
|
65
|
+
:data => data
|
66
|
+
})
|
66
67
|
end
|
67
68
|
|
68
69
|
##
|
@@ -71,27 +72,65 @@ module Jsender
|
|
71
72
|
# @param data optional [Hash]
|
72
73
|
# @return [String] jsend json
|
73
74
|
def error_json(msg, code = nil, data = nil)
|
74
|
-
|
75
|
-
code, data = nil, code if not code.is_a? Integer and code.is_a? Hash and data.nil?
|
76
|
-
raise ArgumentError, 'Optional data argument should be of type Hash' if not data.nil? and not data.is_a? Hash
|
77
|
-
raise ArgumentError, 'Optional code argument should be of type Integer' if not code.nil? and not code.is_a? Integer
|
75
|
+
code, data = validate_and_sanitize(msg, code, data)
|
78
76
|
jsend = {
|
79
|
-
|
80
|
-
|
77
|
+
:status => 'error',
|
78
|
+
:message => msg
|
81
79
|
}
|
82
|
-
jsend
|
83
|
-
jsend['data'] = data if not data.nil?
|
84
|
-
return JSON.generate(jsend)
|
80
|
+
generate_error_json(jsend, code, data)
|
85
81
|
end
|
86
82
|
|
87
83
|
private
|
88
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
|
+
|
89
127
|
def compile_data(result)
|
90
128
|
data ||= {}
|
91
|
-
result = {
|
129
|
+
result = {'result' => result} unless result.is_a? Hash
|
92
130
|
result.each do |key, value|
|
93
131
|
data[key] = value
|
94
132
|
end
|
95
133
|
data
|
96
134
|
end
|
135
|
+
|
97
136
|
end
|
data/lib/jsender/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsender
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ernst van Graan
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-08-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -17,42 +17,70 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '1.
|
20
|
+
version: '1.3'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '1.
|
27
|
+
version: '1.3'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rspec
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
48
|
+
version: '0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- - "
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: coveralls
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: byebug
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
54
82
|
- !ruby/object:Gem::Version
|
55
|
-
version:
|
83
|
+
version: '0'
|
56
84
|
description: JSender facilitates a simple jsend implementation for ruby
|
57
85
|
email:
|
58
86
|
- ernst.van.graan@hetzner.co.za
|
@@ -94,9 +122,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
122
|
version: '0'
|
95
123
|
requirements: []
|
96
124
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.6.11
|
98
126
|
signing_key:
|
99
127
|
specification_version: 4
|
100
128
|
summary: JSender facilitates a simple jsend implementation for ruby
|
101
129
|
test_files: []
|
102
|
-
has_rdoc:
|