Class: PlantWatchdog::UI::SinatraApp

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Datadetection, Monthhelper
Defined in:
lib/plantwatchdog/sinatra.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Monthhelper

#days_of_month

Methods included from Datadetection

#days_with_data, #plant_aggregates, #time_series, #utc_offset, #years_with_data

Constructor Details

#initializeSinatraApp

Returns a new instance of SinatraApp.



54
55
56
# File 'lib/plantwatchdog/sinatra.rb', line 54

def initialize
  @log = Logger.new(STDERR)
end

Instance Attribute Details

#logObject

TODO: patir logger from config



52
53
54
# File 'lib/plantwatchdog/sinatra.rb', line 52

def log
  @log
end

Instance Method Details

#getAvailableData(year, month = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/plantwatchdog/sinatra.rb', line 127

def getAvailableData(year, month=nil) 
  result = []
  if month.nil?
    getDayOfYearConverter(year).months().each {
      |m|
      result << { :id =>  m, :label => m.to_s }
    }
  elsif (not year.nil?) and not month.nil?
    getDayOfYearConverter(year).days(month.to_i).each {
      |d|
      result << { :id => d, :label => d.to_s }
    }
  end
  ActiveSupport::JSON.encode result
end

#getDayOfYearConverter(year) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/plantwatchdog/sinatra.rb', line 143

def getDayOfYearConverter(year)
  result = session[year]
  if result.nil?
    throw "invalid state: getDayOfYearConverter must be called with year argument first" if year.nil?
    days = days_with_data(user, year)
    result = PlantWatchdog::DayOfYearConverter.new(year, days)
    p "adding DayOfYearConverter to session with key "+ year
    session[year]=result
  end
  result
end

#graph_heightObject



76
77
78
# File 'lib/plantwatchdog/sinatra.rb', line 76

def graph_height
  request[:height] ? request[:height] : "450px"
end

#graph_widthObject



80
81
82
# File 'lib/plantwatchdog/sinatra.rb', line 80

def graph_width
  request[:width] ? request[:width] : "700px"
end

#upload(unique_id) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/plantwatchdog/sinatra.rb', line 176

def upload(unique_id)
  protected!
  begin
    return Model::SyncManager.new.sync(session[:user], unique_id, request.body).to_s
  rescue StandardError
     # the put method does not return a 404 page with the exception as the get method does
     log.error "Error uploading csv:"
     log.error $!.message
     log.error $!.backtrace
     status 500
     return $!.message  
  end
end

#userObject



167
168
169
170
# File 'lib/plantwatchdog/sinatra.rb', line 167

def user
  # TODO: support multiple users
  Model::User.find( :first )
end