jsend_wrapper-rails 0.3.1 → 0.4.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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: eee04f91289f43f6ebf82e8b1ee53ec90e82ebec
|
|
4
|
+
data.tar.gz: 93be3df4245222a92426c33d595aa5f371b83ea6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: be9ce6da5c187e1c7ad1b87cd2c869acf5eaeaf33e8f54d9305bd111df4bec7b4b2b5e09e51de4049736c57a08f348006cc0ec7f093ccd16a6edb82b19723817
|
|
7
|
+
data.tar.gz: 9f4ad8062573d02f7e28eabbc8963cd0167e71ee02e776f486325252f8cbf309795401dd0c6aee4db1834a90a7bff411d25100aeca0dfa6d217867b806add048
|
data/README.md
CHANGED
|
@@ -32,7 +32,9 @@ The elements `code` and `data` are optional for JSend Error containers. If you
|
|
|
32
32
|
leave them out, they will be absent from the rendered JSON. Note the differences
|
|
33
33
|
in these two examples:
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
```ruby
|
|
36
|
+
render jsend: {error: 'too bad'}`
|
|
37
|
+
```
|
|
36
38
|
|
|
37
39
|
**Result**:
|
|
38
40
|
```json
|
|
@@ -42,7 +44,9 @@ in these two examples:
|
|
|
42
44
|
}
|
|
43
45
|
```
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
```ruby
|
|
48
|
+
render jsend: {error: 'too bad', data: nil}`
|
|
49
|
+
```
|
|
46
50
|
|
|
47
51
|
**Result**:
|
|
48
52
|
```json
|
|
@@ -104,6 +108,33 @@ end
|
|
|
104
108
|
}
|
|
105
109
|
```
|
|
106
110
|
|
|
111
|
+
#### Use Renderers directly
|
|
112
|
+
|
|
113
|
+
If you have some other purpose in mind, you can access the renderers directly:
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
require 'jsend_wrapper/renderers/success_renderer'
|
|
117
|
+
require 'jsend_wrapper/renderers/fail_renderer'
|
|
118
|
+
require 'jsend_wrapper/renderers/error_renderer'
|
|
119
|
+
|
|
120
|
+
# or, for all three:
|
|
121
|
+
require 'jsend_wrapper/renderers'
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
##### Usages
|
|
125
|
+
|
|
126
|
+
To use these renderers, construct them with the same arguments as you pass to
|
|
127
|
+
the `render` statements listed above.
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
renderer = JsendWrapper::SuccessRenderer @data
|
|
131
|
+
renderer = JsendWrapper::FailRenderer 'a message'
|
|
132
|
+
renderer = JsendWrapper::ErrorRenderer 'a message', code: 123, data: @data
|
|
133
|
+
|
|
134
|
+
json = renderer.to_s
|
|
135
|
+
hash = renderer.to_h
|
|
136
|
+
```
|
|
137
|
+
|
|
107
138
|
## JSend Specification
|
|
108
139
|
|
|
109
140
|
*This section is copied from [omniti.com](http://labs.omniti.com/labs/jsend)
|
|
@@ -29,7 +29,7 @@ module JsendWrapper
|
|
|
29
29
|
#@option optional [#to_i] :code a numeric code representing the error
|
|
30
30
|
#@option optional [Object] :data a generic container for any other
|
|
31
31
|
# information about the error
|
|
32
|
-
def initialize(message, optional)
|
|
32
|
+
def initialize(message, optional={})
|
|
33
33
|
@message = message.to_s
|
|
34
34
|
@has_code = optional.key? :code
|
|
35
35
|
@has_data = optional.key? :data
|
|
@@ -40,10 +40,16 @@ module JsendWrapper
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
#@return [String] the rendered JSON
|
|
43
|
-
def
|
|
43
|
+
def to_s
|
|
44
44
|
%[{"status":"error","message":#{message.inspect}#{optional}}]
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
+
def to_h
|
|
48
|
+
{status: 'error', message: message}.tap do |hash|
|
|
49
|
+
hash[:code] = code if code?
|
|
50
|
+
hash[:data] = data if data?
|
|
51
|
+
end
|
|
52
|
+
end
|
|
47
53
|
|
|
48
54
|
private
|
|
49
55
|
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
require 'jsend_wrapper/renderers/renderer'
|
|
17
17
|
|
|
18
18
|
module JsendWrapper
|
|
19
|
-
# Wraps the given message in a JSend Success. JSend
|
|
20
|
-
# elements (status, data).
|
|
19
|
+
# Wraps the given message in a JSend Success. JSend Successes have two
|
|
20
|
+
# required elements (status, data).
|
|
21
21
|
class SuccessRenderer < Renderer
|
|
22
22
|
attr_reader :data
|
|
23
23
|
|
|
@@ -25,8 +25,12 @@ module JsendWrapper
|
|
|
25
25
|
@data = data
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
def
|
|
28
|
+
def to_s
|
|
29
29
|
%[{"status":"success","data":#{json_string data}}]
|
|
30
30
|
end
|
|
31
|
+
|
|
32
|
+
def to_h
|
|
33
|
+
{ status: 'success', data: data }
|
|
34
|
+
end
|
|
31
35
|
end
|
|
32
36
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsend_wrapper-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jon Sangster
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-
|
|
11
|
+
date: 2014-09-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: pry
|