Module: Janeway

Defined in:
lib/janeway.rb,
lib/janeway/error.rb,
lib/janeway/lexer.rb,
lib/janeway/token.rb,
lib/janeway/parser.rb,
lib/janeway/version.rb,
lib/janeway/ast/null.rb,
lib/janeway/ast/error.rb,
lib/janeway/ast/query.rb,
lib/janeway/functions.rb,
lib/janeway/ast/number.rb,
lib/janeway/ast/boolean.rb,
lib/janeway/ast/helpers.rb,
lib/janeway/interpreter.rb,
lib/janeway/ast/function.rb,
lib/janeway/ast/selector.rb,
lib/janeway/ast/root_node.rb,
lib/janeway/ast/expression.rb,
lib/janeway/ast/string_type.rb,
lib/janeway/functions/count.rb,
lib/janeway/functions/match.rb,
lib/janeway/functions/value.rb,
lib/janeway/ast/current_node.rb,
lib/janeway/functions/length.rb,
lib/janeway/functions/search.rb,
lib/janeway/ast/child_segment.rb,
lib/janeway/ast/name_selector.rb,
lib/janeway/interpreters/base.rb,
lib/janeway/ast/index_selector.rb,
lib/janeway/ast/unary_operator.rb,
lib/janeway/ast/binary_operator.rb,
lib/janeway/ast/filter_selector.rb,
lib/janeway/interpreters/yielder.rb,
lib/janeway/ast/wildcard_selector.rb,
lib/janeway/ast/descendant_segment.rb,
lib/janeway/ast/array_slice_selector.rb,
lib/janeway/interpreters/tree_constructor.rb,
lib/janeway/interpreters/function_interpreter.rb,
lib/janeway/interpreters/root_node_interpreter.rb,
lib/janeway/interpreters/current_node_interpreter.rb,
lib/janeway/interpreters/child_segment_interpreter.rb,
lib/janeway/interpreters/name_selector_interpreter.rb,
lib/janeway/interpreters/index_selector_interpreter.rb,
lib/janeway/interpreters/unary_operator_interpreter.rb,
lib/janeway/interpreters/binary_operator_interpreter.rb,
lib/janeway/interpreters/filter_selector_interpreter.rb,
lib/janeway/interpreters/wildcard_selector_interpreter.rb,
lib/janeway/interpreters/descendant_segment_interpreter.rb,
lib/janeway/interpreters/array_slice_selector_interpreter.rb

Overview

Defined Under Namespace

Modules: AST, Functions, Interpreters Classes: Error, Interpreter, Lexer, Parser, Token

Constant Summary collapse

OPERATORS =
{
  and: '&&',
  array_slice_separator: ':',
  child_end: ']',
  child_start: '[',
  current_node: '@',
  descendants: '..',
  dot: '.',
  equal: '==',
  filter: '?',
  greater_than: '>',
  greater_than_or_equal: '>=',
  group_end: ')',
  group_start: '(',
  less_than: '<',
  less_than_or_equal: '<=',
  minus: '-',
  not: '!',
  not_equal: '!=',
  or: '||',
  root: '$',
  union: ',',
  wildcard: '*',
}.freeze
ONE_CHAR_LEX =
OPERATORS.values.select { |lexeme| lexeme.size == 1 }.freeze
TWO_CHAR_LEX =
OPERATORS.values.select { |lexeme| lexeme.size == 2 }.freeze
TWO_CHAR_LEX_FIRST =
TWO_CHAR_LEX.map { |lexeme| lexeme[0] }.freeze
ONE_OR_TWO_CHAR_LEX =
ONE_CHAR_LEX & TWO_CHAR_LEX.map { |str| str[0] }.freeze
WHITESPACE =
" \t\n\r"
KEYWORD =
%w[true false null].freeze
FUNCTIONS =
%w[length count match search value].freeze
ALPHABET =

faster to check membership in a string than an array of char (benchmarked ruby 3.1.2)

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
DIGITS =
'0123456789'
NAME_FIRST =

chars that may be used as the first letter of member-name-shorthand

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'
VERSION =
'0.3.0'

Class Method Summary collapse

Class Method Details

.compile(query) ⇒ Janeway::AST::Query

Compile a JSONPath query into an Abstract Syntax Tree.

This can be used and re-used later on multiple inputs.

Parameters:

  • query (String)

    jsonpath query

Returns:



30
31
32
# File 'lib/janeway.rb', line 30

def self.compile(query)
  Janeway::Parser.parse(query)
end

.each(query, input) {|matched| ... } ⇒ void

This method returns an undefined value.

Iterate through each value matched by the JSONPath query.

Parameters:

  • query (String)

    jsonpath query

  • input (Hash, Array)

    ruby object to be searched

Yield Parameters:

  • matched (Object)

    value

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/janeway.rb', line 40

def self.each(query, input, &block)
  raise ArgumentError, "Invalid jsonpath query: #{query.inspect}" unless query.is_a?(String)
  unless [Hash, Array, String].include?(input.class)
    raise ArgumentError, "Invalid input, expecting array or hash: #{input.inspect}"
  end
  return enum_for(:each, query, input) unless block_given?

  ast = Janeway::Parser.parse(query)
  interpreter = Janeway::Interpreter.new(ast)
  yielder = Janeway::Interpreters::Yielder.new(&block)
  interpreter.push(yielder)
  interpreter.interpret(input)
end

.find_all(query, input) ⇒ Array

Apply a JSONPath query to the input, and return all matched values.

Parameters:

  • query (String)

    JSONPath query

  • input (Hash, Array)

    ruby object to be searched

Returns:

  • (Array)

    all matched objects



19
20
21
22
# File 'lib/janeway.rb', line 19

def self.find_all(query, input)
  ast = compile(query)
  Janeway::Interpreter.new(ast).interpret(input)
end