Class: Factbook::Page
- Inherits:
-
Object
- Object
- Factbook::Page
- 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
-
#data ⇒ Object
readonly
“plain” access with vanilla hash.
-
#info ⇒ Object
readonly
meta info e.g.
-
#sects ⇒ Object
readonly
“structured” access e.g.
Instance Method Summary collapse
-
#[](key) ⇒ Object
convenience shortcut.
-
#attrib ⇒ Object
add convenience (shortcut) accessors / attributes / fields / getters.
-
#initialize(code, opts = {}) ⇒ Page
constructor
A new instance of Page.
-
#to_json(opts = {}) ⇒ Object
convenience helper for data.to_json; note: pretty print by default!.
Constructor Details
#initialize(code, opts = {}) ⇒ Page
Returns a new instance of Page.
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 |
# File 'lib/factbook/page.rb', line 39 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?? 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
#data ⇒ Object (readonly)
“plain” access with vanilla hash
33 34 35 |
# File 'lib/factbook/page.rb', line 33 def data @data end |
#info ⇒ Object (readonly)
meta info e.g. country_code, country_name, region_name, last_updated, etc.
32 33 34 |
# File 'lib/factbook/page.rb', line 32 def info @info end |
#sects ⇒ Object (readonly)
“structured” access e.g. sects/subsects/etc.
31 32 33 |
# File 'lib/factbook/page.rb', line 31 def sects @sects end |
Instance Method Details
#[](key) ⇒ Object
convenience shortcut
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/factbook/page.rb', line 87 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 |
#attrib ⇒ Object
add convenience (shortcut) accessors / attributes / fields / getters
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/factbook/page.rb', line 100 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!
77 78 79 80 81 82 83 84 |
# File 'lib/factbook/page.rb', line 77 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 |