esilverberg-google_otg 1.0.0 → 1.0.1

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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/google_otg.gemspec +2 -2
  3. data/lib/google_otg.rb +92 -0
  4. metadata +4 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/google_otg.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{google_otg}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["esilverberg"]
12
- s.date = %q{2009-09-03}
12
+ s.date = %q{2009-09-16}
13
13
  s.description = %q{Include Google's Over Time Graph in your app}
14
14
  s.email = %q{eric@ericsilverberg.com}
15
15
  s.extra_rdoc_files = [
data/lib/google_otg.rb CHANGED
@@ -19,7 +19,98 @@ pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-sh
19
19
  src="#{src}"/>
20
20
  eos
21
21
 
22
+ return html
23
+
22
24
  end
25
+
26
+ def google_pie(hits, label_fn, args = {})
27
+ height = args.has_key?(:height) ? args[:height] : 125
28
+ width = args.has_key?(:width) ? args[:width] : 125
29
+ pie_values = extract_pct_values(hits, label_fn, args)
30
+ vars = pie_to_flashvars(pie_values, args)
31
+ src = args.has_key?(:src) ? args[:src] : "http://www.google.com/analytics/static/flash/pie.swf"
32
+
33
+ html = <<-eos
34
+ <embed
35
+ width="#{width}"
36
+ height="#{height}"
37
+ salign="tl"
38
+ scale="noScale"
39
+ quality="high"
40
+ bgcolor="#FFFFFF"
41
+ flashvars="input=#{vars}&amp;locale=en-US"
42
+ pluginspage="http://www.macromedia.com/go/getflashplayer"
43
+ type="application/x-shockwave-flash"
44
+ src="#{src}"/>
45
+ eos
46
+ return html
47
+
48
+ end
49
+
50
+ def pie_to_flashvars(args = {})
51
+
52
+ labels = args[:labels]
53
+ raw_values = args[:raw_values]
54
+ percent_values = args[:percent_values]
55
+
56
+ options = {
57
+ :Pie => {
58
+ :Id => "Pie",
59
+ :Compare => false,
60
+ :HasOtherSlice => false,
61
+ :RawValues => raw_values,
62
+ :Format => "DASHBOARD",
63
+ :PercentValues => percent_values
64
+ }
65
+ }
66
+
67
+ return URI::encode(options.to_json)
68
+
69
+ end
70
+ protected :pie_to_flashvars
71
+
72
+ def extract_pct_values(hits, label_fn, args = {})
73
+
74
+ limit = args.has_key?(:limit) ? args[:limit] : 0.0
75
+
76
+ total = 0.0
77
+ other = 0.0
78
+ percent_values = []
79
+ raw_values = []
80
+ labels = []
81
+ values = []
82
+ hits.each{|hit|
83
+ total += hit.count.to_f
84
+ }
85
+ hits.each{|hit|
86
+ ct = hit.count.to_f
87
+ pct = (ct / total)
88
+
89
+ if pct > limit
90
+ percent_values.push([pct, sprintf("%.2f%%", pct * 100)])
91
+ raw_values.push([ct, ct])
92
+
93
+ label = label_fn.call(hit)
94
+ meta = args.has_key?(:meta) ? args[:meta].call(hit) : nil
95
+
96
+ labels.push(label)
97
+ values.push({:label => label, :meta => meta, :percent_value => [pct, sprintf("%.2f%%", pct * 100)], :raw_value => ct})
98
+ else
99
+ other += ct
100
+ end
101
+ }
102
+ if other > 0.0
103
+ pct = other / total
104
+ percent_values.push([pct, sprintf("%.2f%%", pct * 100)])
105
+ raw_values.push([other, other])
106
+ labels.push("Other")
107
+ values.push({:label => "Other", :percent_value => [pct, sprintf("%.2f%%", pct * 100)], :raw_value => other})
108
+ end
109
+
110
+ return {:labels => labels, :raw_values => raw_values, :percent_values => percent_values, :values => values}
111
+
112
+ end
113
+ protected :extract_pct_values
23
114
 
24
115
  def flto10(val)
25
116
  return ((val / 10) * 10).to_i
@@ -79,6 +170,7 @@ eos
79
170
  end
80
171
  end
81
172
 
173
+ max_y = args.has_key?(:max_y) ? (args[:max_y] > max_y ? args[:max_y] : max_y) : max_y
82
174
 
83
175
  mid_y = self.flto10(max_y / 2)
84
176
  top_y = self.flto10(max_y)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esilverberg-google_otg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - esilverberg
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-03 00:00:00 -07:00
12
+ date: 2009-09-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ files:
45
45
  - test/test_helper.rb
46
46
  has_rdoc: true
47
47
  homepage: http://github.com/esilverberg/google_otg
48
+ licenses:
48
49
  post_install_message:
49
50
  rdoc_options:
50
51
  - --charset=UTF-8
@@ -65,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
66
  requirements: []
66
67
 
67
68
  rubyforge_project:
68
- rubygems_version: 1.2.0
69
+ rubygems_version: 1.3.5
69
70
  signing_key:
70
71
  specification_version: 2
71
72
  summary: Google's amazing over-time graph, in your rails app