Class: Sparkplug

Inherits:
Object
  • Object
show all
Defined in:
lib/sparkplug.rb,
lib/sparkplug/cachers/memory.rb,
lib/sparkplug/cachers/abstract.rb,
lib/sparkplug/cachers/filesystem.rb,
lib/sparkplug/handlers/abstract_data.rb

Overview

Render sparkline graphs dynamically from datapoints in a matching CSV file (or anything that there is a Handler for).

Defined Under Namespace

Modules: Cachers, Handlers

Constant Summary collapse

DEFAULT_SPARK_OPTIONS =
{:has_min => true, :has_max => true, :height => 40, :step => 10}

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Sparkplug

Options:

:spark   - Hash of sparkline options.  See spark_pr.rb
:prefix  - URL prefix for handled requests.  Setting it to "/sparks"
  treats requests like "/sparks/stats.csv" as dynamic sparklines.
:cacher  - Cachers know how to store and stream sparkline PNG data.
:handler - Handler instances know how to fetch data and pass them 
  to the Sparklines library.


27
28
29
30
# File 'lib/sparkplug.rb', line 27

def initialize(app, options = {})
  @app, @options   = app, options
  @options[:spark] = DEFAULT_SPARK_OPTIONS.merge(@options[:spark] || {})
end

Instance Method Details

#_call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sparkplug.rb', line 36

def _call(env)
  if env['PATH_INFO'][@options[:prefix]] == @options[:prefix]
    @data_path = env['PATH_INFO'][@options[:prefix].size+1..-1]
    @data_path.sub! /\.png$/, ''
    @png_path = @data_path + ".png"
    @cacher   = @options[:cacher].set(@png_path)
    @handler  = @options[:handler].set(@data_path)
    if !@handler.exists?
      return @app.call(env)
    end
    if !@handler.already_cached?(@cacher)
      @handler.fetch do |data|
        @cacher.save(data, @options[:spark])
      end
    end
    @cacher.serve(self)
  else
    @app.call(env)
  end
end

#call(env) ⇒ Object



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

def call(env)
  dup._call(env)
end

#eachObject



57
58
59
# File 'lib/sparkplug.rb', line 57

def each
  @cacher.stream { |part| yield part }
end