Class: Arstotzka::Fetcher Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/fetcher.rb,
lib/arstotzka/fetcher/cache.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.

Class responsible for orquestrating the fetch value from the hash and post-processing it

Defined Under Namespace

Classes: Cache

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#iniitalize(options_hash = {}) ⇒ Fetcher #iniitalize(options) ⇒ Fetcher

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 an instance of Artotzka::Fetcher

Overloads:



22
23
24
# File 'lib/arstotzka/fetcher.rb', line 22

def initialize(options_hash = {})
  self.options = options_hash
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

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.

Checks if other equals self

Parameters:

  • other (Object)

Returns:

  • (TrueClass, FalseClass)


97
98
99
100
101
# File 'lib/arstotzka/fetcher.rb', line 97

def ==(other)
  return false unless other.class == self.class

  options == other.options
end

#fetchObject

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 the hash for the value

After the crawling, final transformation is applied on the final result (collection not value)

Examples:

Fetching with wrapping and processing

class Transaction
  attr_reader :value, :type

  def initialize(value:, type:)
    @value = value
    @type = type
  end

  def positive?
    type == 'income'
  end
end

class Account
  def initialize(json = {})
    @json = json
  end

  private

  attr_reader :json

  def filter_income(transactions)
    transactions.select(&:positive?)
  end
end

hash = {
  transactions: [
    { value: 1000.53, type: 'income' },
    { value: 324.56,  type: 'outcome' },
    { value: 50.23,   type: 'income' },
    { value: 150.00,  type: 'outcome' },
    { value: 10.23,   type: 'outcome' },
    { value: 100.12,  type: 'outcome' },
    { value: 101.00,  type: 'outcome' }
  ]
}

instance = Account.new

fetcher = Arstotzka::Fetcher.new(
  instance: instance,
  path:     'transactions',
  klass:    Transaction,
  after:    :filter_income
)

fetcher.fetch # retruns [
              #   Transaction.new(value: 1000.53, type: 'income'),
              #   Transaction.new(value: 50.23, type: 'income')
              # ]

Returns:

  • (Object)

    The final value found and transformed



86
87
88
89
90
# File 'lib/arstotzka/fetcher.rb', line 86

def fetch
  Cache.new(options) do
    fetch_value
  end.fetch
end

#hash_readerArstotzka::HashReader (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.

Reader responsible for fetching hash from instance



162
163
164
# File 'lib/arstotzka/fetcher.rb', line 162

def hash_reader
  @hash_reader ||= HashReader.new(options)
end