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_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(message)
  "{\"error\":\"#{message}\"}"
end

#json_error_response(status, message) ⇒ Object



81
82
83
# File 'lib/cloudkit/store/response_helpers.rb', line 81

def json_error_response(status, message)
  response(status, json_error(message), 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 json_meta_response(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, options={})
  cache_control = options[: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'      => options[:location],
    'ETag'          => etag)
  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