holla_back 0.0.1 → 0.1.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: 4ccaa754e972d5506f0559b10564e8685e601fce
4
- data.tar.gz: 163b69aa7405593d6ca55330fa2ee99dd5dbedcb
3
+ metadata.gz: 2d3cdb1ab0b7097d38b9e53bdb9e2fb844718d6e
4
+ data.tar.gz: 03802d0d26af9674af2c28bbedbcc504e29b4e67
5
5
  SHA512:
6
- metadata.gz: 00594d4f2e7b168846de8365ae2ad27f8f4880eae097dad9733bb78151986fa576065ef9cfdebcbe13dc4cb34da2c744482020d3858766717e50903c542e8d2d
7
- data.tar.gz: e4888389ef496d624db493feb8c863764696e62fab5f9d498213b168864d35223f0a17b686a1cad84690e83b42fba586eaf7a2f05254e068a6404d30d369a075
6
+ metadata.gz: b11a00e5d99920efd607c666810a07d8b2d88bfc59172a2bd3341101d60e4daee4280032906b0cbca54b9bc9198770b04bf2d0f6251a8aa925b7318f97e3f714
7
+ data.tar.gz: e8a4fefa7efc0edc69e16f9abec94f801699622d38c063b54f16e8bed01817aa445abe5cab873c9145599106938cd27619a8502dcba0409179577f0806c47a07
data/README.md CHANGED
@@ -21,73 +21,87 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- Ponder the hypothetical class with HollaBack included that will be used for these examples as defined below:
24
+ Ponder the hypothetical class with HollaBack included that will be used for these examples:
25
25
 
26
26
  > `some_class.rb`
27
27
 
28
- ```ruby
29
- class SomeClass
30
- include HollaBack
28
+ class SomeClass
29
+ include HollaBack
31
30
 
32
- def truthy_method
33
- true
34
- end
31
+ def truthy_method
32
+ true
33
+ end
35
34
 
36
- def falsey_method
37
- [nil, false].sample
38
- end
35
+ def falsey_method
36
+ [nil, false].sample
37
+ end
39
38
 
40
- def conditional_method_with_params(condition=nil)
41
- condition ? true : false
42
- end
39
+ def conditional_method_with_params(condition=nil)
40
+ condition ? true : false
41
+ end
42
+
43
+ def info_method
44
+ 'some random info'
45
+ end
46
+
47
+ def optional_method
48
+ 'some random info'
49
+ end
50
+
51
+ def optional_method_with_params(param=nil)
52
+ param ? true : false
53
+ end
54
+ end
43
55
 
44
- def info_method
45
- 'some random info'
46
- end
47
- end
48
- ```
49
56
 
50
57
  Simple example:
51
58
 
52
- ```ruby
53
- klass = SomeClass.new
59
+ klass = SomeClass.new
60
+
61
+ # Minimal request
62
+ response = klass.response(status_method: 'truthy_method')
63
+ # => #<HollaBack::Response>
64
+ response.successful?
65
+ # => true
66
+ response.status_message
67
+ # => "Status: successful"
68
+
69
+ With params in the status method:
70
+
71
+ klass = SomeClass.new
54
72
 
55
- # Minimal request
56
- response = klass.response(status_method: 'truthy_method')
57
- # => #<HollaBack::Response>
58
- response.successful?
59
- # => true
60
- response.status_message
61
- # => "Status: successful"
62
- ```
73
+ # Minimal request
74
+ response = klass.response(status_method: {optional_method_with_params: [true]})
75
+ # => #<HollaBack::Response>
76
+ response.successful?
77
+ # => true
78
+ response.status_message
79
+ # => "Status: successful"
63
80
 
64
81
  With customized status messages:
65
82
 
66
- ```ruby
67
- # If we have a truthy status method
68
- response = klass.response(status_method: 'truthy_method', success_message: "It worked! :D", failure_message: "It didn't work! D:")
69
- # => #<HollaBack::Response>
70
- response.successful?
71
- # => true
72
- response.status_message
73
- # => "It worked! :D"
74
-
75
- # If we have a falsy status method
76
- response = klass.response(status_method: 'falsey_method', success_message: "It worked! :D", failure_message: "It didn't work! D:")
77
- # => #<HollaBack::Response>
78
- response.successful?
79
- # => false
80
- response.status_message
81
- # => "It didn't work! D:"
82
- ```
83
+ # If we have a truthy status method
84
+ response = klass.response(status_method: 'truthy_method', success_message: "It worked! :D", failure_message: "It didn't work! D:")
85
+ # => #<HollaBack::Response>
86
+ response.successful?
87
+ # => true
88
+ response.status_message
89
+ # => "It worked! :D"
90
+
91
+ # If we have a falsy status method
92
+ response = klass.response(status_method: 'falsey_method', success_message: "It worked! :D", failure_message: "It didn't work! D:")
93
+ # => #<HollaBack::Response>
94
+ response.successful?
95
+ # => false
96
+ response.status_message
97
+ # => "It didn't work! D:"
98
+
83
99
  With extra methods you need information from
84
100
 
85
- ```ruby
86
- response = klass.response(status_method: 'falsey_method', responding_methods: [:optional_method, {:optional_method_with_params => ['some param']}])
87
- # => #<HollaBack::Response>
88
- response.responding_methods
89
- #=> {:optional_method=>"this was only optional", :optional_method_with_params=>true}
90
- ```
101
+ response = klass.response(status_method: 'falsey_method', responding_methods: [:optional_method, {:optional_method_with_params => ['some param']}])
102
+ # => #<HollaBack::Response>
103
+ response.responding_methods
104
+ #=> {:optional_method=>"this was only optional", :optional_method_with_params=>true}
91
105
 
92
106
  If you pass in a status method or some responding methods that raise exceptions, HollaBack will
93
107
  simply handle these gracefully, because exceptions are still a response from your code. The important
@@ -96,14 +110,26 @@ the exception on the status method, else you can check the responding object for
96
110
 
97
111
  With some method that totally doesn't exist
98
112
 
99
- ```ruby
100
- response = klass.response(status_method: 'some_method_that_totally_doesnt_exist')
101
- # => #<HollaBack::Response>
102
- response.responding_object
103
- #=> NoMethodError
104
- response.status_message
105
- #=> "undefined method `some_method_that_totally_doesnt_exist' for #<SomeClass:0x00000001e1b008>"
106
- ```
113
+ response = klass.response(status_method: 'some_method_that_totally_doesnt_exist')
114
+ # => #<HollaBack::Response>
115
+ response.responding_object
116
+ #=> NoMethodError
117
+ response.status_message
118
+ #=> "undefined method `some_method_that_totally_doesnt_exist' for #<SomeClass:0x00000001e1b008>"
119
+
120
+ ## Synopsis For Sending Methods
121
+
122
+ Synopsis for sending a method with parameters (the all important status method):
123
+
124
+ # This is how you'd do it for the status method
125
+ {:name_of_status_method => ['array', 'of', 'input', 'parameters']}
126
+
127
+ Synopsis for sending multiple methods, some requiring parameters:
128
+
129
+ # This is how you'd do it for the responding methods, which follows the same
130
+ # pattern as the status method, but as elements in an array so that you can
131
+ # respond with multiple responding methods
132
+ ['name_of_some_method_with_no_params', {:name_of_some_method_with_params => ['array', 'of', 'input', 'parameters']}]
107
133
 
108
134
 
109
135
  ## Contributing
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -6,10 +6,10 @@ Gem::Specification.new do |gem|
6
6
  gem.name = "holla_back"
7
7
  gem.version = `cat VERSION`.strip
8
8
  gem.authors = ["David Chapman"]
9
- gem.email = ["david@isotope11.com"]
9
+ gem.email = ["david@dchapman.io"]
10
10
  gem.description = %q{A simple Ruby gem for providing a response standard to your libraries}
11
11
  gem.summary = %q{HollaBack will simply provide a unified response object containing needed information and responses.}
12
- gem.homepage = ""
12
+ gem.homepage = "http://dchapman.io/"
13
13
  gem.license = "MIT"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
@@ -40,7 +40,7 @@ module HollaBack
40
40
  # @return [Boolean] false - if the status method is nil or false
41
41
  # @api public
42
42
  def successful?
43
- !!@responding_object.send(@status_method)
43
+ !!get_status_method
44
44
  end
45
45
 
46
46
  private
@@ -53,18 +53,36 @@ module HollaBack
53
53
  # @api private
54
54
  def set_response!
55
55
  get_responding_methods
56
+ self.responding_object = @responding_object
57
+ if successful?
58
+ self.status_message ||= (@success_message || "Status: successful")
59
+ else
60
+ self.status_message ||= (@failure_message || "Status: unsuccessful")
61
+ end
62
+ return true
63
+ end
64
+
65
+ # Gets the returning value of the status method
66
+ #
67
+ # @example
68
+ # response.get_status_method
69
+ #
70
+ # @return [Object] the returned object from the status method
71
+ # @api private
72
+ def get_status_method
73
+ status = false
56
74
  begin
57
- self.responding_object = @responding_object
58
- if successful?
59
- self.status_message = @success_message || "Status: successful"
75
+ if @status_method.is_a? Hash
76
+ meth = @status_method.first
77
+ status = !!@responding_object.send(meth.first, *meth.last)
60
78
  else
61
- self.status_message = @failure_message || "Status: unsuccessful"
79
+ status = !!@responding_object.send(@status_method)
62
80
  end
63
81
  rescue Exception => e
64
82
  self.responding_object = e.class
65
83
  self.status_message = e.message
66
84
  end
67
- return true
85
+ return status
68
86
  end
69
87
 
70
88
  # Gets the responding methods
@@ -78,17 +96,13 @@ module HollaBack
78
96
  @responding_methods.each do |meth|
79
97
  begin
80
98
  if meth.is_a? Hash
81
- meth.each_pair { |k,v|
82
- meth_responses[k.to_sym] = @responding_object.send(k, *v)
83
- }
99
+ meth.each_pair { |k,v| meth_responses[k.to_sym] = @responding_object.send(k, *v) }
84
100
  else
85
101
  meth_responses[meth.to_sym] = @responding_object.send(meth)
86
102
  end
87
103
  rescue Exception => e
88
104
  if meth.is_a? Hash
89
- meth.each_pair { |k,v|
90
- meth_responses[k.to_sym] = "#{e.class}: #{e.message}"
91
- }
105
+ meth.each_pair { |k,v| meth_responses[k.to_sym] = "#{e.class}: #{e.message}" }
92
106
  else
93
107
  meth_responses[meth.to_sym] = "#{e.class}: #{e.message}"
94
108
  end
@@ -33,6 +33,38 @@ describe HollaBack::Response do
33
33
  end
34
34
  end
35
35
 
36
+ describe "A successful response with params in the status method" do
37
+ before do
38
+ responding_object = mock('responding_object')
39
+ responding_object.expects(:status_meth_with_params).with('some param').returns([]).at_least_once
40
+ responding_object.expects(:meth1).returns(42).at_least_once
41
+ responding_object.expects(:meth2).with('param').returns(42).at_least_once
42
+ responding_methods = [:meth1, {:meth2 => ['param']}]
43
+ @options = {
44
+ responding_methods: responding_methods,
45
+ status_method: {:status_meth_with_params => ['some param']},
46
+ success_message: 'success',
47
+ failure_message: 'failure'
48
+ }
49
+ @response = HollaBack::Response.new(responding_object, @options)
50
+ end
51
+
52
+ it 'should provide a successful status method' do
53
+ @response.successful?.must_equal true
54
+ end
55
+
56
+ it 'should provide the responding methods' do
57
+ @response.responding_methods.must_be_kind_of Hash
58
+ @response.responding_methods[:meth1].must_equal 42
59
+ @response.responding_methods[:meth2].must_equal 42
60
+ end
61
+
62
+ it 'should return the success message' do
63
+ @response.status_message.must_be_kind_of String
64
+ @response.status_message.must_equal "success"
65
+ end
66
+ end
67
+
36
68
  describe "A unsuccessful response" do
37
69
  before do
38
70
  responding_object = mock('responding_object')
@@ -63,6 +95,67 @@ describe HollaBack::Response do
63
95
  end
64
96
  end
65
97
 
98
+ describe "A unsuccessful response with params in the status method" do
99
+ before do
100
+ responding_object = mock('responding_object')
101
+ responding_object.expects(:status_meth_with_params).with('some param').returns([false, nil].sample).at_least_once
102
+ responding_object.expects(:meth).returns(42).at_least_once
103
+ responding_methods = ['meth']
104
+ @options = {
105
+ responding_methods: responding_methods,
106
+ status_method: {:status_meth_with_params => ['some param']},
107
+ success_message: 'success',
108
+ failure_message: 'failure'
109
+ }
110
+ @response = HollaBack::Response.new(responding_object, @options)
111
+ end
112
+
113
+ it 'should provide a successful status method' do
114
+ @response.successful?.must_equal false
115
+ end
116
+
117
+ it 'should provide the responding methods' do
118
+ @response.responding_methods.must_be_kind_of Hash
119
+ @response.responding_methods[:meth].must_equal 42
120
+ end
121
+
122
+ it 'should return the success message' do
123
+ @response.status_message.must_be_kind_of String
124
+ @response.status_message.must_equal "failure"
125
+ end
126
+ end
127
+
128
+ describe "A unsuccessful response due to undefined status method with params" do
129
+ before do
130
+ responding_object = TestClass.new
131
+ responding_methods = ['meth']
132
+ @options = {
133
+ responding_methods: responding_methods,
134
+ status_method: {:status_meth_with_params => ['some param']},
135
+ success_message: 'success',
136
+ failure_message: 'failure'
137
+ }
138
+ @response = HollaBack::Response.new(@options)
139
+ end
140
+
141
+ it 'should provide a successful status method' do
142
+ @response.successful?.must_equal false
143
+ end
144
+
145
+ it 'should provide the responding methods' do
146
+ @response.responding_methods.must_be_kind_of Hash
147
+ end
148
+
149
+ it 'should return the exception class as the responding object' do
150
+ @response.responding_object.must_equal NoMethodError
151
+ end
152
+
153
+ it 'should provide the error message as the response' do
154
+ @response.status_message.must_be_kind_of String
155
+ end
156
+ end
157
+
158
+
66
159
  describe "A unsuccessful response due to a undefined methods" do
67
160
  before do
68
161
  responding_object = TestClass.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: holla_back
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Chapman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-27 00:00:00.000000000 Z
11
+ date: 2013-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -264,7 +264,7 @@ dependencies:
264
264
  version: '0'
265
265
  description: A simple Ruby gem for providing a response standard to your libraries
266
266
  email:
267
- - david@isotope11.com
267
+ - david@dchapman.io
268
268
  executables: []
269
269
  extensions: []
270
270
  extra_rdoc_files: []
@@ -286,7 +286,7 @@ files:
286
286
  - test/unit/holla_back/option_loader_test.rb
287
287
  - test/unit/holla_back/response_test.rb
288
288
  - test/unit/holla_back_test.rb
289
- homepage: ''
289
+ homepage: http://dchapman.io/
290
290
  licenses:
291
291
  - MIT
292
292
  metadata: {}