Class: Factbook::Page

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

Constant Summary collapse

SITE_BASE =

standard version (note: requires https)

'https://www.cia.gov/library/publications/the-world-factbook/geos/{code}.html'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, opts = {}) ⇒ Page

Returns a new instance of Page.



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
# File 'lib/factbook/page.rb', line 38

def initialize( code, opts={} )
  ### keep code - why? why not??  (use page_info/info e.g. info.country_code??)

  if opts[:json]
    json = opts[:json]    ## note: json is (still) a string/text (NOT yet parsed to structured data)
    b = JsonBuilder.from_string( json )
  else  ## assume html
    if opts[:html]    ## note: expects ASCII-7BIT/BINARY encoding
       ## for debugging and testing allow "custom" passed-in html page
      html = opts[:html]
    else
      url_string =  SITE_BASE.gsub( '{code}', code )
      ## note: expects ASCII-7BIT/BINARY encoding

      ## html = fetch_page( url_string )   ## use PageFetcher class - why?? why not??
      html = Webcache.read( url_string )
    end
    b = Builder.from_string( html )
  end

  @sects = b.sects
  @info  = b.info

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

  @data = {}
  @sects.each do |sect|
    @data[ sect.title ] = sect.data
  end

  self  ## return self (check - not needed??)
end

Instance Attribute Details

#dataObject (readonly)

“plain” access with vanilla hash



32
33
34
# File 'lib/factbook/page.rb', line 32

def data
  @data
end

#infoObject (readonly)

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



31
32
33
# File 'lib/factbook/page.rb', line 31

def info
  @info
end

#sectsObject (readonly)

“structured” access e.g. sects/subsects/etc.



30
31
32
# File 'lib/factbook/page.rb', line 30

def sects
  @sects
end

Instance Method Details

#[](key) ⇒ Object

convenience shortcut



88
89
90
91
92
93
94
95
96
97
# File 'lib/factbook/page.rb', line 88

def [](key)  ### convenience shortcut
  # lets you use
  #   page['geo']
  #   instead of
  #   page.data['geo']

  ##  fix: use delegate data, [] from forwardable lib - why?? why not??

  data[key]
end

#attribObject

add convenience (shortcut) accessors / attributes / fields / getters



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/factbook/page.rb', line 101

ATTRIBUTES.each do |attrib|
  ## e.g.
  ##    def background()  data['Introduction']['Background']['text']; end
  ##    def location()    data['Geography']['Location']['text'];      end
  ##    etc.
  if attrib.path.size == 1
    define_method attrib.name.to_sym do
      @data.fetch( attrib.category, {} ).
            fetch( attrib.path[0], {} )['text']
    end
  else  ## assume size 2 for now
    define_method attrib.name.to_sym do
      @data.fetch( attrib.category, {} ).
            fetch( attrib.path[0], {} ).
            fetch( attrib.path[1], {} )['text']
    end
  end
end

#to_json(opts = {}) ⇒ Object

convenience helper for data.to_json; note: pretty print by default!



78
79
80
81
82
83
84
85
# File 'lib/factbook/page.rb', line 78

def to_json( opts={} )  ## convenience helper for data.to_json; note: pretty print by default!
  if opts[:minify]
    data.to_json
  else
    ## was: -- opts[:pretty] || opts[:pp]
    JSON.pretty_generate( data )   ## note: pretty print by default!
  end
end