class TowerBridgeLifts::View

Public Instance Methods

render(object, command, options, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 9
def render(object, command, options, format)
  options = {} if command != 'lifts'
  result = object.error || (options.empty? ? object.send(command) : object.send(command, options))
  view_name = case
    when object.error                 then 'error'
    when result.nil? || result == []  then 'empty'
    when command == 'lifts'           then ( [:lifts] + ([:group, :compact] & options.keys) ).join('_')
    when true                         then command
  end
  self.send(view_name, result, format)
end

Private Instance Methods

bascules(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 60
def bascules(result, format)
  case format
    when :txt  then result
    when :json then [result].to_json
    when :html then result
  end
end
empty(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 27
def empty(result, format)
  msg = 'no lifts scheduled'
  case format
    when :txt  then "#{msg}\n"
    when :json then [msg].to_json
    when :html then msg
  end
end
error(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 23
def error(result, format)
  result.to_s
end
lifts(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 76
def lifts(result, format)
  case format
    when :txt  then result.map{ |lift| lift.to_s }.join("\n")
    when :json then result.map{ |lift| lift.to_h }.to_json
    when :html then template(result, 'lifts.erb')
  end 
end
lifts_compact(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 84
def lifts_compact(result, format)
  case format
    when :txt  then result.map{ |lift| lift.date_weekday_time }.join("\n") 
    when :json then result.map{ |lift| lift.to_h }.to_json
    when :html then template(result, 'lifts_compact.erb')
  end
end
lifts_group(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 93
def lifts_group(result, format)
  case format
    when :txt  then result.map{ |k,v| [k] + (v.map{|lift| [[lift.time, lift.dir, lift.vessel].join(' ')] }) + [''] }.join("\n")
    when :json then Hash[ result.map{|date, lifts| [date, lifts.map(&:to_h)]} ].to_json
    when :html then template(result, 'lifts_group.erb')
  end
end
lifts_group_compact(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 101
def lifts_group_compact(result, format)
  case format
    when :html then template(result, 'lifts_group_compact.erb')
    else
      result.map{ |k,v| ([k] + ["[#{v.count}]"] + (v.map{|lift| lift.time})).join(' ') }.join("\n")
    end
end
next_lift(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 52
def next_lift(result, format)
  case format
    when :txt  then result
    when :json then result.to_h.to_json
    when :html then result
  end
end
status(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 36
def status(result, format)
  case format
    when :txt  then result.map{|k,v| "#{k}: #{v}" }.join("\n")
    when :json then Hash[result.map{|k,v| [k, v.respond_to?(:to_h) ? v.to_h : v] }].to_json
    when :html then template(result, 'status.erb')
  end
end
template(result, name) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 109
def template(result, name)
  ERB.new(File.read(File.join(__dir__, 'templates', name))).result(binding)
end
time(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 44
def time(result, format)
  case format
    when :txt  then result
    when :json then [result].to_json
    when :html then result
  end      
end
traffic(result, format) click to toggle source
# File lib/tower_bridge_lifts/views.rb, line 68
def traffic(result, format)
  case format
    when :txt  then result
    when :json then [result].to_json
    when :html then result
  end
end