nifty_services 0.0.2 → 0.0.3

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: fbc637a70ce76172e4046bea262d1e5dc441812e
4
- data.tar.gz: b9eab049883acf9e7326016148867c0c2a8d0955
3
+ metadata.gz: 6ecb01584fa196dbc0885c27d09a913dcad6cb78
4
+ data.tar.gz: 80eac1cc0ca04a6f16fafd9cc9eff9e29d0079bd
5
5
  SHA512:
6
- metadata.gz: e7418c345da76821b3d923f4713daad3bad32b3c99ba5e2e1f86984082d97c7e0a1e5b951d3c008a6cc27b6819adafa44198eaac48b8d6e4478b5889b531be7d
7
- data.tar.gz: aada69447972ebce79dad95e7e2251907aae2af998973c96040b1c19e4607b5a108e60d49c72810b3e8e3242332d96da812dd5e38bcd54305d5cb558214d1703
6
+ metadata.gz: 18e82bfb9c763ffdfb00ae3a5a1908a5991b63f9d73bea81693f61a24680036db456db242a582b2cdf1253199cd741377118bf6ff88ba80ce0a1bef4dca3f944
7
+ data.tar.gz: 174cc9ab744aa6dc8d4cf7dbfca1e6b28ecaf14602172ce5991c6d38142308106c9539adeb4316fe15b1e8bbe6dc40bc02d9d1b07fe67edf6b921bd2600d42fc
@@ -61,15 +61,6 @@ module NiftyServices
61
61
  alias :record_safe_params :record_allowed_params
62
62
 
63
63
  private
64
-
65
- def valid_user?
66
- user_class = NiftyServices.config.user_class
67
-
68
- raise 'Invalid User class. Use NitfyService.config.user_class = ClassName' if user_class.blank?
69
-
70
- valid_object?(@user, user_class)
71
- end
72
-
73
64
  def array_values_from_hash(options, key, root = nil)
74
65
  options = options.symbolize_keys
75
66
 
@@ -1,7 +1,7 @@
1
1
  module NiftyServices
2
2
  class BaseService
3
3
 
4
- attr_reader :options, :response_status, :errors
4
+ attr_reader :options, :response_status, :response_status_code, :errors
5
5
 
6
6
  CALLBACKS = [
7
7
  :after_initialize,
@@ -60,9 +60,9 @@ module NiftyServices
60
60
 
61
61
  def initialize(options = {}, initial_response_status = 400)
62
62
  @options = options.to_options!
63
- @response_status = initial_response_status
64
63
  @errors = []
65
64
 
65
+ set_response_status(initial_response_status)
66
66
  initial_callbacks_setup
67
67
 
68
68
  call_callback(:after_initialize)
@@ -81,13 +81,21 @@ module NiftyServices
81
81
  end
82
82
 
83
83
  def response_status
84
- @response_status ||= 400
84
+ @response_status ||= :bad_request
85
85
  end
86
86
 
87
87
  def changed?
88
88
  changed_attributes.any?
89
89
  end
90
90
 
91
+ def valid_user?
92
+ user_class = NiftyServices.config.user_class
93
+
94
+ raise 'Invalid User class. Use NitfyService.config.user_class = ClassName' if user_class.blank?
95
+
96
+ valid_object?(@user, user_class)
97
+ end
98
+
91
99
  def callback_fired?(callback_name)
92
100
  return (
93
101
  callback_fired_in?(@fired_callbacks, callback_name) ||
@@ -133,22 +141,54 @@ module NiftyServices
133
141
  @registered_callbacks = Hash.new {|k,v| k[v] = [] }
134
142
  end
135
143
 
136
- def success_response(status = 200)
137
- call_callback(:before_success)
138
-
139
- @response_status = status
140
- @success = true
144
+ def success_response(status = :ok)
145
+ unless Configuration::SUCCESS_RESPONSE_STATUS.key?(status.to_sym)
146
+ raise "#{status} is not a valid success response status"
147
+ end
141
148
 
142
- call_callback(:after_success)
149
+ with_before_and_after_callbacks(:success) do
150
+ @success = true
151
+ set_response_status(status)
152
+ end
143
153
  end
144
154
 
145
155
  def success_created_response
146
- success_response(201)
156
+ success_response(:created)
157
+ end
158
+
159
+ def set_response_status(status)
160
+ @response_status = response_status_reason_for(status)
161
+ @response_status_code = response_status_code_for(status)
162
+ end
163
+
164
+ def response_status_for(status)
165
+ error_list = Configuration::ERROR_RESPONSE_STATUS
166
+ success_list = Configuration::SUCCESS_RESPONSE_STATUS
167
+
168
+ select_method = [Symbol, String].member?(status.class) ? :key : :value
169
+
170
+ response_list = error_list.merge(success_list)
171
+
172
+ selected_status = response_list.select do |status_key, status_code|
173
+ if select_method == :key
174
+ status_key == status
175
+ else
176
+ status_code == status
177
+ end
178
+ end
179
+ end
180
+
181
+ def response_status_code_for(status)
182
+ response_status_for(status).values.first
183
+ end
184
+
185
+ def response_status_reason_for(status)
186
+ response_status_for(status).keys.first
147
187
  end
148
188
 
149
- def error(status, message_key, options={})
189
+ def error(status, message_key, options = {})
150
190
  with_before_and_after_callbacks(:error) do
151
- @response_status = status
191
+ set_response_status(status)
152
192
  error_message = process_error_message_for_key(message_key, options)
153
193
 
154
194
  add_error(error_message)
@@ -6,7 +6,7 @@ module NiftyServices
6
6
  DEFAULT_I18N_NAMESPACE = "nifty_services"
7
7
  DEFAULT_SERVICE_CONCERN_NAMESPACE = 'NitfyServices::Concerns'
8
8
 
9
- RESPONSE_ERRORS_LIST = {
9
+ ERROR_RESPONSE_STATUS = {
10
10
  :bad_request => 400,
11
11
  :not_authorized => 401,
12
12
  :forbidden => 403,
@@ -16,9 +16,14 @@ module NiftyServices
16
16
  :not_implemented => 501
17
17
  }
18
18
 
19
+ SUCCESS_RESPONSE_STATUS = {
20
+ :ok => 200,
21
+ :created => 201
22
+ }
23
+
19
24
  class << self
20
25
  def response_errors_list
21
- RESPONSE_ERRORS_LIST
26
+ ERROR_RESPONSE_STATUS
22
27
  end
23
28
 
24
29
  def add_response_error_method(reason, status_code)
@@ -1,3 +1,3 @@
1
1
  module NiftyServices
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty_services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Fidelis