Class: Trainspotter::Ingest::ParamsParser

Inherits:
Object
  • Object
show all
Defined in:
app/jobs/trainspotter/ingest/params_parser.rb

Defined Under Namespace

Classes: ParseError

Class Method Summary collapse

Class Method Details

.evaluate(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/jobs/trainspotter/ingest/params_parser.rb', line 17

def self.evaluate(node)
  case node
  when Prism::HashNode
    node.elements.to_h { |assoc| [evaluate(assoc.key), evaluate(assoc.value)] }
  when Prism::ArrayNode
    node.elements.map { |el| evaluate(el) }
  when Prism::StringNode
    node.unescaped
  when Prism::IntegerNode, Prism::FloatNode
    node.value
  when Prism::NilNode then nil
  when Prism::TrueNode then true
  when Prism::FalseNode then false
  else
    raise ParseError, "Unsafe node type: #{node.class}"
  end
end

.parse(string) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
# File 'app/jobs/trainspotter/ingest/params_parser.rb', line 8

def self.parse(string)
  return {} if string.nil? || string.strip.empty?

  result = Prism.parse(string)
  raise ParseError, result.errors.first.message unless result.success?

  evaluate(result.value.statements.body.first)
end