Class: Scruber::Core::Extensions::Loop

Inherits:
Base show all
Defined in:
lib/scruber/core/extensions/loop.rb

Overview

Helper for reading dictionaries.

Examples:

Adding dictionary and reading it

Scruber.run :sample do
  add_dictionary :zip_codes_usa, Scruber.root.join('dict', 'zip_codes_usa.csv'), :csv
  seed do
    loop :zip_codes_usa, state: 'NY' do |row|
      get 'https://example.com/by_zip/'+row['zip'].to_s
    end
  end
end

Author:

  • Ivan Goncharov

Defined Under Namespace

Modules: CoreMethods

Class Method Summary collapse

Methods inherited from Base

descendants, inherited, register

Class Method Details

._registered_dictionariesObject



65
66
67
# File 'lib/scruber/core/extensions/loop.rb', line 65

def _registered_dictionaries
  @registered_dictionaries ||= {}
end

.add_dictionary(name, file_path, file_type) ⇒ Object



58
59
60
61
62
63
# File 'lib/scruber/core/extensions/loop.rb', line 58

def add_dictionary(name, file_path, file_type)
  _registered_dictionaries[name.to_sym] = {
    file_path: file_path,
    file_type: file_type
  }
end

.loop(dictionary, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/scruber/core/extensions/loop.rb', line 48

def loop(dictionary, options={})
  if _registered_dictionaries.keys.include?(dictionary.to_sym)
    Scruber::Helpers::DictionaryReader.read(_registered_dictionaries[dictionary.to_sym][:file_path], _registered_dictionaries[dictionary.to_sym][:file_type], options) do |obj|
      yield obj
    end
  else
    raise ArgumentError, "dictionary not registered, available dictionaries #{_registered_dictionaries.keys.inspect}"
  end
end