reply 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/reply/reply.rb +58 -26
- data/readme.md +7 -0
- data/reply.gemspec +2 -2
- data/spec/lib/reply/reply_spec.rb +88 -25
- metadata +45 -15
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/reply/reply.rb
CHANGED
@@ -1,30 +1,42 @@
|
|
1
1
|
class Reply
|
2
|
-
|
2
|
+
# simple_status either 0, 1, 2
|
3
|
+
# status can be any code e.g. 200, 401
|
4
|
+
attr_accessor :status, :data
|
5
|
+
attr_reader :simple_status, :messages
|
3
6
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
+
SIMPLE_STATUS_ERROR = 0
|
8
|
+
SIMPLE_STATUS_SUCCESS = 1
|
9
|
+
SIMPLE_STATUS_WARNING = 2
|
10
|
+
SIMPLE_STATUSES = [SIMPLE_STATUS_SUCCESS, SIMPLE_STATUS_WARNING, SIMPLE_STATUS_ERROR]
|
7
11
|
|
8
12
|
def initialize(params={})
|
9
13
|
@messages = params[:messages] || []
|
10
|
-
@status = params[:status] || STATUS_SUCCESS
|
11
14
|
@data = params[:data] || {}
|
12
|
-
@
|
15
|
+
@status = params[:status] || nil
|
16
|
+
@simple_status = params[:simple_status] || SIMPLE_STATUS_SUCCESS
|
13
17
|
end
|
14
18
|
|
15
|
-
def
|
16
|
-
|
19
|
+
def simple_status=(s)
|
20
|
+
raise(ArgumentError) unless SIMPLE_STATUSES.include?(s)
|
21
|
+
@simple_status = s
|
17
22
|
end
|
18
23
|
|
19
|
-
|
20
|
-
|
24
|
+
#-------------------------------------------------------------------------
|
25
|
+
#
|
26
|
+
#-------------------------------------------------------------------------
|
27
|
+
|
28
|
+
def clear_messages
|
29
|
+
@messages.clear
|
21
30
|
end
|
22
31
|
|
23
|
-
def add_messages(
|
32
|
+
def add_messages(array_or_str)
|
33
|
+
array = wrap_arr(array_or_str)
|
24
34
|
@messages = @messages | array
|
25
35
|
end
|
36
|
+
alias :add_message :add_messages
|
26
37
|
|
27
|
-
def replace_messages(
|
38
|
+
def replace_messages(array_or_str)
|
39
|
+
array = wrap_arr(array_or_str)
|
28
40
|
@messages = array
|
29
41
|
end
|
30
42
|
|
@@ -32,42 +44,62 @@ class Reply
|
|
32
44
|
@messages = object.errors.full_messages
|
33
45
|
end
|
34
46
|
|
35
|
-
def
|
36
|
-
|
47
|
+
def add_errors(array_or_str)
|
48
|
+
add_messages(array_or_str)
|
37
49
|
mark_as_error
|
38
50
|
end
|
51
|
+
alias :add_error :add_errors
|
39
52
|
|
40
|
-
def
|
41
|
-
|
42
|
-
mark_as_error
|
53
|
+
def mark_as_success
|
54
|
+
@simple_status = SIMPLE_STATUS_SUCCESS
|
43
55
|
end
|
56
|
+
alias :success! :mark_as_success
|
44
57
|
|
45
58
|
def mark_as_error
|
46
|
-
@
|
59
|
+
@simple_status = SIMPLE_STATUS_ERROR
|
47
60
|
end
|
48
61
|
alias :error! :mark_as_error
|
49
62
|
|
50
63
|
def mark_as_warning
|
51
|
-
@
|
64
|
+
@simple_status = SIMPLE_STATUS_WARNING
|
52
65
|
end
|
53
66
|
alias :warning! :mark_as_warning
|
54
67
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
alias :success! :mark_as_success
|
68
|
+
#-------------------------------------------------------------------------
|
69
|
+
# Predicates
|
70
|
+
#-------------------------------------------------------------------------
|
59
71
|
|
60
72
|
def success?
|
61
|
-
@
|
73
|
+
@simple_status == SIMPLE_STATUS_SUCCESS
|
62
74
|
end
|
63
75
|
alias :successful? :success?
|
64
76
|
|
65
77
|
def error?
|
66
|
-
@
|
78
|
+
@simple_status == SIMPLE_STATUS_ERROR
|
67
79
|
end
|
68
80
|
alias :failure? :error?
|
69
81
|
|
70
82
|
def warning?
|
71
|
-
@
|
83
|
+
@simple_status == SIMPLE_STATUS_WARNING
|
72
84
|
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
# Wraps the object in an Array unless it's an Array. Converts the
|
89
|
+
# object to an Array using #to_ary if it implements that.
|
90
|
+
def wrap_arr(object)
|
91
|
+
case object
|
92
|
+
when nil
|
93
|
+
[]
|
94
|
+
when Array
|
95
|
+
object
|
96
|
+
else
|
97
|
+
if object.respond_to?(:to_ary)
|
98
|
+
object.to_ary
|
99
|
+
else
|
100
|
+
[object]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
73
105
|
end
|
data/readme.md
CHANGED
@@ -42,6 +42,13 @@ Usage
|
|
42
42
|
|
43
43
|
# add some data to the reply
|
44
44
|
reply.data = something
|
45
|
+
|
46
|
+
# add a status to the reply
|
47
|
+
# this is independent of success, error and warning
|
48
|
+
reply.status = 401
|
49
|
+
|
50
|
+
# copy all the errors from an active record object
|
51
|
+
reply.replace_messages_with_errors_for(active_record_object)
|
45
52
|
```
|
46
53
|
|
47
54
|
Copyright
|
data/reply.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "reply"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sebastian Porto"]
|
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.homepage = "http://github.com/sporto/reply"
|
38
38
|
s.licenses = ["MIT"]
|
39
39
|
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version = "1.8.
|
40
|
+
s.rubygems_version = "1.8.25"
|
41
41
|
s.summary = "An standarized reply object"
|
42
42
|
|
43
43
|
if s.respond_to? :specification_version then
|
@@ -12,14 +12,70 @@ describe Reply do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
let(:reply) { Reply.new }
|
15
|
-
let(:invalid_object) { Monkey.new }
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
describe '.simple_status' do
|
17
|
+
|
18
|
+
it "is successful by default" do
|
19
|
+
expect(reply).to be_successful
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'can be set in the initialiser' do
|
23
|
+
reply = Reply.new(simple_status: 0)
|
24
|
+
expect(reply).to be_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'can be set' do
|
28
|
+
reply.simple_status = 2
|
29
|
+
expect(reply).to be_warning
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'rejects unknown' do
|
33
|
+
expect{ reply.simple_status = 4 }.to raise_error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '.status' do
|
38
|
+
it 'can be set in the initialiser' do
|
39
|
+
reply = Reply.new(status: 401)
|
40
|
+
expect(reply.status).to eq(401)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'can be set' do
|
44
|
+
reply.status = 400
|
45
|
+
expect(reply.status).to eq(400)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '.replace_messages' do
|
50
|
+
|
51
|
+
it 'accepts an array' do
|
52
|
+
arr = [1,2]
|
53
|
+
reply.replace_messages(arr)
|
54
|
+
expect(reply.messages).to eq(arr)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'accepts a string' do
|
58
|
+
str = 'x'
|
59
|
+
reply.replace_messages(str)
|
60
|
+
expect(reply.messages).to eq([str])
|
61
|
+
end
|
62
|
+
|
19
63
|
end
|
20
64
|
|
21
|
-
|
22
|
-
|
65
|
+
describe '.add_messages' do
|
66
|
+
|
67
|
+
it 'accepts an array' do
|
68
|
+
arr = [1,2]
|
69
|
+
reply.add_messages(arr)
|
70
|
+
expect(reply.messages).to eq(arr)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'accepts a string' do
|
74
|
+
str = 'x'
|
75
|
+
reply.add_messages(str)
|
76
|
+
expect(reply.messages).to eq([str])
|
77
|
+
end
|
78
|
+
|
23
79
|
end
|
24
80
|
|
25
81
|
describe ".add_error" do
|
@@ -50,14 +106,17 @@ describe Reply do
|
|
50
106
|
end
|
51
107
|
end
|
52
108
|
|
53
|
-
describe ".
|
54
|
-
|
55
|
-
|
56
|
-
reply.
|
109
|
+
describe ".mark_as_success" do
|
110
|
+
it "marks the reply as success" do
|
111
|
+
reply.mark_as_success
|
112
|
+
expect(reply).to be_successful
|
57
113
|
end
|
114
|
+
end
|
58
115
|
|
59
|
-
|
60
|
-
|
116
|
+
describe ".success!" do
|
117
|
+
it "marks the reply as success" do
|
118
|
+
reply.success!
|
119
|
+
expect(reply).to be_successful
|
61
120
|
end
|
62
121
|
end
|
63
122
|
|
@@ -75,20 +134,6 @@ describe Reply do
|
|
75
134
|
end
|
76
135
|
end
|
77
136
|
|
78
|
-
describe ".mark_as_success" do
|
79
|
-
it "marks the reply as success" do
|
80
|
-
reply.mark_as_success
|
81
|
-
expect(reply).to be_successful
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe ".success!" do
|
86
|
-
it "marks the reply as success" do
|
87
|
-
reply.success!
|
88
|
-
expect(reply).to be_successful
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
137
|
describe ".mark_as_warning" do
|
93
138
|
it "marks the reply as warning" do
|
94
139
|
reply.mark_as_warning
|
@@ -103,4 +148,22 @@ describe Reply do
|
|
103
148
|
end
|
104
149
|
end
|
105
150
|
|
151
|
+
describe ".replace_messages_with_errors_for" do
|
152
|
+
let(:invalid_object) { Monkey.new }
|
153
|
+
|
154
|
+
before do
|
155
|
+
invalid_object.valid?
|
156
|
+
reply.replace_messages_with_errors_for(invalid_object)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "is invalid" do
|
160
|
+
expect(invalid_object).not_to be_valid
|
161
|
+
end
|
162
|
+
|
163
|
+
it "copies the messages" do
|
164
|
+
expect(reply.messages.size).to eq(1)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
|
106
169
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reply
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-09-25 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: guard-rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rdoc
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '3.12'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.12'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: bundler
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ~>
|
@@ -54,10 +69,15 @@ dependencies:
|
|
54
69
|
version: '1.0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.0'
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: jeweler
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
83
|
- - ~>
|
@@ -65,10 +85,15 @@ dependencies:
|
|
65
85
|
version: 1.8.7
|
66
86
|
type: :development
|
67
87
|
prerelease: false
|
68
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.8.7
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: active_attr
|
71
|
-
requirement:
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
72
97
|
none: false
|
73
98
|
requirements:
|
74
99
|
- - ~>
|
@@ -76,7 +101,12 @@ dependencies:
|
|
76
101
|
version: '0.8'
|
77
102
|
type: :development
|
78
103
|
prerelease: false
|
79
|
-
version_requirements:
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0.8'
|
80
110
|
description: An standarized reply object
|
81
111
|
email: s@porto5.com
|
82
112
|
executables: []
|
@@ -116,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
146
|
version: '0'
|
117
147
|
segments:
|
118
148
|
- 0
|
119
|
-
hash:
|
149
|
+
hash: -1892639974551080489
|
120
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
151
|
none: false
|
122
152
|
requirements:
|
@@ -125,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
155
|
version: '0'
|
126
156
|
requirements: []
|
127
157
|
rubyforge_project:
|
128
|
-
rubygems_version: 1.8.
|
158
|
+
rubygems_version: 1.8.25
|
129
159
|
signing_key:
|
130
160
|
specification_version: 3
|
131
161
|
summary: An standarized reply object
|