Class: WebFunction::Promise::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/web_function/promise.rb

Overview

A path is a JSONPath expression that can be used to resolve a value from a response.

Examples:

path = WebFunction::Promise::Path.new("$[0]")
path.to_s # => "$[0]"
path[0] # => { "a" => 1 }

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path

Returns a new instance of Path.



26
27
28
# File 'lib/web_function/promise.rb', line 26

def initialize(path)
  @path = path
end

Instance Method Details

#[](key) ⇒ Path

Returns a new path with the given key.

Parameters:

  • key (String, Symbol, Integer)

    The key to add to the path

Returns:

  • (Path)

    A new Path instance



54
55
56
57
58
59
60
61
62
63
# File 'lib/web_function/promise.rb', line 54

def [](key)
  case key
  when String, Symbol
    mutate("#{@path}.#{key}")
  when Integer
    mutate("#{@path}[#{key}]")
  else
    raise ArgumentError
  end
end

#mutate(path) ⇒ Path

Mutates the path with the given path.

Parameters:

  • path (String)

    The path to mutate

Returns:

  • (Path)

    A new Path instance



71
72
73
# File 'lib/web_function/promise.rb', line 71

def mutate(path)
  Path.new(path)
end

#to_json(*args) ⇒ String

Returns the JSON representation of the path.

Parameters:

  • args (Array)

    The arguments to pass to the JSON.generate method

Returns:

  • (String)

    The JSON representation of the path



44
45
46
# File 'lib/web_function/promise.rb', line 44

def to_json(*args)
  @path.to_json(*args)
end

#to_sString

Returns the string representation of the path.

Returns:

  • (String)

    The string representation of the path



34
35
36
# File 'lib/web_function/promise.rb', line 34

def to_s
  @path
end