Last active
September 19, 2017 21:15
-
-
Save ktopolski/447870a00ee3cf10eae793eff60de6ff to your computer and use it in GitHub Desktop.
Enumerator Strategies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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