Class: CloudKit::Service

Inherits:
Object show all
Includes:
ResponseHelpers
Defined in:
lib/cloudkit/service.rb

Overview

A CloudKit Service is Rack middleware providing a REST/HTTP 1.1 interface to a Store. Its primary purpose is to initialize and adapt a Store for use in a Rack middleware stack.

Examples

A rackup file exposing items and things as REST collections:

require 'cloudkit'
expose :items, :things

The same as above, adding OpenID and OAuth/Discovery:

require 'cloudkit'
contain :items, :things

An explicit setup, without using the Rack::Builder shortcuts:

require 'cloudkit'
use Rack::Session::Pool
use CloudKit::OAuthFilter
use CloudKit::OpenIDFilter
use CloudKit::Service, :collections => [:items, :things]
run lambda{|env| [200, {'Content-Type' => 'text/html'}, ['Hello']]}

For more examples, including the use of different storage implementations, see the Table of Contents in the examples directory.

Constant Summary collapse

@@lock =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResponseHelpers

#allow, #data_required, #etag_required, #internal_server_error, #invalid_entity_type, #json_create_response, #json_error, #json_error_response, #json_meta_response, #json_metadata, #response, #status_404, #status_405, #status_410, #status_412, #status_422

Constructor Details

#initialize(app, options) ⇒ Service

Returns a new instance of Service.



34
35
36
37
# File 'lib/cloudkit/service.rb', line 34

def initialize(app, options)
  @app         = app
  @collections = options[:collections]
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



32
33
34
# File 'lib/cloudkit/service.rb', line 32

def store
  @store
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudkit/service.rb', line 39

def call(env)
  @@lock.synchronize do
    @store = Store.new(:collections => @collections)
  end unless @store

  request = Request.new(env)
  unless bypass?(request)
    return auth_config_error if (request.using_auth? && auth_missing?(request))
    return not_implemented unless @store.implements?(request.request_method)
    send(request.request_method.downcase, request) rescue internal_server_error
  else
    @app.call(env)
  end
end