Module: CloudKit::ResponseHelpers

Included in:
Service, Store
Defined in:
lib/cloudkit/store/response_helpers.rb

Overview

A set of mixins for building CloudKit::Response objects.

Instance Method Summary collapse

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_requiredObject



29
30
31
# File 'lib/cloudkit/store/response_helpers.rb', line 29

def data_required
  json_error_response(400, 'data required')
end

#etag_requiredObject



37
38
39
# File 'lib/cloudkit/store/response_helpers.rb', line 37

def etag_required
  json_error_response(400, 'etag required')
end

#internal_server_errorObject



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_typeObject



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_error(message) ⇒ Object



66
67
68
# File 'lib/cloudkit/store/response_helpers.rb', line 66

def json_error(message)
  "{\"error\":\"#{message}\"}"
end

#json_error_response(status, message) ⇒ Object



70
71
72
73
# File 'lib/cloudkit/store/response_helpers.rb', line 70

def json_error_response(status, message)
  "trying to throw a json error message for #{status} #{message}"
  response(status, json_error(message), nil, nil, :cache => false)
end

#json_meta_response(status, uri, etag, last_modified) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/cloudkit/store/response_helpers.rb', line 57

def json_meta_response(status, uri, etag, last_modified)
  json = JSON.generate(
    :ok            => true,
    :uri           => uri,
    :etag          => etag,
    :last_modified => last_modified)
  response(status, json, nil, nil, :cache => false)
end

#response(status, content = '', etag = nil, last_modified = nil, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/cloudkit/store/response_helpers.rb', line 47

def response(status, content='', etag=nil, last_modified=nil, options={})
  cache_control = options[:cache] == false ? 'no-cache' : 'proxy-revalidate'
  headers = {
    'Content-Type'  => 'application/json',
    'Cache-Control' =>  cache_control}
  headers.merge!('ETag' => "\"#{etag}\"") if etag
  headers.merge!('Last-Modified' => last_modified) if last_modified
  CloudKit::Response.new(status, headers, content)
end

#status_404Object



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_410Object



13
14
15
# File 'lib/cloudkit/store/response_helpers.rb', line 13

def status_410
  json_error_response(410, 'entity previously deleted')
end

#status_412Object



17
18
19
# File 'lib/cloudkit/store/response_helpers.rb', line 17

def status_412
  json_error_response(412, 'precondition failed')
end

#status_422Object



21
22
23
# File 'lib/cloudkit/store/response_helpers.rb', line 21

def status_422
  json_error_response(422, 'unprocessable entity')
end