flan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in flan.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ # The MIT License
2
+
3
+ Copyright (c) 2011 Kristján Pétursson
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the "Software"), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ ## 1-2-3 Go
2
+
3
+ ### Controller
4
+
5
+ include Flan
6
+
7
+ def create
8
+ # ...
9
+ flan(:pageview, 'pudding/creation')
10
+ # ...
11
+ end
12
+
13
+ ### Layout
14
+
15
+ %head
16
+ -# Initialize Google Analytics
17
+ = flan_js
18
+
19
+ ### Generates
20
+
21
+ <script type="text/javascript">
22
+ _gaq.push(['_trackPageview', '/_virtual/pudding/creation']);
23
+ </script>
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/flan.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "flan/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "flan"
7
+ s.version = Flan::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Kristán Pétursson"]
10
+ s.email = ["kristjan@gmail.com`"]
11
+ s.homepage = "https://github.com/kristjan/flan"
12
+ s.summary = %q{FLash ANalytics}
13
+ s.description = %q{A server-client bridge to Google Analytics using Rails' flash}
14
+
15
+ s.rubyforge_project = "flan"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/lib/flan.rb ADDED
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), 'flan', 'controller_methods')
2
+ require File.join(File.dirname(__FILE__), 'flan', 'view_helpers')
3
+
4
+ module Flan
5
+ def self.included(base)
6
+ base.send(:include, Flan::ControllerMethods)
7
+ base.send(:helper, Flan::ViewHelpers)
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Flan
2
+ module ControllerMethods
3
+ private # Included in controllers, shouldn't be exposed as actions
4
+
5
+ def flan(type, name)
6
+ flash["flan_#{type}"] ||= []
7
+ flash["flan_#{type}"] << name
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Flan
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ module Flan
2
+ module ViewHelpers
3
+ def flan_command(key)
4
+ match = /flan_(.*)/.match(key.to_s)
5
+ raise ArgumentError.new("#{key} wasn't generated by Flan") unless match
6
+ "_track#{match[1].capitalize}"
7
+ end
8
+
9
+ def flan_js
10
+ js = []
11
+ flan_keys.each do |key|
12
+ flash[key].each do |page|
13
+ js << "_gaq.push(['#{flan_command(key)}', '#{flan_path(page)}']);"
14
+ end
15
+ end
16
+ js.empty? ? '' : content_tag(:script, js.join("\n"),
17
+ :type => 'text/javascript')
18
+ end
19
+
20
+ def flan_keys
21
+ flan_keys = flash.keys.select{|k| k.to_s =~ /^flan_/}
22
+ end
23
+
24
+ def flan_path(path)
25
+ "/_virtual/#{path}".gsub('//', '/')
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flan::ControllerMethods do
4
+ context "#flan" do
5
+ before(:all) do
6
+ TestController.send(:include, Flan)
7
+ TestController.send(:public, :flan)
8
+ end
9
+
10
+ before(:each) do
11
+ @controller = TestController.new
12
+ @controller.flan :pudding, "caramel"
13
+ end
14
+
15
+ it "sets the flash key" do
16
+ @controller.flash.should have_key('flan_pudding')
17
+ end
18
+
19
+ it "stores the value" do
20
+ @controller.flash['flan_pudding'].size.should == 1
21
+ @controller.flash['flan_pudding'].should include('caramel')
22
+ end
23
+
24
+ it "stores multiple values" do
25
+ @controller.flan :pudding, 'chocolate'
26
+ @controller.flash['flan_pudding'].size.should == 2
27
+ @controller.flash['flan_pudding'].should include('caramel')
28
+ @controller.flash['flan_pudding'].should include('chocolate')
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flan::ViewHelpers do
4
+ include Flan::ViewHelpers
5
+
6
+ def content_tag(tag, content, options={})
7
+ attrs = options.map{|k,v| %[#{k}="#{v}"]}.join(' ')
8
+ ["<#{tag}#{' ' + attrs if attrs.any?}>", content, "</#{tag}>"].join("\n")
9
+ end
10
+
11
+ context "#flan_command" do
12
+ it "generates the right GA command from a key" do
13
+ flan_command(:flan_pageview).should == '_trackPageview'
14
+ end
15
+
16
+ it "yells if the input isn't a Flan key" do
17
+ expect { flan_command(:notice) }.to raise_error(ArgumentError)
18
+ end
19
+ end
20
+
21
+ context "#flan_js" do
22
+ context "when there are no pageviews to track" do
23
+ let(:flash) do
24
+ {
25
+ :notice => "Flan is delicious!",
26
+ }
27
+ end
28
+
29
+ it "generates nothing" do
30
+ flan_js.should be_empty
31
+ end
32
+ end
33
+
34
+ context "when there are pageviews to track" do
35
+ let(:flash) do
36
+ {
37
+ :notice => "Flan is delicious!",
38
+ :flan_pageview => ["/flan/caramel", "flan/chocolate"],
39
+ }
40
+ end
41
+
42
+ it "generates a script tag" do
43
+ flan_js.should =~ %r{<script type="text/javascript">.*</script>}m
44
+ end
45
+
46
+ it "generates a push call for each page" do
47
+ flan_js.should(
48
+ include(%{_gaq.push(['_trackPageview', '/_virtual/flan/caramel']);}))
49
+ flan_js.should(
50
+ include(%{_gaq.push(['_trackPageview', '/_virtual/flan/chocolate']);}))
51
+ end
52
+ end
53
+ end
54
+
55
+ context "#flan_keys" do
56
+ let(:flash) do
57
+ {
58
+ :notice => "Flan is delicious!",
59
+ :flan_key_a => [],
60
+ :flan_key_b => [],
61
+ }
62
+ end
63
+
64
+ it "finds keys that start with flan_" do
65
+ flan_keys.should =~ [:flan_key_a, :flan_key_b]
66
+ end
67
+
68
+ it "ignores keys that do not start with flan_" do
69
+ flan_keys.should_not include(:notice)
70
+ end
71
+ end
72
+
73
+ context "#flan_path" do
74
+ it "prefixes a path with /_virtual" do
75
+ flan_path('desserts/caramel').should == '/_virtual/desserts/caramel'
76
+ end
77
+
78
+ it "squishes duplicate slashes" do
79
+ flan_path('/desserts/caramel').should == '/_virtual/desserts/caramel'
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flan do
4
+ context "including Flan" do
5
+ it "adds #flan as a private method" do
6
+ TestController.send(:include, Flan)
7
+ TestController.new.private_methods.should include('flan')
8
+ end
9
+
10
+ it "adds the ViewHelpers" do
11
+ TestController.should_receive(:helper).with(Flan::ViewHelpers)
12
+ TestController.send(:include, Flan)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ Dir['lib/**/*.rb'].each {|f| require f}
5
+
6
+ RSpec.configure do |config|
7
+ end
8
+
9
+ class TestController
10
+ def self.helper(helper)
11
+ # Pass
12
+ end
13
+
14
+ def flash
15
+ @flash ||= {}
16
+ end
17
+ end
18
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flan
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "Krist\xC3\xA1n P\xC3\xA9tursson"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-26 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A server-client bridge to Google Analytics using Rails' flash
23
+ email:
24
+ - kristjan@gmail.com`
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - flan.gemspec
39
+ - lib/flan.rb
40
+ - lib/flan/controller_methods.rb
41
+ - lib/flan/version.rb
42
+ - lib/flan/view_helpers.rb
43
+ - spec/lib/flan/controller_methods_spec.rb
44
+ - spec/lib/flan/view_helpers_spec.rb
45
+ - spec/lib/flan_spec.rb
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: https://github.com/kristjan/flan
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project: flan
77
+ rubygems_version: 1.5.3
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: FLash ANalytics
81
+ test_files:
82
+ - spec/lib/flan/controller_methods_spec.rb
83
+ - spec/lib/flan/view_helpers_spec.rb
84
+ - spec/lib/flan_spec.rb
85
+ - spec/spec_helper.rb