Class: Wayfarer::CLI::RoutePrinter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfarer/cli/route_printer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Turns a routing tree into a Hash and prints it. Used by the route CLI subcommand.

Constant Summary collapse

BATCH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"tmp"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, url, serializer) ⇒ RoutePrinter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RoutePrinter.

Parameters:

  • route (Wayfarer::Routing::Route)

    route to print

  • url (String)

    URL to match

  • serializer (Proc<Hash=>String>)

    output serializer



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wayfarer/cli/route_printer.rb', line 32

def initialize(route, url, serializer)
  @route = route
  @serializer = serializer

  @nodes = {}
  @root_hash = nil

  task = Wayfarer::Task.new(url, BATCH)
  task[:uri] = Addressable::URI.parse(url)
  @path_finder = Wayfarer::Routing::PathFinder.new(
    task,
    stop_when_found: false,
    &method(:call)
  )
end

Class Method Details

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a routing tree.

Parameters:

  • route (Wayfarer::Routing::Route)

    route to print

  • url (String)

    URL to match

  • format (String, Symbol)

    :json, :yaml or :ruby



25
26
27
# File 'lib/wayfarer/cli/route_printer.rb', line 25

def self.print(route, url, format:)
  new(route, url, serializers.fetch(format.to_sym)).print
end

Instance Method Details

#call(route, result, path_finder) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Callback method called by path_finder with the result of matching the route.

Parameters:



63
64
65
66
67
68
69
70
# File 'lib/wayfarer/cli/route_printer.rb', line 63

def call(route, result, path_finder)
  node = (nodes[route] ||= attributes(route, result, path_finder))
  parent = route.parent

  return @root_hash ||= node unless parent

  nodes.dig(parent, route_type(parent), :children).append(node)
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Processes the routing trees and prints the serialized output.



49
50
51
52
53
54
55
# File 'lib/wayfarer/cli/route_printer.rb', line 49

def print
  route.accept(path_finder)

  hash = routing_result(path_finder).merge(root_hash)

  puts serializer.call(hash)
end

#serializersHash<Symbol, Proc>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash<Symbol, Proc>)


11
12
13
14
15
16
# File 'lib/wayfarer/cli/route_printer.rb', line 11

class_attribute :serializers,
default: { yaml: ->(hash) { YAML.dump(hash.deep_stringify_keys) },
           json: ->(hash) { JSON.pretty_generate(hash) },
           ruby: ->(hash) { pp(hash) } },
instance_accessor: false,
instance_predicate: false