Class: Arstotzka::PostProcessor Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/post_processor.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 reponsible for processing the result of Crawler#crawl.

PostProcessor proccess the whole collection and not individual results

Examples:

Simple Usage

class Employe
  attr_reader :name, :age, :company

  def initialize(name:, age:, company:)
    @name    = name
    @age     = age
    @company = company
  end

  def adult?
    age >= 18
  end

  def ==(other)
    return unless other.class == self.class
    other.name == name &&
      other.age == age &&
      other.company == company
  end
end

class Company
  def create_employes(people)
    people.map do |person|
      Employe.new(company: self, **person)
    end.select(&:adult?)
  end
end

company = Company.new

options = {
  after: :create_employes,
  flatten: true,
  instance: company
}

processor = Arstotzka::PostProcessor.new(options)

value = [
  [
    { name: 'Bob',   age: 21 },
    { name: 'Rose',  age: 19 }
  ], [
    { name: 'Klara', age: 18 },
    { name: 'Al',    age: 15 }
  ]
]

processor.process(value)

# returns [
#   Employe.new(name: 'Bob',   age: 21, company: company),
#   Employe.new(name: 'Rose',  age: 19, company: company),
#   Employe.new(name: 'Klara', age: 18, company: company)
# ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(options_hash) ⇒ PostProcessor #initialize(options) ⇒ PostProcessor

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.

Returns a new instance of PostProcessor.

Overloads:

  • #initialize(options_hash) ⇒ PostProcessor

    Parameters:

    Options Hash (options_hash):

    • instance (Objct)
    • after (String, Symbol)

      instance method to be called on the returning value returned by Crawler before being returned by Fetcher.

    • flatten (Boolean)

      flag signallying if multi levels arrays should be flattened to one level array (applying Array#flatten)

  • #initialize(options) ⇒ PostProcessor

    Parameters:



81
82
83
# File 'lib/arstotzka/post_processor.rb', line 81

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

Instance Attribute Details

#optionsObject (readonly, 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.



101
102
103
# File 'lib/arstotzka/post_processor.rb', line 101

def options
  @options
end

Instance Method Details

#process(value) ⇒ 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.

Apply transformation and filters on the result

Parameters:

  • value (Object)

    value is returned from crawler being a single object, or an array.

Returns:

  • (Object)


91
92
93
94
95
96
97
# File 'lib/arstotzka/post_processor.rb', line 91

def process(value)
  value.flatten! if flatten && value.is_a?(Array)

  return value unless after

  instance.send(after, value)
end