bruce-bumpspark 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bruce Williams (and collaborators at RedHanded)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Bumpspark
2
+
3
+ A modified copy of _why's `bumpspark' code, originally discussed and
4
+ collaborated on at [RedHanded] [1], built out a gem suitable for
5
+ inclusion in Rails projects (and standalone Ruby code).
6
+
7
+ This version of the library generates a transparent PNG.
8
+
9
+ Many thanks to the various collaborators:
10
+ * _why (concept, BMP implementation)
11
+ * jzp (png)
12
+ * MenTaLguY (transparency)
13
+
14
+ == Note on Patches/Pull Requests
15
+
16
+ * Fork the project.
17
+ * Make your feature addition or bug fix.
18
+ * Add tests for it. This is important so I don't break it in a
19
+ future version unintentionally.
20
+ * Commit, do not mess with rakefile, version, or history.
21
+ (if you want to have your own version, that is fine but
22
+ bump version in a commit by itself I can ignore when I pull)
23
+ * Send me a pull request. Bonus points for topic branches.
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2009 Bruce Williams, et al. See LICENSE for details.
28
+
29
+ [1]: http://redhanded.hobix.com/inspect/sparklinesForMinimalists.html
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "bumpspark"
8
+ gem.summary = %Q{Generates "bumpspark"-style sparklines for Ruby & Rails}
9
+ gem.description = %Q{Generates transparent PNG "bumpspark"-style sparklines. Use from Ruby directly or as a Rails helper generating an image tag w/ built-in data, as conceived by whytheluckystiff.}
10
+ gem.email = "bruce@codefluency.com"
11
+ gem.homepage = "http://github.com/bruce/bumpspark"
12
+ gem.authors = ["Bruce Williams"]
13
+ # TESTING ONLY
14
+ gem.add_development_dependency "thoughtbot-shoulda"
15
+ gem.add_development_dependency "activesupport"
16
+ gem.add_development_dependency "actionpack"
17
+ gem.add_development_dependency "rmagick"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ if File.exist?('VERSION')
51
+ version = File.read('VERSION')
52
+ else
53
+ version = ""
54
+ end
55
+
56
+ rdoc.rdoc_dir = 'rdoc'
57
+ rdoc.title = "bumpspark #{version}"
58
+ rdoc.rdoc_files.include('README*')
59
+ rdoc.rdoc_files.include('lib/**/*.rb')
60
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) << "/bumpspark/formats/png"
2
+ require File.dirname(__FILE__) << "/bumpspark/graph"
3
+
4
+ module Bumpspark
5
+ VERSION = File.read(File.dirname(__FILE__) << "/../VERSION").strip
6
+ end
@@ -0,0 +1,63 @@
1
+ require 'zlib'
2
+
3
+ module Bumpspark
4
+
5
+ module Formats
6
+
7
+ module PNG
8
+
9
+ BLACK = [0, 0, 0]
10
+ RED = [0xFF, 0, 0]
11
+ GREY = [0x99, 0x99, 0x99]
12
+
13
+ HEADER = [137, 80, 78, 71, 13, 10, 26, 10].pack("C*")
14
+
15
+ # Generate a PNG
16
+ def to_png
17
+ generate_png
18
+ end
19
+
20
+ private
21
+
22
+ def rows
23
+ normalized_numbers.inject([]) { |ary, r|
24
+ ary << [BLACK] * 15 << [BLACK] * 15
25
+ ary.last[r / 9,4] = [(r > 50 and RED or GREY)] * 4
26
+ ary
27
+ }.transpose.reverse
28
+ end
29
+
30
+ def generate_png
31
+ raw_data = rows.map { |row| [0] + row }.flatten.pack("C*")
32
+ ihdr_data = [
33
+ rows.first.length,rows.length,
34
+ 8,2,0,0,0
35
+ ].pack("NNCCCCC")
36
+ ihdr = chunk("IHDR", ihdr_data)
37
+ trns = chunk("tRNS", ([ 0 ]*6).pack("C6"))
38
+ idat = chunk("IDAT", Zlib::Deflate.deflate(raw_data))
39
+ iend = chunk("IEND", "")
40
+ HEADER + ihdr + trns + idat + iend
41
+ end
42
+
43
+ def chunk(type, data)
44
+ to_check = type + data
45
+ [data.length].pack("N") + to_check + [Zlib.crc32(to_check)].pack("N")
46
+ end
47
+
48
+ def normalized_numbers
49
+ nums = numbers.empty? ? [0] : numbers
50
+ min, max = nums.min, nums.max
51
+ width = max - min
52
+ return [1] * nums.size if width == 0
53
+ width += (300 * 1000)
54
+ nums.map do |result|
55
+ ((result - min) * 100 / width.to_f).to_i
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,14 @@
1
+ module Bumpspark
2
+
3
+ class Graph
4
+
5
+ include Formats::PNG
6
+
7
+ attr_reader :numbers
8
+ def initialize(numbers)
9
+ @numbers = numbers
10
+ end
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'base64'
2
+
3
+ module BumpsparkHelper
4
+
5
+ # Generate a "bumpspark"-style sparkline image tag
6
+ #
7
+ # call-seq:
8
+ # <%= bumpspark_tag([20, 23, 12, 23]) %>
9
+ def bumpspark_tag(numbers, html_opts={})
10
+ graph = Bumpspark::Graph.new(numbers)
11
+ tag(:img, html_opts.merge(:src => bumpspark_tag_src(graph)))
12
+ end
13
+
14
+ def bumpspark_tag_src(graph) #:nodoc:
15
+ data = Base64.encode64(graph.to_png).delete("\n")
16
+ return "data:image/png;base64,#{CGI.escape(data)}"
17
+ end
18
+
19
+ end
@@ -0,0 +1 @@
1
+ ApplicationHelper.send(:include, BumpsparkHelper)
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ require 'activesupport'
4
+ require 'action_view'
5
+ require 'action_controller'
6
+
7
+ require 'bumpspark_helper'
8
+
9
+ BumpsparkHelper.send(:include, ActionView::Helpers::TagHelper)
10
+ BumpsparkHelper.send(:include, ActionView::Helpers::AssetTagHelper)
11
+
12
+ class BumpsparkHelperTest < Test::Unit::TestCase
13
+
14
+ include BumpsparkHelper
15
+ include ActionView::Helpers::AssetTagHelper
16
+
17
+ should "generate an image" do
18
+ tag = bumpspark_tag([1, 2, 3])
19
+ assert tag.include?('<img'), tag
20
+ assert tag.include?('src'), tag
21
+ end
22
+
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+ require 'rmagick'
3
+
4
+ class BumpsparkTest < Test::Unit::TestCase
5
+
6
+ context "generating pngs" do
7
+
8
+ should "work for an empty bumpspark" do
9
+ assert_valid_png []
10
+ end
11
+
12
+ should "work for a bumpspark with one datapoint" do
13
+ assert_valid_png [1]
14
+ end
15
+
16
+ should "work for a bumpspark with multiple datapoints" do
17
+ assert_valid_png [1, 2, 3]
18
+ end
19
+
20
+ end
21
+
22
+ def assert_valid_png(data)
23
+ image = Magick::Image.from_blob(generate(data)).first
24
+ assert image, "No valid image generated"
25
+ assert_equal "image/png", image.mime_type, "Image has wrong mime type"
26
+ end
27
+
28
+ def generate(data, format=:png)
29
+ Bumpspark::Graph.new(data).send("to_#{format}")
30
+ end
31
+
32
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'bumpspark'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bruce-bumpspark
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruce Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: actionpack
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rmagick
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Generates transparent PNG "bumpspark"-style sparklines. Use from Ruby directly or as a Rails helper generating an image tag w/ built-in data, as conceived by whytheluckystiff.
56
+ email: bruce@codefluency.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.markdown
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.markdown
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/bumpspark.rb
72
+ - lib/bumpspark/formats/png.rb
73
+ - lib/bumpspark/graph.rb
74
+ - lib/bumpspark_helper.rb
75
+ - lib/rails/init.rb
76
+ - test/bumpspark_helper_test.rb
77
+ - test/bumpspark_test.rb
78
+ - test/test_helper.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/bruce/bumpspark
81
+ licenses:
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.5
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Generates "bumpspark"-style sparklines for Ruby & Rails
106
+ test_files:
107
+ - test/bumpspark_helper_test.rb
108
+ - test/bumpspark_test.rb
109
+ - test/test_helper.rb