Sha256: a037a4e31b5dd558a240c09205b3642a7da9084fe28e96781a7c3e08826fd416

Contents?: true

Size: 1.27 KB

Versions: 11

Compression:

Stored size: 1.27 KB

Contents

class PlasticineController < ApplicationController
  before_action :authenticate

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

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

      visual_model.build builder

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


private

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

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

  def visual_model
    name = params[:id].underscore.camelize + 'Visual'

    begin
      name.constantize.new(params)
    rescue
      raise "Plasticine: You must defined #{name}"
    end
  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

11 entries across 11 versions & 1 rubygems

Version Path
plasticine-1.2.8 app/controllers/plasticine_controller.rb
plasticine-1.2.7 app/controllers/plasticine_controller.rb
plasticine-1.2.6 app/controllers/plasticine_controller.rb
plasticine-1.2.5 app/controllers/plasticine_controller.rb
plasticine-1.2.4 app/controllers/plasticine_controller.rb
plasticine-1.2.3 app/controllers/plasticine_controller.rb
plasticine-1.2.2 app/controllers/plasticine_controller.rb
plasticine-1.2.1 app/controllers/plasticine_controller.rb
plasticine-1.2.0 app/controllers/plasticine_controller.rb
plasticine-1.1.1 app/controllers/plasticine_controller.rb
plasticine-1.1.0 app/controllers/plasticine_controller.rb