Class: Enroute::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/enroute/export.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_path, config_path) ⇒ Export

Returns a new instance of Export.



11
12
13
14
# File 'lib/enroute/export.rb', line 11

def initialize(output_path, config_path)
  @output_path = output_path
  @config_path = config_path
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



5
6
7
# File 'lib/enroute/export.rb', line 5

def config_path
  @config_path
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



5
6
7
# File 'lib/enroute/export.rb', line 5

def output_path
  @output_path
end

Class Method Details

.call(output_path, config_path) ⇒ Object



7
8
9
# File 'lib/enroute/export.rb', line 7

def self.call(output_path, config_path)
  new(output_path, config_path).call
end

Instance Method Details

#build_ts_args_definition(route) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/enroute/export.rb', line 56

def build_ts_args_definition(route)
  route[:segments].map do |segment|
    type = route.dig(:typings, segment)&.chomp ||
           config.dig(:typings, :_default, segment)&.chomp ||
           "any"

    optional = route[:requiredSegments].include?(segment) ? "" : "?"
    "#{segment.camelize(:lower)}#{optional}: #{type}"
  end.join(", ")
end

#build_ts_handler_function(route) ⇒ Object



67
68
69
70
# File 'lib/enroute/export.rb', line 67

def build_ts_handler_function(route)
  args = JSON.pretty_generate(route.except(:typings))
  %[const #{route[:name]}Handler = buildRoute(#{args});]
end

#build_ts_route_function(route) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/enroute/export.rb', line 72

def build_ts_route_function(route)
  args = build_ts_args_definition(route)
  segments = route[:segments].map {|segment| segment.camelize(:lower) }

  [
    %[export const #{route[:name]}Url = (#{args}): string =>],
    %[  #{route[:name]}Handler(#{segments.join(', ')});]
  ].join("\n")
end

#callObject



26
27
28
29
30
# File 'lib/enroute/export.rb', line 26

def call
  FileUtils.mkdir_p(File.dirname(output_path))

  write_template(output_path)
end

#configObject



16
17
18
19
20
21
22
23
24
# File 'lib/enroute/export.rb', line 16

def config
  @config ||= if File.file?(config_path)
                ActiveSupport::HashWithIndifferentAccess.new(
                  YAML.load_file(config_path)
                )
              else
                {}
              end
end

#handler_functionsObject



48
49
50
# File 'lib/enroute/export.rb', line 48

def handler_functions
  routes.map {|route| build_ts_handler_function(route) }.join("\n\n")
end

#render_templateObject



52
53
54
# File 'lib/enroute/export.rb', line 52

def render_template
  ERB.new(File.read("#{__dir__}/template.ts.erb")).result binding
end

#route_functionsObject



42
43
44
45
46
# File 'lib/enroute/export.rb', line 42

def route_functions
  routes
    .map {|route| build_ts_route_function(route) }
    .join("\n\n")
end

#routesObject



32
33
34
# File 'lib/enroute/export.rb', line 32

def routes
  @routes ||= Routes.call(config)
end

#write_template(output_path) ⇒ Object



36
37
38
39
40
# File 'lib/enroute/export.rb', line 36

def write_template(output_path)
  File.open(output_path, "w+") do |file|
    file << render_template
  end
end