class Underpass::QL::Parser

Deals with parsing the API request response into easily digestable objects which are then returned as matches

Public Class Methods

new(response) click to toggle source
# File lib/underpass/ql/parser.rb, line 8
def initialize(response)
  @response = response
  @matches = []
end

Public Instance Methods

matches() click to toggle source
# File lib/underpass/ql/parser.rb, line 23
def matches
  @nodes.each_value do |node|
    @matches << point_from_node(node) if node.key?(:tags)
  end

  @ways.each_value do |way|
    @matches << way_matches(way) if way.key?(:tags)
  end

  @matches
end
parse() click to toggle source
# File lib/underpass/ql/parser.rb, line 13
def parse
  parsed_json = JSON.parse(@response.body, symbolize_names: true)
  elements = parsed_json[:elements]

  @nodes = extract_indexed_nodes(elements)
  @ways = extract_indexed_ways(elements)

  self
end

Private Instance Methods

extract_indexed_nodes(elements) click to toggle source
# File lib/underpass/ql/parser.rb, line 61
def extract_indexed_nodes(elements)
  nodes = elements.select { |e| e[:type] == 'node' }
  nodes.map { |e| [e[:id], e] }.to_h
end
extract_indexed_ways(elements) click to toggle source
# File lib/underpass/ql/parser.rb, line 66
def extract_indexed_ways(elements)
  ways = elements.select { |e| e[:type] == 'way' }
  ways.map { |e| [e[:id], e] }.to_h
end
line_string_from_way(way, nodes) click to toggle source
# File lib/underpass/ql/parser.rb, line 57
def line_string_from_way(way, nodes)
  Underpass::QL::Shape.line_string_from_way(way, nodes)
end
open_way?(way) click to toggle source
# File lib/underpass/ql/parser.rb, line 45
def open_way?(way)
  Underpass::QL::Shape.open_way?(way)
end
point_from_node(node) click to toggle source
# File lib/underpass/ql/parser.rb, line 49
def point_from_node(node)
  Underpass::QL::Shape.point_from_node(node)
end
polygon_from_way(way, nodes) click to toggle source
# File lib/underpass/ql/parser.rb, line 53
def polygon_from_way(way, nodes)
  Underpass::QL::Shape.polygon_from_way(way, nodes)
end
way_matches(way) click to toggle source
# File lib/underpass/ql/parser.rb, line 37
def way_matches(way)
  if open_way?(way)
    polygon_from_way(way, @nodes)
  else
    line_string_from_way(way, @nodes)
  end
end