Module: ApiHammer::HaltMethods

Included in:
Rails, Sinatra::Halt
Defined in:
lib/api_hammer/halt_methods.rb

Instance Method Summary collapse

Instance Method Details

#find_or_halt(model, find_attrs, options = {}) ⇒ Object

attempts to find and return the given model (an ActiveRecord::Base subclass) with the given attributes. halts with 404 (does not return) if that fails. options[:status] may specify a different status if that is required.

e.g.:

find_or_halt(User, :email => '[email protected]')


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/api_hammer/halt_methods.rb', line 48

def find_or_halt(model, find_attrs, options={})
  options = {:status => 404}.merge(options)
  record = model.where(find_attrs).first
  unless record
    attributes = find_attrs.map { |attr, val| "#{attr}: #{val}" }.join(", ")
    model_name = model.table_name
    model_name = model_name.singularize if model_name.respond_to?(:singularize)
    message = I18n.t(:"errors.unknown_record_with_attributes", :default => "Unknown %{model_name}! %{attributes}",
      :model_name => model_name,
      :attributes => attributes
    )
    halt_error(options[:status], {model_name => [message]})
  end
  record
end

#halt_accepted(body, render_options = {}) ⇒ Object

halt with status 202 Accepted, responding with the given body object



75
76
77
# File 'lib/api_hammer/halt_methods.rb', line 75

def halt_accepted(body, render_options = {})
  halt(202, body, render_options)
end

#halt_already_reported(body, render_options = {}) ⇒ Object

halt with status 208 Already Reported, responding with the given body object



105
106
107
# File 'lib/api_hammer/halt_methods.rb', line 105

def halt_already_reported(body, render_options = {})
  halt(208, body, render_options)
end

#halt_authentication_timeout(errors, render_options = {}) ⇒ Object

halt with status 419 Authentication Timeout, responding with the given errors object on the 'errors' key



250
251
252
# File 'lib/api_hammer/halt_methods.rb', line 250

def halt_authentication_timeout(errors, render_options = {})
  halt_error(419, errors, render_options)
end

#halt_bad_gateway(errors, render_options = {}) ⇒ Object

halt with status 502 Bad Gateway, responding with the given errors object on the 'errors' key



320
321
322
# File 'lib/api_hammer/halt_methods.rb', line 320

def halt_bad_gateway(errors, render_options = {})
  halt_error(502, errors, render_options)
end

#halt_bad_request(errors, render_options = {}) ⇒ Object

halt with status 400 Bad Request, responding with the given errors object on the 'errors' key



155
156
157
# File 'lib/api_hammer/halt_methods.rb', line 155

def halt_bad_request(errors, render_options = {})
  halt_error(400, errors, render_options)
end

#halt_bandwidth_limit_exceeded(errors, render_options = {}) ⇒ Object

halt with status 509 Bandwidth Limit Exceeded, responding with the given errors object on the 'errors' key



355
356
357
# File 'lib/api_hammer/halt_methods.rb', line 355

def halt_bandwidth_limit_exceeded(errors, render_options = {})
  halt_error(509, errors, render_options)
end

#halt_conflict(errors, render_options = {}) ⇒ Object

halt with status 409 Conflict, responding with the given errors object on the 'errors' key



200
201
202
# File 'lib/api_hammer/halt_methods.rb', line 200

def halt_conflict(errors, render_options = {})
  halt_error(409, errors, render_options)
end

#halt_created(body, render_options = {}) ⇒ Object

halt with status 201 Created, responding with the given body object



70
71
72
# File 'lib/api_hammer/halt_methods.rb', line 70

def halt_created(body, render_options = {})
  halt(201, body, render_options)
end

#halt_error(status, errors, options = {}) ⇒ Object

halt and render the given errors in the body on the 'errors' key



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_hammer/halt_methods.rb', line 4

def halt_error(status, errors, options = {})
  errors_as_json = errors.respond_to?(:as_json) ? errors.as_json : errors
  unless errors_as_json.is_a?(Hash)
    raise ArgumentError, "errors be an object representable in JSON as a Hash; got errors = #{errors.inspect}"
  end
  unless errors_as_json.keys.all? { |k| k.is_a?(String) || k.is_a?(Symbol) }
    raise ArgumentError, "errors keys must all be string or symbol; got errors = #{errors.inspect}"
  end
  unless errors_as_json.values.all? { |v| v.is_a?(Array) && v.all? { |e| e.is_a?(String) } }
    raise ArgumentError, "errors values must all be arrays of strings; got errors = #{errors.inspect}"
  end
  error_message = nil
  halt_options = options.reject do |k,v|
    (k.to_s == 'error_message').tap do |is_error_message|
      if is_error_message
        error_message = v
      end
    end
  end
  body = {'errors' => errors}
  error_message ||= begin
    error_values = errors.values.inject([], &:+)
    if error_values.size <= 1
      error_values.first
    else
      # sentencify with periods 
      error_values.map { |v| v =~ /\.\s*\z/ ? v : v + '.' }.join(' ')
    end
  end
  body['error_message'] = error_message if error_message
  if Object.const_defined?(:Rollbar) and status != 404 and Object.const_defined?(:DEBUG_4XX) and DEBUG_4XX['enabled']
    Rollbar.debug "Service halted with status #{status}", status: status, body: body, halt_options: halt_options
  end
  halt(status, body, halt_options)
end

#halt_expectation_failed(errors, render_options = {}) ⇒ Object

halt with status 417 Expectation Failed, responding with the given errors object on the 'errors' key



240
241
242
# File 'lib/api_hammer/halt_methods.rb', line 240

def halt_expectation_failed(errors, render_options = {})
  halt_error(417, errors, render_options)
end

#halt_failed_dependency(errors, render_options = {}) ⇒ Object

halt with status 424 Failed Dependency, responding with the given errors object on the 'errors' key



265
266
267
# File 'lib/api_hammer/halt_methods.rb', line 265

def halt_failed_dependency(errors, render_options = {})
  halt_error(424, errors, render_options)
end

#halt_forbidden(errors, render_options = {}) ⇒ Object

halt with status 403 Forbidden, responding with the given errors object on the 'errors' key



170
171
172
# File 'lib/api_hammer/halt_methods.rb', line 170

def halt_forbidden(errors, render_options = {})
  halt_error(403, errors, render_options)
end

#halt_found(body, render_options = {}) ⇒ Object

halt with status 302 Found, responding with the given body object



125
126
127
# File 'lib/api_hammer/halt_methods.rb', line 125

def halt_found(body, render_options = {})
  halt(302, body, render_options)
end

#halt_gateway_timeout(errors, render_options = {}) ⇒ Object

halt with status 504 Gateway Timeout, responding with the given errors object on the 'errors' key



330
331
332
# File 'lib/api_hammer/halt_methods.rb', line 330

def halt_gateway_timeout(errors, render_options = {})
  halt_error(504, errors, render_options)
end

#halt_gone(errors, render_options = {}) ⇒ Object

halt with status 410 Gone, responding with the given errors object on the 'errors' key



205
206
207
# File 'lib/api_hammer/halt_methods.rb', line 205

def halt_gone(errors, render_options = {})
  halt_error(410, errors, render_options)
end

#halt_http_version_not_supported(errors, render_options = {}) ⇒ Object

halt with status 505 HTTP Version Not Supported, responding with the given errors object on the 'errors' key



335
336
337
# File 'lib/api_hammer/halt_methods.rb', line 335

def halt_http_version_not_supported(errors, render_options = {})
  halt_error(505, errors, render_options)
end

#halt_im_a_teapot(errors, render_options = {}) ⇒ Object

halt with status 418 I'm a teapot, responding with the given errors object on the 'errors' key



245
246
247
# File 'lib/api_hammer/halt_methods.rb', line 245

def halt_im_a_teapot(errors, render_options = {})
  halt_error(418, errors, render_options)
end

#halt_im_used(body, render_options = {}) ⇒ Object

halt with status 226 IM Used, responding with the given body object



110
111
112
# File 'lib/api_hammer/halt_methods.rb', line 110

def halt_im_used(body, render_options = {})
  halt(226, body, render_options)
end

#halt_insufficient_storage(errors, render_options = {}) ⇒ Object

halt with status 507 Insufficient Storage, responding with the given errors object on the 'errors' key



345
346
347
# File 'lib/api_hammer/halt_methods.rb', line 345

def halt_insufficient_storage(errors, render_options = {})
  halt_error(507, errors, render_options)
end

#halt_internal_server_error(errors, render_options = {}) ⇒ Object

halt with status 500 Internal Server Error, responding with the given errors object on the 'errors' key



310
311
312
# File 'lib/api_hammer/halt_methods.rb', line 310

def halt_internal_server_error(errors, render_options = {})
  halt_error(500, errors, render_options)
end

#halt_length_required(errors, render_options = {}) ⇒ Object

halt with status 411 Length Required, responding with the given errors object on the 'errors' key



210
211
212
# File 'lib/api_hammer/halt_methods.rb', line 210

def halt_length_required(errors, render_options = {})
  halt_error(411, errors, render_options)
end

#halt_locked(errors, render_options = {}) ⇒ Object

halt with status 423 Locked, responding with the given errors object on the 'errors' key



260
261
262
# File 'lib/api_hammer/halt_methods.rb', line 260

def halt_locked(errors, render_options = {})
  halt_error(423, errors, render_options)
end

#halt_loop_detected(errors, render_options = {}) ⇒ Object

halt with status 508 Loop Detected, responding with the given errors object on the 'errors' key



350
351
352
# File 'lib/api_hammer/halt_methods.rb', line 350

def halt_loop_detected(errors, render_options = {})
  halt_error(508, errors, render_options)
end

#halt_method_not_allowed(errors, render_options = {}) ⇒ Object

halt with status 405 Method Not Allowed, responding with the given errors object on the 'errors' key



180
181
182
# File 'lib/api_hammer/halt_methods.rb', line 180

def halt_method_not_allowed(errors, render_options = {})
  halt_error(405, errors, render_options)
end

#halt_moved_permanently(body, render_options = {}) ⇒ Object

halt with status 301 Moved Permanently, responding with the given body object



120
121
122
# File 'lib/api_hammer/halt_methods.rb', line 120

def halt_moved_permanently(body, render_options = {})
  halt(301, body, render_options)
end

#halt_multi_status(body, render_options = {}) ⇒ Object

halt with status 207 Multi-Status, responding with the given body object



100
101
102
# File 'lib/api_hammer/halt_methods.rb', line 100

def halt_multi_status(body, render_options = {})
  halt(207, body, render_options)
end

#halt_multiple_choices(body, render_options = {}) ⇒ Object

halt with status 300 Multiple Choices, responding with the given body object



115
116
117
# File 'lib/api_hammer/halt_methods.rb', line 115

def halt_multiple_choices(body, render_options = {})
  halt(300, body, render_options)
end

#halt_network_authentication_required(errors, render_options = {}) ⇒ Object

halt with status 511 Network Authentication Required, responding with the given errors object on the 'errors' key



365
366
367
# File 'lib/api_hammer/halt_methods.rb', line 365

def halt_network_authentication_required(errors, render_options = {})
  halt_error(511, errors, render_options)
end

#halt_no_content(render_options = {}) ⇒ Object

halt with status 204 No Content



85
86
87
# File 'lib/api_hammer/halt_methods.rb', line 85

def halt_no_content(render_options = {})
  halt(204, '', render_options)
end

#halt_no_response(errors, render_options = {}) ⇒ Object

halt with status 444 No Response, responding with the given errors object on the 'errors' key



295
296
297
# File 'lib/api_hammer/halt_methods.rb', line 295

def halt_no_response(errors, render_options = {})
  halt_error(444, errors, render_options)
end

#halt_non_authoritative_information(body, render_options = {}) ⇒ Object

halt with status 203 Non-Authoritative Information, responding with the given body object



80
81
82
# File 'lib/api_hammer/halt_methods.rb', line 80

def halt_non_authoritative_information(body, render_options = {})
  halt(203, body, render_options)
end

#halt_not_acceptable(errors, render_options = {}) ⇒ Object

halt with status 406 Not Acceptable, responding with the given errors object on the 'errors' key



185
186
187
# File 'lib/api_hammer/halt_methods.rb', line 185

def halt_not_acceptable(errors, render_options = {})
  halt_error(406, errors, render_options)
end

#halt_not_extended(errors, render_options = {}) ⇒ Object

halt with status 510 Not Extended, responding with the given errors object on the 'errors' key



360
361
362
# File 'lib/api_hammer/halt_methods.rb', line 360

def halt_not_extended(errors, render_options = {})
  halt_error(510, errors, render_options)
end

#halt_not_found(errors, render_options = {}) ⇒ Object

halt with status 404 Not Found, responding with the given errors object on the 'errors' key



175
176
177
# File 'lib/api_hammer/halt_methods.rb', line 175

def halt_not_found(errors, render_options = {})
  halt_error(404, errors, render_options)
end

#halt_not_implemented(errors, render_options = {}) ⇒ Object

halt with status 501 Not Implemented, responding with the given errors object on the 'errors' key



315
316
317
# File 'lib/api_hammer/halt_methods.rb', line 315

def halt_not_implemented(errors, render_options = {})
  halt_error(501, errors, render_options)
end

#halt_not_modified(render_options = {}) ⇒ Object

halt with status 304 Not Modified



135
136
137
# File 'lib/api_hammer/halt_methods.rb', line 135

def halt_not_modified(render_options = {})
  halt(304, '', render_options)
end

#halt_ok(body, render_options = {}) ⇒ Object

halt with status 200 OK, responding with the given body object



65
66
67
# File 'lib/api_hammer/halt_methods.rb', line 65

def halt_ok(body, render_options = {})
  halt(200, body, render_options)
end

#halt_partial_content(body, render_options = {}) ⇒ Object

halt with status 206 Partial Content, responding with the given body object



95
96
97
# File 'lib/api_hammer/halt_methods.rb', line 95

def halt_partial_content(body, render_options = {})
  halt(206, body, render_options)
end

#halt_payment_required(errors, render_options = {}) ⇒ Object

halt with status 402 Payment Required, responding with the given errors object on the 'errors' key



165
166
167
# File 'lib/api_hammer/halt_methods.rb', line 165

def halt_payment_required(errors, render_options = {})
  halt_error(402, errors, render_options)
end

#halt_permanent_redirect(body, render_options = {}) ⇒ Object

halt with status 308 Permanent Redirect, responding with the given body object



150
151
152
# File 'lib/api_hammer/halt_methods.rb', line 150

def halt_permanent_redirect(body, render_options = {})
  halt(308, body, render_options)
end

#halt_precondition_failed(errors, render_options = {}) ⇒ Object

halt with status 412 Precondition Failed, responding with the given errors object on the 'errors' key



215
216
217
# File 'lib/api_hammer/halt_methods.rb', line 215

def halt_precondition_failed(errors, render_options = {})
  halt_error(412, errors, render_options)
end

#halt_precondition_required(errors, render_options = {}) ⇒ Object

halt with status 428 Precondition Required, responding with the given errors object on the 'errors' key



280
281
282
# File 'lib/api_hammer/halt_methods.rb', line 280

def halt_precondition_required(errors, render_options = {})
  halt_error(428, errors, render_options)
end

#halt_proxy_authentication_required(errors, render_options = {}) ⇒ Object

halt with status 407 Proxy Authentication Required, responding with the given errors object on the 'errors' key



190
191
192
# File 'lib/api_hammer/halt_methods.rb', line 190

def halt_proxy_authentication_required(errors, render_options = {})
  halt_error(407, errors, render_options)
end

#halt_redirect(errors, render_options = {}) ⇒ Object

halt with status 451 Redirect, responding with the given errors object on the 'errors' key



305
306
307
# File 'lib/api_hammer/halt_methods.rb', line 305

def halt_redirect(errors, render_options = {})
  halt_error(451, errors, render_options)
end

#halt_request_entity_too_large(errors, render_options = {}) ⇒ Object

halt with status 413 Request Entity Too Large, responding with the given errors object on the 'errors' key



220
221
222
# File 'lib/api_hammer/halt_methods.rb', line 220

def halt_request_entity_too_large(errors, render_options = {})
  halt_error(413, errors, render_options)
end

#halt_request_header_fields_too_large(errors, render_options = {}) ⇒ Object

halt with status 431 Request Header Fields Too Large, responding with the given errors object on the 'errors' key



290
291
292
# File 'lib/api_hammer/halt_methods.rb', line 290

def halt_request_header_fields_too_large(errors, render_options = {})
  halt_error(431, errors, render_options)
end

#halt_request_timeout(errors, render_options = {}) ⇒ Object

halt with status 408 Request Timeout, responding with the given errors object on the 'errors' key



195
196
197
# File 'lib/api_hammer/halt_methods.rb', line 195

def halt_request_timeout(errors, render_options = {})
  halt_error(408, errors, render_options)
end

#halt_request_uri_too_long(errors, render_options = {}) ⇒ Object

halt with status 414 Request-URI Too Long, responding with the given errors object on the 'errors' key



225
226
227
# File 'lib/api_hammer/halt_methods.rb', line 225

def halt_request_uri_too_long(errors, render_options = {})
  halt_error(414, errors, render_options)
end

#halt_requested_range_not_satisfiable(errors, render_options = {}) ⇒ Object

halt with status 416 Requested Range Not Satisfiable, responding with the given errors object on the 'errors' key



235
236
237
# File 'lib/api_hammer/halt_methods.rb', line 235

def halt_requested_range_not_satisfiable(errors, render_options = {})
  halt_error(416, errors, render_options)
end

#halt_reset_content(render_options = {}) ⇒ Object

halt with status 205 Reset Content



90
91
92
# File 'lib/api_hammer/halt_methods.rb', line 90

def halt_reset_content(render_options = {})
  halt(205, '', render_options)
end

#halt_see_other(body, render_options = {}) ⇒ Object

halt with status 303 See Other, responding with the given body object



130
131
132
# File 'lib/api_hammer/halt_methods.rb', line 130

def halt_see_other(body, render_options = {})
  halt(303, body, render_options)
end

#halt_service_unavailable(errors, render_options = {}) ⇒ Object

halt with status 503 Service Unavailable, responding with the given errors object on the 'errors' key



325
326
327
# File 'lib/api_hammer/halt_methods.rb', line 325

def halt_service_unavailable(errors, render_options = {})
  halt_error(503, errors, render_options)
end

#halt_temporary_redirect(body, render_options = {}) ⇒ Object

halt with status 307 Temporary Redirect, responding with the given body object



145
146
147
# File 'lib/api_hammer/halt_methods.rb', line 145

def halt_temporary_redirect(body, render_options = {})
  halt(307, body, render_options)
end

#halt_too_many_requests(errors, render_options = {}) ⇒ Object

halt with status 429 Too Many Requests, responding with the given errors object on the 'errors' key



285
286
287
# File 'lib/api_hammer/halt_methods.rb', line 285

def halt_too_many_requests(errors, render_options = {})
  halt_error(429, errors, render_options)
end

#halt_unauthorized(errors, render_options = {}) ⇒ Object

halt with status 401 Unauthorized, responding with the given errors object on the 'errors' key



160
161
162
# File 'lib/api_hammer/halt_methods.rb', line 160

def halt_unauthorized(errors, render_options = {})
  halt_error(401, errors, render_options)
end

halt with status 451 Unavailable For Legal Reasons, responding with the given errors object on the 'errors' key



300
301
302
# File 'lib/api_hammer/halt_methods.rb', line 300

def halt_unavailable_for_legal_reasons(errors, render_options = {})
  halt_error(451, errors, render_options)
end

#halt_unordered_collection(errors, render_options = {}) ⇒ Object

halt with status 425 Unordered Collection, responding with the given errors object on the 'errors' key



270
271
272
# File 'lib/api_hammer/halt_methods.rb', line 270

def halt_unordered_collection(errors, render_options = {})
  halt_error(425, errors, render_options)
end

#halt_unprocessable_entity(errors, render_options = {}) ⇒ Object

halt with status 422 Unprocessable Entity, responding with the given errors object on the 'errors' key



255
256
257
# File 'lib/api_hammer/halt_methods.rb', line 255

def halt_unprocessable_entity(errors, render_options = {})
  halt_error(422, errors, render_options)
end

#halt_unsupported_media_type(errors, render_options = {}) ⇒ Object

halt with status 415 Unsupported Media Type, responding with the given errors object on the 'errors' key



230
231
232
# File 'lib/api_hammer/halt_methods.rb', line 230

def halt_unsupported_media_type(errors, render_options = {})
  halt_error(415, errors, render_options)
end

#halt_upgrade_required(errors, render_options = {}) ⇒ Object

halt with status 426 Upgrade Required, responding with the given errors object on the 'errors' key



275
276
277
# File 'lib/api_hammer/halt_methods.rb', line 275

def halt_upgrade_required(errors, render_options = {})
  halt_error(426, errors, render_options)
end

#halt_use_proxy(body, render_options = {}) ⇒ Object

halt with status 305 Use Proxy, responding with the given body object



140
141
142
# File 'lib/api_hammer/halt_methods.rb', line 140

def halt_use_proxy(body, render_options = {})
  halt(305, body, render_options)
end

#halt_variant_also_negotiates(errors, render_options = {}) ⇒ Object

halt with status 506 Variant Also Negotiates, responding with the given errors object on the 'errors' key



340
341
342
# File 'lib/api_hammer/halt_methods.rb', line 340

def halt_variant_also_negotiates(errors, render_options = {})
  halt_error(506, errors, render_options)
end