Class: Factbook::JsonBuilder

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

Overview

json builder – lets us rebuild a page from “dumped” json (instead of parsing html page)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NormalizeHelper

#normalize_category

Constructor Details

#initialize(text) ⇒ JsonBuilder

Returns a new instance of JsonBuilder.



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

def initialize( text )
  @text = text
    
  @json = JSON.parse( text )

  @info   = nil   ## fix/todo: sorry - for now no page info (use header in json - why? why not??)
  @errors = []       ## fix/todo: sorry - for now no errors possible/tracked

  @sects = []

  @json.each do |k1,v1|
    sect_title    = k1
    sect_subsects = v1
    
    sect = Sect.new
    sect.title = sect_title
    
    ## get subsections
    subsects = []
    sect_subsects.each do |k2,v2|
      subsect_title = k2
      subsect_data  = v2
      
      subsect = Subsect.new
      subsect.title = subsect_title

      #####
      ## note: run data hash through normalize_category (again)
      if subsect_data.is_a?( Hash )
        new_subsect_data = {}
        subsect_data.each do |k3,v3|
          new_subsect_data[ normalize_category(k3) ] = v3
        end
        subsect_data = new_subsect_data
      end  
 
      subsect.data  = subsect_data
      
      subsects << subsect
    end
    
    sect.subsects = subsects
    @sects << sect
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'lib/factbook/builder_json.rb', line 23

def errors
  @errors
end

#infoObject (readonly)

Returns the value of attribute info.



23
24
25
# File 'lib/factbook/builder_json.rb', line 23

def info
  @info
end

#jsonObject (readonly)

Returns the value of attribute json.



23
24
25
# File 'lib/factbook/builder_json.rb', line 23

def json
  @json
end

#sectsObject (readonly)

Returns the value of attribute sects.



23
24
25
# File 'lib/factbook/builder_json.rb', line 23

def sects
  @sects
end

#textObject (readonly)

Returns the value of attribute text.



23
24
25
# File 'lib/factbook/builder_json.rb', line 23

def text
  @text
end

Class Method Details

.from_file(path) ⇒ Object



13
14
15
16
# File 'lib/factbook/builder_json.rb', line 13

def self.from_file( path )
  text = File.read( path )     ## fix: use File.read_utf8  from textutils
  self.from_string( text )
end

.from_string(text) ⇒ Object



18
19
20
# File 'lib/factbook/builder_json.rb', line 18

def self.from_string( text )
  self.new( text )
end