Class: Factbook::JsonPageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/factbook/reader_json.rb

Instance Method Summary collapse

Constructor Details

#initialize(json_dir) ⇒ JsonPageReader

Returns a new instance of JsonPageReader.



7
8
9
# File 'lib/factbook/reader_json.rb', line 7

def initialize( json_dir )
  @json_dir = json_dir
end

Instance Method Details

#read_page(code) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/factbook/reader_json.rb', line 11

def read_page( code )
  path = "#{@json_dir}/#{region_to_slug(code.region)}/#{code.code}.json"

  puts "reading #{code.code} #{code.name} (#{code.region}) [#{path}]..."
  json = File.read( path )

## todo/fix/quick hack: for now until we have a proper header/meta/info section in json
#    add some page info from code struct

  info = PageInfo.new
  info.country_code = code.code
  info.country_name = code.name
  info.region_name  = code.region
  
  page = Page.new( code.code, json: json, info: info )
  page
end

#read_pages(codes, limit: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/factbook/reader_json.rb', line 29

def read_pages( codes, limit: nil )
  pages = []
  i=0
  codes.each do |code|
    next if limit && i > limit   ## for debugging just process first x entries
    
    pages << read_page( code )  
  end
  pages
end