Class: TimelineSetter::Timeline

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Timeline

Instantiate a new timeline from an events array created in Parser#initialize



6
7
8
9
# File 'lib/timeline_setter/timeline.rb', line 6

def initialize(opts = {})
  @events   = opts[:events]
  @interval = opts[:interval] || ''
end

Instance Attribute Details

#timelineObject (readonly)

Create timeline HTML by interpolating events hash into an ERB template. Re-template timeline by editing ../templates/timeline.erb This version preserves external links to CSS and JS.



32
33
34
# File 'lib/timeline_setter/timeline.rb', line 32

def timeline
  @timeline
end

Instance Method Details

#config_jsonObject



18
19
20
21
22
23
# File 'lib/timeline_setter/timeline.rb', line 18

def config_json
  {
    "interval"  => "#{@interval}",
    "container" => "#timeline"
  }.to_json
end

#timeline_markupObject



25
26
27
# File 'lib/timeline_setter/timeline.rb', line 25

def timeline_markup
  tmpl("timeline-markup.erb")
end

#timeline_minObject

Create a minified one-page version of a timeline by minifying CSS and JS and embedding all assets into our ERB template.



38
39
40
41
42
43
44
45
46
# File 'lib/timeline_setter/timeline.rb', line 38

def timeline_min
  @js = ""
  @css = Kompress::CSS.new(File.open("#{TimelineSetter::ROOT}/public/stylesheets/timeline-setter.css").read).css
  libs = Dir.glob("#{TimelineSetter::ROOT}/public/javascripts/vendor/**").select {|q| q =~ /min/ }
  libs.each { |lib| @js << File.open(lib,'r').read }
  @min_html = Kompress::HTML.new(timeline_markup).html
  @js << File.open("#{TimelineSetter::ROOT}/public/javascripts/timeline-setter.min.js", 'r').read
  @timeline = tmpl("timeline-min.erb")
end

#tmpl(tmpl_file) ⇒ Object



48
49
50
# File 'lib/timeline_setter/timeline.rb', line 48

def tmpl(tmpl_file)
  ERB.new(File.open("#{TimelineSetter::ROOT}/templates/#{tmpl_file}").read).result(binding)
end

#to_jsonObject

Convert human dates to timestamps, sort the hash by timestamp, and convert the events hash to JSON to stick into our HTML.



13
14
15
16
# File 'lib/timeline_setter/timeline.rb', line 13

def to_json
  @events.each {|r| r[:timestamp] = Time.parse(r[:date]).to_i * 1000 }
  @events.to_json
end