Class: Arstotzka::Crawler Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/crawler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Crawl a hash through the keys of keys

Examples:

Simple usage

crawler = Arstotzka::Crawler.new(full_path: 'person.information.first_name')
hash = {
  person: {
    'information' => {
      'firstName' => 'John'
    }
  }
}
crawler.value(hash) # returns 'John'

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(options_hash = {}, &block) ⇒ Crawler #initialize(options, &block) ⇒ Crawler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of Crawler

Examples:

Simple usage

crawler = Arstotzka::Crawler.new(full_path: 'person.information.first_name')
hash = {
  person: {
    'information' => {
      'firstName' => 'John'
    }
  }
}
crawler.value(hash) # returns 'John'

Overloads:

  • #initialize(options_hash = {}, &block) ⇒ Crawler

    Parameters:

    • options_hash (Hash) (defaults to: {})

      options of initialization

    • block (Proc)

      block to be ran over the fetched value before returning it

    Options Hash (options_hash):

    • keys (Array)

      keys of keys to be crawled

    • case (Symbol)

      case type of the keys

      • snake: snake_cased keys

      • lower_camel: lowerCamelCased keys

      • upper_camel: UperCamelCased keys

    • compact (TrueClass, FalseClass)

      flag signallying if nil values should be removed of array

    • default (Object)

      default value to be returned when failing to fetch a value

  • #initialize(options, &block) ⇒ Crawler

    Parameters:

    • options (Arstotzka::Options)

      options of initialization object

    • block (Proc)

      block to be ran over the fetched value before returning it



43
44
45
46
# File 'lib/arstotzka/crawler.rb', line 43

def initialize(options = {}, &block)
  self.options = options
  @block = block
end

Instance Method Details

#crawl(hash, index = 0) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fetch the value from hash by crawling the keys

The crawling is similar to fetching values in a chain { a: { b: 10 } }.fetch(:a).fetch(:b) with added features like accessing string and symbol keys alike, wrapping the values in new objects, post processing the values, treating arrays as collection of hashes, etc…

Once the value is found (with final key), it is wrapped

If value found in any step (except finel step) and this is an Array, then next interations will happen with it element of the array, returning an array of results

Parameters:

  • hash (Hash)

    the hash to be crawled

  • index (Integer) (defaults to: 0)

    the index of the key to be used in the current iteration

Returns:

  • (Object)

    value found at the lest key after transformation

See Also:

  • #wrap
  • #crawl_array


162
163
164
165
166
167
# File 'lib/arstotzka/crawler.rb', line 162

def crawl(hash, index = 0)
  return wrap(hash) if reader.ended?(index)
  return crawl_array(hash, index) if hash.is_a?(Array)

  crawl(reader.read(hash, index), index + 1)
end

#value(hash, index = 0) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Crawls into the hash looking for all keys in the given keys

Examples:

Passing compact and case options

crawler = Arstotzka::Crawler.new(
  full_path: 'companies.games.hero',
  compact:   true,
  case:      :snake
)
games_hash = {
  'companies' => [{
    name: 'Lucas Pope',
    games: [{
      'name' => 'papers, please'
    }, {
      'name' => 'TheNextBigThing',
      hero_name: 'Rakhar'
    }]
  }, {
    name: 'Old Company'
  }]
}

crawler.value(games_hash) # returns [['Rakhar']]

Passing default option

crawler = Arstotzka::Crawler.new(
  full_path: 'companies.games.hero',
  compact: true, case: :snake, default: 'NO HERO'
)

crawler.value(games_hash) # returns [['NO HERO', 'Rakhar'], 'NO HERO']

Passing a post processor block

crawler = Arstotzka::Crawler.new(
  full_path: 'companies.games.hero',
  compact:   true,
  case: :    snake
) { |value| value.&to_sym }

crawler.value(games_hash) # returns [[:Rakhar]]

Simple usage

crawler = Arstotzka::Crawler.new(full_path: 'person.information.first_name')
hash = {
  person: {
    'information' => {
      'firstName' => 'John'
    }
  }
}
crawler.value(hash) # returns 'John'

Returns:

  • (Object)

    value fetched from the last Hash#fetch call using the last part of keys



93
94
95
96
97
# File 'lib/arstotzka/crawler.rb', line 93

def value(hash, index = 0)
  crawl(hash, index)
rescue Exception::KeyNotFound
  wrap(default)
end