Class: TimelineGen

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

Instance Method Summary collapse

Constructor Details

#initialize(input, headlinefield, textfield, startdatefield, enddatefield, timelinetitle) ⇒ TimelineGen

Returns a new instance of TimelineGen.



5
6
7
8
9
10
11
12
# File 'lib/timelinegen.rb', line 5

def initialize(input, headlinefield, textfield, startdatefield, enddatefield, timelinetitle)
  @input = input
  @textfield = textfield
  @headlinefield = headlinefield
  @startdatefield = startdatefield
  @enddatefiled = enddatefield
  @timelinetitle = timelinetitle
end

Instance Method Details

#checkNil(f, num, pe) ⇒ Object

Check to see if value exists



76
77
78
79
80
81
# File 'lib/timelinegen.rb', line 76

def checkNil(f, num, pe)
  if (pe[num+1])[f]
    return (pe[num+1])[f].to_s
  else return ""
  end
end

#checkSplit(field, num, pe) ⇒ Object

Check to see if events should be split on comma



55
56
57
58
59
60
61
# File 'lib/timelinegen.rb', line 55

def checkSplit(field, num, pe)
  if field != nil && field.include?(",")
    return split(field, num, pe)
  else 
    return (pe[num+1])[field]
  end
end

#genEvent(startDate, endDate, headline, text) ⇒ Object

Generate JSON for event



27
28
29
30
31
32
33
34
# File 'lib/timelinegen.rb', line 27

def genEvent(startDate, endDate, headline, text)
  JSON.pretty_generate(
                       "startDate" => parseDate(startDate),
                       "endDate" => parseDate(endDate),
                       "headline" => headline,
                       "text" => text
                      )
end

#genTimeline(event, headline) ⇒ Object

Generates full timeline JSON



89
90
91
92
93
94
95
96
97
98
# File 'lib/timelinegen.rb', line 89

def genTimeline(event, headline)
  JSON.pretty_generate(
                       "timeline" => {
                         "headline" => headline,
                         "type" => "default",
                         "text" => "   ",
                         "date" => event
                       }
                     )
end

#parseDate(date) ⇒ Object

Change date format to work with TimelineJS



15
16
17
18
19
20
21
22
23
24
# File 'lib/timelinegen.rb', line 15

def parseDate(date)
  if date != nil
    date.gsub!("-",",")
    date.gsub!(" ",",")
    date.gsub!("T", ",")
    date.gsub!("Z", "")
    date.gsub!(":",",")
  end
  date
end

#parseEventsObject

Parses events for timeline



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/timelinegen.rb', line 37

def parseEvents
  pe = JSON.parse(@input)
  k = pe.length
  event = Array.new
  (0..k-2).each do |i|
            event[i] = JSON.parse(
                            genEvent(
                                     checkSplit(@startdatefield, i, pe),
                                     checkSplit(@enddatefield, i, pe),
                                     checkSplit(@headlinefield, i, pe),
                                     checkSplit(@textfield, i, pe)
                                     )
                            )
  end
  return event
end

#parseHeadlineObject

Gets the headline



84
85
86
# File 'lib/timelinegen.rb', line 84

def parseHeadline
  headline = @timelinetitle
end

#split(field, num, pe) ⇒ Object

Splits fields on comma and returns values



64
65
66
67
68
69
70
71
72
73
# File 'lib/timelinegen.rb', line 64

def split(field, num, pe)
  fieldarray = field.split( /, */ )
  resultstring = ""

  fieldarray.each do |f|
    resultstring = resultstring + f + ": " + checkNil(f, num, pe) + "\n"
  end

  return resultstring
end

#timelineObject



100
101
102
103
104
# File 'lib/timelinegen.rb', line 100

def timeline
  event = parseEvents
  headline = parseHeadline
  genTimeline(event, headline)
end