Class: Factbook::Page

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/factbook-readers/page.rb

Constant Summary collapse

SITE_BASE =

standard version (note: requires https)

"https://www.cia.gov/the-world-factbook/geos/{code}.json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = nil, json: nil, cache: false, info: nil) ⇒ Page

Returns a new instance of Page.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/factbook-readers/page.rb', line 30

def initialize( code=nil,
                json: nil,
                cache: false,
                info: nil )
  if json
     ## note: assumes json is (still) a string/text
     ##        (NOT yet parsed to structured data)
    b = ProfileBuilder.new( json )
  else  ## assume "raw" json dataset
      ## allow passing in code struct too - just use/pluck two-letter code from struct !!!
      code = code.code   if code.is_a?( Codes::Code )

      raise ArgumentError, "two letter code (e.g. au) required to download page & build page url"   if code.nil?
      url = SITE_BASE.sub( '{code}', code )

      raw_data = if cache && Webcache.exist?( url )
                   text = Webcache.read( url )  ## for debugging - read from cache
                   JSON.parse( text )
                 else
                   download_data( url )
                 end

## meta info from raw date - example:
##   "name": "Aruba",
##   "code": "AA",
##   "region": "Central America",
##   "published": "2021-01-25 09:07:08 -0500",
##   "updated": "2021-01-22 14:38:14 -0500",
##
## note: published is NOT before updated (like an alias for created) BUT is often older/later than updated - why!?

      @info = PageInfo.new

      @info.country_code = raw_data['code'].downcase
      @info.country_name = raw_data['name']
      @info.region_name  = raw_data['region']

      ## note: just parse year,month,day for now (skip hours,minutes,etc.)
      @info.published    = Date.strptime( raw_data['published'], '%Y-%m-%d' )
      @info.updated      = Date.strptime( raw_data['updated'], '%Y-%m-%d' )

      data = convert_cia( raw_data )
      b = ProfileBuilder.new( data )
  end

  @profile = b.profile

  ## todo/fix/quick hack:
  ##  check for info opts - lets you overwrite page info
  ##  -- use proper header to setup page info - why, why not??
  @info = info    if info
end

Instance Attribute Details

#infoObject (readonly)

meta info e.g. country_code, country_name, region_name, updated, etc.



8
9
10
# File 'lib/factbook-readers/page.rb', line 8

def info
  @info
end

Class Method Details

.download(code, cache: false) ⇒ Object



24
25
26
# File 'lib/factbook-readers/page.rb', line 24

def self.download( code, cache: false )
  new( code, cache: cache )
end

.parse_json(json) ⇒ Object

parse json from string



15
16
17
# File 'lib/factbook-readers/page.rb', line 15

def self.parse_json( json )  ## parse json from string
  new( json: json )
end

.read_json(path) ⇒ Object



19
20
21
22
# File 'lib/factbook-readers/page.rb', line 19

def self.read_json( path )
  json = File.open( path, 'r:utf-8' ) { |f| f.read }
  new( json: json )
end

Instance Method Details

#[](key) ⇒ Object

convenience helpers - forward to profile



86
# File 'lib/factbook-readers/page.rb', line 86

def [](key)                   @profile[key]; end

#sizeObject



89
# File 'lib/factbook-readers/page.rb', line 89

def size()                    @profile.size; end

#to_hObject



87
# File 'lib/factbook-readers/page.rb', line 87

def to_h()                    @profile.to_h; end

#to_json(minify: false) ⇒ Object



88
# File 'lib/factbook-readers/page.rb', line 88

def to_json( minify: false )  @profile.to_json( minify: minify ); end