Module: CloudKit::ResponseHelpers
Overview
A set of mixins for building CloudKit::Response objects.
Instance Method Summary collapse
- #allow(methods) ⇒ Object
- #data_required ⇒ Object
- #etag_required ⇒ Object
- #internal_server_error ⇒ Object
- #invalid_entity_type ⇒ Object
- #json_create_response(uri, etag, last_modified) ⇒ Object
- #json_error(message) ⇒ Object
- #json_error_response(status, message) ⇒ Object
- #json_meta_response(uri, etag, last_modified) ⇒ Object
- #json_metadata(uri, etag, last_modified) ⇒ Object
- #response(status, content = '', etag = nil, last_modified = nil, options = {}) ⇒ Object
- #status_404 ⇒ Object
- #status_405(methods) ⇒ Object
- #status_410 ⇒ Object
- #status_412 ⇒ Object
- #status_422 ⇒ Object
Instance Method Details
#allow(methods) ⇒ Object
41 42 43 44 45 |
# File 'lib/cloudkit/store/response_helpers.rb', line 41 def allow(methods) CloudKit::Response.new( 200, {'Allow' => methods.join(', '), 'Content-Type' => 'application/json'}) end |
#data_required ⇒ Object
29 30 31 |
# File 'lib/cloudkit/store/response_helpers.rb', line 29 def data_required json_error_response(400, 'data required') end |
#etag_required ⇒ Object
37 38 39 |
# File 'lib/cloudkit/store/response_helpers.rb', line 37 def etag_required json_error_response(400, 'etag required') end |
#internal_server_error ⇒ Object
25 26 27 |
# File 'lib/cloudkit/store/response_helpers.rb', line 25 def internal_server_error json_error_response(500, 'unknown server error') end |
#invalid_entity_type ⇒ Object
33 34 35 |
# File 'lib/cloudkit/store/response_helpers.rb', line 33 def invalid_entity_type json_error_response(400, 'valid entity type required') end |
#json_create_response(uri, etag, last_modified) ⇒ Object
64 65 66 67 |
# File 'lib/cloudkit/store/response_helpers.rb', line 64 def json_create_response(uri, etag, last_modified) json = (uri, etag, last_modified) response(201, json, nil, nil, {:cache => false, :location => uri}) end |
#json_error(message) ⇒ Object
77 78 79 |
# File 'lib/cloudkit/store/response_helpers.rb', line 77 def json_error() "{\"error\":\"#{}\"}" end |
#json_error_response(status, message) ⇒ Object
81 82 83 |
# File 'lib/cloudkit/store/response_helpers.rb', line 81 def json_error_response(status, ) response(status, json_error(), nil, nil, :cache => false) end |
#json_meta_response(uri, etag, last_modified) ⇒ Object
59 60 61 62 |
# File 'lib/cloudkit/store/response_helpers.rb', line 59 def (uri, etag, last_modified) json = (uri, etag, last_modified) response(200, json, nil, nil, :cache => false) end |
#json_metadata(uri, etag, last_modified) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/cloudkit/store/response_helpers.rb', line 69 def (uri, etag, last_modified) JSON.generate( :ok => true, :uri => uri, :etag => etag, :last_modified => last_modified) end |
#response(status, content = '', etag = nil, last_modified = nil, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cloudkit/store/response_helpers.rb', line 47 def response(status, content='', etag=nil, last_modified=nil, ={}) cache_control = [:cache] == false ? 'no-cache' : 'proxy-revalidate' etag = "\"#{etag}\"" if etag headers = {}.filter_merge!( 'Content-Type' => 'application/json', 'Cache-Control' => cache_control, 'Last-Modified' => last_modified, 'Location' => [:location], 'ETag' => etag) CloudKit::Response.new(status, headers, content) end |
#status_404 ⇒ Object
3 4 5 |
# File 'lib/cloudkit/store/response_helpers.rb', line 3 def status_404 json_error_response(404, 'not found') end |
#status_405(methods) ⇒ Object
7 8 9 10 11 |
# File 'lib/cloudkit/store/response_helpers.rb', line 7 def status_405(methods) response = json_error_response(405, 'method not allowed') response['Allow'] = methods.join(', ') response end |
#status_410 ⇒ Object
13 14 15 |
# File 'lib/cloudkit/store/response_helpers.rb', line 13 def status_410 json_error_response(410, 'entity previously deleted') end |
#status_412 ⇒ Object
17 18 19 |
# File 'lib/cloudkit/store/response_helpers.rb', line 17 def status_412 json_error_response(412, 'precondition failed') end |
#status_422 ⇒ Object
21 22 23 |
# File 'lib/cloudkit/store/response_helpers.rb', line 21 def status_422 json_error_response(422, 'unprocessable entity') end |