Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add abstract cacher

  • Loading branch information...
commit c24d79568948bcb4b48ab69b2a9bd14a8dd6eed3 1 parent b758f65
@technoweenie authored
View
6 lib/rack-sparklines.rb
@@ -37,11 +37,7 @@ def _call(env)
@cacher.save(data, @options[:spark])
end
end
- [200, {
- "Last-Modified" => @cacher.updated_at.rfc822,
- "Content-Type" => "image/png",
- "Content-Length" => @cacher.size.to_s
- }, self]
+ @cacher.serve(self)
else
@app.call(env)
end
View
53 lib/rack-sparklines/cachers/abstract.rb
@@ -0,0 +1,53 @@
+require 'rack-sparklines'
+require 'time'
+
+class Rack::Sparklines
+ module Cachers
+ # Reads sparkline data from CSV files. Only the first line of numbers are
+ # read. Requests for "/sparks/stats.csv" will pass a data_path of "stats.csv"
+ class Abstract
+ attr_accessor :png_path
+
+ def initialize
+ @size, @updated_at = nil
+ end
+
+ # Setting the png_path returns a duplicate of this object that has any
+ # custom instance variables (configuration settings, for example).
+ def set(png_path)
+ cacher = dup
+ cacher.png_path = png_path
+ cacher
+ end
+
+ def size
+ raise NotImplementedError
+ end
+
+ def exists?
+ raise NotImplementedError
+ end
+
+ def updated_at
+ raise NotImplementedError
+ end
+
+ def serve(app, headers = {})
+ headers = {
+ "Last-Modified" => updated_at.rfc822,
+ "Content-Type" => "image/png",
+ "Content-Length" => size.to_s
+ }.update(headers)
+ [200, headers, app]
+ end
+
+ def save(data, options)
+ raise NotImplementedError
+ end
+
+ def stream
+ raise NotImplementedError
+ end
+ end
+ end
+end
View
16 lib/rack-sparklines/cachers/filesystem.rb
@@ -1,26 +1,16 @@
-require 'rack-sparklines'
+require 'rack-sparklines/cachers/abstract'
require 'fileutils'
-require 'time'
class Rack::Sparklines
module Cachers
# Reads sparkline data from CSV files. Only the first line of numbers are
# read. Requests for "/sparks/stats.csv" will pass a data_path of "stats.csv"
- class Filesystem
+ class Filesystem < Abstract
attr_accessor :png_path
- attr_accessor :directory
def initialize(directory)
@directory = directory
- @size, @updated_at = nil
- end
-
- # Setting the png_path returns a duplicate of this object that has any
- # custom instance variables (configuration settings, for example).
- def set(png_path)
- cacher = dup
- cacher.png_path = png_path
- cacher
+ super()
end
def png_path=(s)
Please sign in to comment.
Something went wrong with that request. Please try again.