Class: Capsium::Package::Routes

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
sig/capsium/package/routes.rbs,
lib/capsium/package/routes.rb

Overview

Loads, generates and writes routes.json (ARCHITECTURE.md section 4).

Constant Summary collapse

INDEX_ROUTE =

Returns:

  • (String)
"/"
INDEX_RESOURCE =

Returns:

  • (String)
"content/index.html"
DATASET_ROUTE_PREFIX =

Returns:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, manifest, storage) ⇒ Routes

Returns a new instance of Routes.

Parameters:



18
19
20
21
22
23
24
25
26
27
# File 'lib/capsium/package/routes.rb', line 18

def initialize(path, manifest, storage)
  @path = path
  @manifest = manifest
  @storage = storage
  @config = if File.exist?(path)
              RoutesConfig.from_json(File.read(path))
            else
              generate_routes
            end
end

Instance Attribute Details

#configRoutesConfig (readonly)

Returns the value of attribute config.

Returns:



10
11
12
# File 'lib/capsium/package/routes.rb', line 10

def config
  @config
end

#manifestManifest (readonly)

Returns the value of attribute manifest.

Returns:



10
11
12
# File 'lib/capsium/package/routes.rb', line 10

def manifest
  @manifest
end

#pathString (readonly)

Returns the value of attribute path.

Returns:

  • (String)


10
11
12
# File 'lib/capsium/package/routes.rb', line 10

def path
  @path
end

#storageStorage (readonly)

Returns the value of attribute storage.

Returns:



10
11
12
# File 'lib/capsium/package/routes.rb', line 10

def storage
  @storage
end

Instance Method Details

#add_route(route, target) ⇒ Route

Parameters:

  • route (String)
  • target (String)

Returns:



33
34
35
# File 'lib/capsium/package/routes.rb', line 33

def add_route(route, target)
  @config.add(route, target)
end

#generate_routesRoutesConfig

Auto-generation (ARCHITECTURE.md section 4): every manifest resource gets a route at its path relative to content/; HTML files get two routes; the index HTML additionally gets "/"; every dataset gets /api/v1/data/. Deterministic output.

Returns:



58
59
60
61
# File 'lib/capsium/package/routes.rb', line 58

def generate_routes
  routes = resource_routes + dataset_routes
  RoutesConfig.new(index: index_resource, routes: routes.sort_by(&:path))
end

#remove_route(route) ⇒ Array[Route]

Parameters:

  • route (String)

Returns:



41
42
43
# File 'lib/capsium/package/routes.rb', line 41

def remove_route(route)
  @config.remove(route)
end

#resolve(url_path) ⇒ Route?

Parameters:

  • url_path (String)

Returns:



29
30
31
# File 'lib/capsium/package/routes.rb', line 29

def resolve(url_path)
  @config.resolve(url_path)
end

#save_to_file(output_path = @path) ⇒ void

This method returns an undefined value.

Parameters:

  • output_path (String) (defaults to: @path)


49
50
51
# File 'lib/capsium/package/routes.rb', line 49

def save_to_file(output_path = @path)
  File.write(output_path, to_json)
end

#to_hashHash[String, untyped]

Returns:

  • (Hash[String, untyped])


26
# File 'sig/capsium/package/routes.rbs', line 26

def to_hash: () -> Hash[String, untyped]

#to_json(*_args) ⇒ String

Parameters:

  • args (Object)

Returns:

  • (String)


45
46
47
# File 'lib/capsium/package/routes.rb', line 45

def to_json(*_args)
  @config.to_json
end

#update_route(route, updated_route, updated_target) ⇒ Route

Parameters:

  • route (String)
  • updated_route (String)
  • updated_target (String)

Returns:



37
38
39
# File 'lib/capsium/package/routes.rb', line 37

def update_route(route, updated_route, updated_target)
  @config.update(route, updated_route, updated_target)
end