Skip to content

Instantly share code, notes, and snippets.

@ktopolski ktopolski/strategies.rb

Last active Sep 19, 2017
Embed
What would you like to do?
Enumerator Strategies
# importing method
def import_items(source)
if SECTION_SOURCES.include? source.name
Strategies::SectionStrategy.items(@repository, source.id).each(&method(:import_item))
else
Strategies::ItemsStrategy.items(@repository, source.id).each(&method(:import_item))
end
end
# strategies
module SampleAPI
module Imports
module Strategies
module ItemsStrategy
def self.items(repository, source_id)
Enumerator.new do |yielder|
offset = 0
limit = repository.class::ITEMS_PAGINATION_LIMIT
loop do
items = repository.items(
source_id: source_id,
filters: {
offset: offset,
limit: limit
}
)['items']
raise StopIteration if items.empty?
items.each { |item| yielder << item }
offset += limit
end
end
end
end
end
end
end
module SampleAPI
module Imports
module Strategies
module SectionStrategy
MAX_ITEMS_PER_SECTION = 10_000
def self.items(repository, source_id)
Enumerator.new do |yielder|
section_ids =
repository
.sections(source_id: source_id)['items']
.map { |json| json['id'] }
section_ids.each do |section_id|
sections_record_count = repository.items(
source_id: source_id,
filters: {
offset: 0,
limit: repository.class::ITEMS_PAGINATION_LIMIT,
'sectionCode' => section_id
}
)['recordCount']
if sections_record_count <= MAX_ITEMS_PER_SECTION
import_items_for_section_id(repository, source_id, section_id, yielder)
else
sub_section_ids = repository.sections_children(
source_id: source_id,
section_id: section_id
)['items'].map { |json| json['id'] }
sub_section_ids.each do |sub_section_id|
import_items_for_section_id(repository, source_id, sub_section_id, yielder)
end
end
end
end
end
def self.import_items_for_section_id(repository, source_id, section_id, yielder)
limit = repository.class::ITEMS_PAGINATION_LIMIT
offset = 0
loop do
items = repository.items(
source_id: source_id,
filters: {
offset: offset,
limit: limit,
'sectionCode' => section_id
}
)['items']
raise StopIteration if items.empty?
items.each { |item| yielder << item }
offset += limit
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.