yandex_metrika 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -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 Andrey "Zed" Zaikin
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.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = yandex_metrika
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Andrey "Zed" Zaikin. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "yandex_metrika"
8
+ gem.summary = "[Rails] Easily enable Yandex.Metrika support in your Rails application."
9
+ gem.description = 'By default this gem will output Yandex.Metrika code for ' +
10
+ "every page automagically, if it's configured correctly. " +
11
+ "This is done by adding:\n" +
12
+ "Yandex::Metrika.counter_id = '123456'\n" +
13
+ 'to your `config/environment.rb`, inserting your own COUNTER_ID. ' +
14
+ 'This can be discovered by looking at the value of "new Ya.Metrika(123456)" ' +
15
+ 'in the Javascript code.'
16
+
17
+ gem.email = "zed.0xff@gmail.com"
18
+ gem.homepage = "http://github.com/zed-0xff/yandex_metrika"
19
+ gem.authors = ["Andrey \"Zed\" Zaikin"]
20
+ #gem.add_development_dependency "thoughtbot-shoulda"
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ gem.add_dependency 'actionpack', '>= 2.3.3'
23
+ gem.add_dependency 'activesupport', '>= 2.3.3'
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
27
+ end
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ begin
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/*_test.rb'
41
+ test.verbose = true
42
+ end
43
+ rescue LoadError
44
+ task :rcov do
45
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
+ end
47
+ end
48
+
49
+ task :test => :check_dependencies
50
+
51
+ task :default => :test
52
+
53
+ require 'rake/rdoctask'
54
+ Rake::RDocTask.new do |rdoc|
55
+ if File.exist?('VERSION')
56
+ version = File.read('VERSION')
57
+ else
58
+ version = ""
59
+ end
60
+
61
+ rdoc.rdoc_dir = 'rdoc'
62
+ rdoc.title = "yandex_metrika #{version}"
63
+ rdoc.rdoc_files.include('README*')
64
+ rdoc.rdoc_files.include('lib/**/*.rb')
65
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/lib/yandex.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Yandex #:nodoc:
2
+ end
@@ -0,0 +1,81 @@
1
+ require 'active_support'
2
+ require 'action_pack'
3
+ require 'action_view'
4
+
5
+ module Yandex # :nodoc:
6
+ class Metrika
7
+
8
+ # This module gets mixed in to ActionController::Base
9
+ module Mixin
10
+ # The javascript code to enable Yandex.Metrika on the current page.
11
+ # Normally you won't need to call this directly; the +add_metrika_code+
12
+ # after filter will insert it for you.
13
+ def metrika_code
14
+ Metrika.code if Metrika.enabled?(request.format)
15
+ end
16
+
17
+ # An after_filter to automatically add the metrika code.
18
+ def add_metrika_code
19
+ if Metrika.defer_load
20
+ response.body.sub! /<\/[bB][oO][dD][yY]>/, "#{metrika_code}</body>" if response.body.respond_to?(:sub!)
21
+ else
22
+ response.body.sub! /(<[bB][oO][dD][yY][^>]*>)/, "\\1#{metrika_code}" if response.body.respond_to?(:sub!)
23
+ end
24
+ end
25
+ end
26
+
27
+ class ConfigurationError < StandardError; end
28
+
29
+ @@counter_id = nil
30
+ ##
31
+ # :singleton-method:
32
+ # Specify the Yandex.Metrika COUNTER_ID for this web site. This can be found
33
+ # as the value of "new Ya.Metrika(123456)", where 123456 is the actual ID.
34
+ cattr_accessor :counter_id
35
+
36
+ @@environments = ['production']
37
+ ##
38
+ # :singleton-method:
39
+ # The environments in which to enable the Yandex.Metrika code. Defaults
40
+ # to 'production' only. Supply an array of environment names to change this.
41
+ cattr_accessor :environments
42
+
43
+ @@formats = [:html, :all]
44
+ ##
45
+ # :singleton-method:
46
+ # The request formats where tracking code should be added. Defaults to +[:html, :all]+. The entry for
47
+ # +:all+ is necessary to make Yandex recognize that tracking is installed on a
48
+ # site; it is not the same as responding to all requests. Supply an array
49
+ # of formats to change this.
50
+ cattr_accessor :formats
51
+
52
+ @@defer_load = true
53
+ ##
54
+ # :singleton-method:
55
+ # Set this to true (the default) if you want to load the Metrika javascript at
56
+ # the bottom of page. Set this to false if you want to load the Metrika
57
+ # javascript at the top of the page. The page will render faster if you set this to
58
+ # true.
59
+ cattr_accessor :defer_load
60
+
61
+ # Return true if the Yandex.Metrika system is enabled and configured
62
+ # correctly for the specified format
63
+ def self.enabled?(format)
64
+ raise ConfigurationError if counter_id.blank?
65
+ environments.include?(RAILS_ENV) && formats.include?(format.to_sym)
66
+ end
67
+
68
+ # Construct the javascript code to be inserted on the calling page.
69
+ def self.code
70
+ <<-EOHTML
71
+ <!-- Yandex.Metrika -->
72
+ <script src="//mc.yandex.ru/resource/watch.js" type="text/javascript"></script>
73
+ <script type="text/javascript">
74
+ try { var yaCounter#{counter_id} = new Ya.Metrika(#{counter_id}); } catch(e){}
75
+ </script>
76
+ <noscript><div style="position: absolute;"><img src="//mc.yandex.ru/watch/#{counter_id}" alt="" /></div></noscript>
77
+ <!-- /Yandex.Metrika -->
78
+ EOHTML
79
+ end
80
+ end
81
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'yandex/metrika'
2
+ ActionController::Base.send :include, Yandex::Metrika::Mixin
3
+ ActionController::Base.send :after_filter, :add_metrika_code
@@ -0,0 +1,7 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+
6
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/yandex/metrika.rb')
7
+
@@ -0,0 +1,122 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'mocha'
5
+ RAILS_ENV = 'test'
6
+
7
+ class TestMixin
8
+ class MockRequest
9
+ attr_accessor :format
10
+ end
11
+ class MockResponse
12
+ attr_accessor :body
13
+ end
14
+
15
+ include Yandex::Metrika::Mixin
16
+ attr_accessor :request, :response
17
+
18
+ def initialize
19
+ self.request = MockRequest.new
20
+ self.response = MockResponse.new
21
+ end
22
+
23
+ # override the mixin's method
24
+ def metrika_code
25
+ "Sample Code"
26
+ end
27
+ end
28
+
29
+
30
+ class MetrikaTest < Test::Unit::TestCase
31
+ def setup
32
+ @ga = Yandex::Metrika.new
33
+ @ga.counter_id = "the tracker id"
34
+ end
35
+
36
+ def test_createable
37
+ assert_not_nil(@ga)
38
+ end
39
+
40
+ def test_default_environments
41
+ assert_equal(false, @ga.environments.include?('test'))
42
+ assert_equal(false, @ga.environments.include?('development'))
43
+ assert_equal(true, @ga.environments.include?('production'))
44
+ end
45
+
46
+ def test_default_formats
47
+ assert_equal(false, @ga.formats.include?(:xml))
48
+ assert_equal(true, @ga.formats.include?(:html))
49
+ end
50
+
51
+ def test_defer_load_defaults_to_true
52
+ assert_equal(true, @ga.defer_load)
53
+ end
54
+
55
+ # test self.enabled
56
+ def test_enabled_requires_counter_id
57
+ Yandex::Metrika.stubs(:counter_id).returns(nil)
58
+ assert_raise(Yandex::Metrika::ConfigurationError) { Yandex::Metrika.enabled?(:html) }
59
+ end
60
+
61
+ def test_enabled_returns_false_if_current_environment_not_enabled
62
+ Yandex::Metrika.stubs(:environments).returns(['production'])
63
+ assert_equal(false, Yandex::Metrika.enabled?(:html))
64
+ end
65
+
66
+ def test_enabled_with_default_format
67
+ Yandex::Metrika.stubs(:environments).returns(['test'])
68
+ assert_equal(true, Yandex::Metrika.enabled?(:html))
69
+ end
70
+
71
+ def test_enabled_with_not_included_format
72
+ Yandex::Metrika.stubs(:environments).returns(['test'])
73
+ assert_equal(false, Yandex::Metrika.enabled?(:xml))
74
+ end
75
+
76
+ def test_enabled_with_added_format
77
+ Yandex::Metrika.stubs(:environments).returns(['test'])
78
+ Yandex::Metrika.stubs(:formats).returns([:xml])
79
+ assert_equal(true, Yandex::Metrika.enabled?(:xml))
80
+ end
81
+
82
+ # Test the before_filter method does what we expect by subsituting the body tags and inserting
83
+ # some code for us automagically.
84
+ def test_add_metrika_code
85
+ # setup our test mixin
86
+ mixin = TestMixin.new
87
+
88
+ # bog standard body tag
89
+ Yandex::Metrika.defer_load = false
90
+ mixin.response.body = "<body><p>some text</p></body>"
91
+ mixin.add_metrika_code
92
+ assert_equal mixin.response.body, '<body>Sample Code<p>some text</p></body>'
93
+
94
+ Yandex::Metrika.defer_load = true
95
+ mixin.response.body = "<body><p>some text</p></body>"
96
+ mixin.add_metrika_code
97
+ assert_equal mixin.response.body, '<body><p>some text</p>Sample Code</body>'
98
+
99
+ # body tag upper cased (ignoring this is semantically incorrect)
100
+ Yandex::Metrika.defer_load = false
101
+ mixin.response.body = "<BODY><p>some text</p></BODY>"
102
+ mixin.add_metrika_code
103
+ assert_equal mixin.response.body, '<BODY>Sample Code<p>some text</p></BODY>'
104
+
105
+ Yandex::Metrika.defer_load = true
106
+ mixin.response.body = "<BODY><p>some text</p></BODY>"
107
+ mixin.add_metrika_code
108
+ assert_equal mixin.response.body, '<BODY><p>some text</p>Sample Code</body>'
109
+
110
+ # body tag has additional attributes
111
+ Yandex::Metrika.defer_load = false
112
+ mixin.response.body = '<body style="background-color:red"><p>some text</p></body>'
113
+ mixin.add_metrika_code
114
+ assert_equal mixin.response.body, '<body style="background-color:red">Sample Code<p>some text</p></body>'
115
+
116
+ Yandex::Metrika.defer_load = true
117
+ mixin.response.body = '<body style="background-color:red"><p>some text</p></body>'
118
+ mixin.add_metrika_code
119
+ assert_equal mixin.response.body, '<body style="background-color:red"><p>some text</p>Sample Code</body>'
120
+ end
121
+
122
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{yandex_metrika}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andrey \"Zed\" Zaikin"]
12
+ s.date = %q{2009-10-12}
13
+ s.description = %q{By default this gem will output Yandex.Metrika code for every page automagically, if it's configured correctly. This is done by adding:
14
+ Yandex::Metrika.counter_id = '123456'
15
+ to your `config/environment.rb`, inserting your own COUNTER_ID. This can be discovered by looking at the value of "new Ya.Metrika(123456)" in the Javascript code.}
16
+ s.email = %q{zed.0xff@gmail.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/yandex.rb",
29
+ "lib/yandex/metrika.rb",
30
+ "rails/init.rb",
31
+ "test/test_helper.rb",
32
+ "test/yandex_metrika_test.rb",
33
+ "yandex_metrika.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/zed-0xff/yandex_metrika}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{[Rails] Easily enable Yandex.Metrika support in your Rails application.}
40
+ s.test_files = [
41
+ "test/yandex_metrika_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<actionpack>, [">= 2.3.3"])
51
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.3"])
52
+ else
53
+ s.add_dependency(%q<actionpack>, [">= 2.3.3"])
54
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<actionpack>, [">= 2.3.3"])
58
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yandex_metrika
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey "Zed" Zaikin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-12 00:00:00 +06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.3
34
+ version:
35
+ description: |-
36
+ By default this gem will output Yandex.Metrika code for every page automagically, if it's configured correctly. This is done by adding:
37
+ Yandex::Metrika.counter_id = '123456'
38
+ to your `config/environment.rb`, inserting your own COUNTER_ID. This can be discovered by looking at the value of "new Ya.Metrika(123456)" in the Javascript code.
39
+ email: zed.0xff@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ files:
48
+ - .document
49
+ - .gitignore
50
+ - LICENSE
51
+ - README.rdoc
52
+ - Rakefile
53
+ - VERSION
54
+ - lib/yandex.rb
55
+ - lib/yandex/metrika.rb
56
+ - rails/init.rb
57
+ - test/test_helper.rb
58
+ - test/yandex_metrika_test.rb
59
+ - yandex_metrika.gemspec
60
+ has_rdoc: true
61
+ homepage: http://github.com/zed-0xff/yandex_metrika
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: "[Rails] Easily enable Yandex.Metrika support in your Rails application."
88
+ test_files:
89
+ - test/yandex_metrika_test.rb
90
+ - test/test_helper.rb