Sha256: b1bfecc5eee221b37f7468eabc48f9c330337baf454fd09c8343a79aedff03fc

Contents?: true

Size: 1.72 KB

Versions: 14

Compression:

Stored size: 1.72 KB

Contents

#*************************************************************************************
# TOCOMMENT
#*************************************************************************************
class CandyChartsController < ApplicationController
  before_filter :authenticate

  def show
    if @granted
      set_default_to if params[:from] and not params[:to]

      name = (params[:id].gsub('-', '_').camelize + 'Chart')

      begin
        chart = name.constantize.new(params)
      rescue
        raise "Chart Candy: You must defined #{name}"
      end

      builder = "ChartCandy::Builder::#{params[:nature].camelize}".constantize.new(params[:id], params)

      chart.build builder

      respond_to do |format|
        format.json { render json: builder.to_json }
        format.xls { render_xls builder }
      end
    else
      respond_to do |format|
        format.json { render json: { 'state' => 'access_refused' } }
        format.xls { render text: 'access_refused' }
      end
    end
  end

  def render_xls(builder)
    spreadsheet = StringIO.new
    builder.to_xls.write spreadsheet

    send_data spreadsheet.string, filename: builder.filename, type:  "application/vnd.ms-excel"
  end

  def authenticate
    auth = ChartCandy::Authentication.new(request.url, params)

    @granted = (auth.valid_token? and not auth.expired?)
  end

  def set_default_to
    if params[:nature] == 'line'
      params[:to] = case params[:step]
        when 'day' then (Time.now.utc - 1.day).end_of_day.iso8601
        when 'week' then (Time.now.utc - 1.week).end_of_week.iso8601
        when 'month' then (Time.now.utc - 1.month).end_of_month.iso8601
        else Time.now.utc.iso8601
      end
    else
      params[:to] = Time.now.utc.iso8601
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
chart-candy-0.1.7 app/controllers/candy_charts_controller.rb
chart-candy-0.1.6 app/controllers/candy_charts_controller.rb
chart-candy-0.1.5 app/controllers/candy_charts_controller.rb
chart-candy-0.1.4 app/controllers/candy_charts_controller.rb
chart-candy-0.1.3 app/controllers/candy_charts_controller.rb
chart-candy-0.1.2 app/controllers/candy_charts_controller.rb
chart-candy-0.1.1 app/controllers/candy_charts_controller.rb
chart-candy-0.1.0 app/controllers/candy_charts_controller.rb
chart-candy-0.0.12 app/controllers/candy_charts_controller.rb
chart-candy-0.0.11 app/controllers/candy_charts_controller.rb
chart-candy-0.0.10 app/controllers/candy_charts_controller.rb
chart-candy-0.0.9 app/controllers/candy_charts_controller.rb
chart-candy-0.0.8 app/controllers/candy_charts_controller.rb
chart-candy-0.0.7 app/controllers/candy_charts_controller.rb