Class: Capsium::Reactor::DataApi

Inherits:
Object
  • Object
show all
Includes:
Responses
Defined in:
lib/capsium/reactor/data_api.rb,
sig/capsium/reactor/data_api.rbs

Overview

REST CRUD over a mount's datasets (ARCHITECTURE.md sections 5 and 5a): JSON item append/read/replace/delete against the overlay-merged collection.

Constant Summary collapse

PREFIX =

Returns:

  • (String)
"/api/v1/data/"
COLLECTION_PATTERN =

Returns:

  • (Regexp)
%r{\A/api/v1/data/(?<dataset>[^/]+)\z}
ITEM_PATTERN =

Returns:

  • (Regexp)
%r{\A/api/v1/data/(?<dataset>[^/]+)/(?<id>[^/]+)\z}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Responses

#respond_error, #respond_forbidden, #respond_json, #respond_method_not_allowed, #respond_not_found, #respond_not_implemented, #respond_text

Constructor Details

#initialize(mount) ⇒ DataApi

Returns a new instance of DataApi.

Parameters:



22
23
24
# File 'lib/capsium/reactor/data_api.rb', line 22

def initialize(mount)
  @mount = mount
end

Class Method Details

.path?(inner_path) ⇒ Boolean

Parameters:

  • inner_path (String)

Returns:

  • (Boolean)


18
19
20
# File 'lib/capsium/reactor/data_api.rb', line 18

def self.path?(inner_path)
  inner_path.start_with?(PREFIX)
end

Instance Method Details

#handle(inner_path, request, response) ⇒ Object

Parameters:

  • inner_path (String)
  • request (Object)
  • response (Object)

Returns:

  • (Object)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capsium/reactor/data_api.rb', line 37

def handle(inner_path, request, response)
  route = route_for(inner_path)
  return respond_not_found(response) unless route

  dataset = @mount.package.storage.dataset(route.dataset)
  return respond_not_found(response) unless dataset

  collection = COLLECTION_PATTERN.match(inner_path)
  if collection
    handle_collection(dataset, request, response)
  else
    handle_item(dataset, ITEM_PATTERN.match(inner_path)[:id], request, response)
  end
end

#route_for(inner_path) ⇒ Package::Route?

The declared dataset route a data path addresses, or nil.

Parameters:

  • inner_path (String)

Returns:



29
30
31
32
33
34
35
# File 'lib/capsium/reactor/data_api.rb', line 29

def route_for(inner_path)
  match = COLLECTION_PATTERN.match(inner_path) || ITEM_PATTERN.match(inner_path)
  return nil unless match

  route = @mount.routes.resolve("#{PREFIX}#{match[:dataset]}")
  route if route&.dataset == match[:dataset]
end