tophold_rack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p362@tophold_rack
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ # Specify your gem's dependencies in tophold_rack.gemspec
4
+
5
+ gem 'rack'
6
+
7
+ gem 'devise'
8
+
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 readline
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # TopholdRack
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tophold_rack'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tophold_rack
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,47 @@
1
+ #author: shreadline@gmail.com
2
+ #source http://github.com/tteng/tophold_rack
3
+ #since the tophold_rack depends on the popular authticate gem `devise`,
4
+ #which also based on the rack style auth middlware `warden`(for more detail go and
5
+ #reference:
6
+ # http://railscasts.com/episodes/305-authentication-with-warden
7
+ # http://railscasts.com/episodes/209-introducing-devise
8
+ #onece you invoke the devise generator, say you want user model authticateable
9
+ #~/your_console> rails g devise User
10
+ #which will generate migration file, model and tests etc, but the background mechanism
11
+ #is devise add a rack middleware to the rails middlewares stack, run `rake middleware`
12
+ #to get the difference around the above command.
13
+ #warden keeps the login session, and the seesion key is related with
14
+ #the model you want to include the devise code, which is called a `scope`,
15
+ #
16
+ # module Warden
17
+ # class SessionSerializer
18
+ # attr_reader :env
19
+ # include ::Warden::Mixins::Common
20
+ #
21
+ # def initialize(env)
22
+ # @env = env
23
+ # end
24
+ #
25
+ # def key_for(scope)
26
+ # "warden.user.#{scope}.key"
27
+ # end
28
+ # ... ...
29
+ #
30
+ #
31
+ # and the model name (here is 'user') is corresponding to the scope, and so on 'admin_user' for the admin/.
32
+ # Therefore, in this initializer set the 'tophold_rack_devise_scope' to your corresponding deviseable model
33
+ # name ('user' by default) and with this parameter tophold_rack
34
+ # will *know* the current_user/current_admin_user which helps to collect individual user behavior.
35
+ Rails.configuration.tophold_rack_devise_scope = "user"
36
+
37
+ #BTW, not all requests should be analyzed, such as static files and admin requests, by default
38
+ #tophold_rack will not collect request begin with "uploads", "assets",
39
+ #belowing requests will be ignored:
40
+ # http://somedomain.com/uploads/post/document/14/normal_786_938652_586928.jpg
41
+ # http://somedomain.com/assets/jquery-migrate-1.1.1.min.js?body=1
42
+ # http://somedomain.com/assets/xxx/xxx.*
43
+ #customize this to meet your requirments.
44
+ Rails.configuration.tophold_rack_request_black_list << "admin"
45
+
46
+ #3rd party tracking url
47
+ Rails.configuration.tophold_rack_tracking_url = "http://localhost:8888/tracking/"
@@ -0,0 +1,10 @@
1
+ require 'rails/generators'
2
+ class TopholdRackGenerator < Rails::Generators::Base
3
+
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def install
7
+ template "tophold_rack.rb", "config/initializers/tophold_rack.rb"
8
+ end
9
+
10
+ end
@@ -0,0 +1,24 @@
1
+ module TopholdRack
2
+
3
+ class Engine < Rails::Engine
4
+
5
+ config.tophold_rack_devise_scope = "user"
6
+
7
+ config.tophold_rack_request_black_list = ["uploads", "assets"]
8
+
9
+ config.tophold_rack_tracking_url = "http://localhost:8888/tracking/"
10
+
11
+ initializer "tophold_rack.load_app_instance_data" do |app|
12
+ TopholdRack.setup do |config|
13
+ config.app_root = app.root
14
+ end
15
+ end
16
+
17
+ initializer "tophold_rack.add_middleware_after_devise" do |app|
18
+ #app.middleware.insert_after ActionDispatch::Session::CookieStore, TopholdRack::Redispatcher
19
+ app.middleware.use TopholdRack::Redispatcher
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+
4
+ module TopholdRack
5
+
6
+ class Redispatcher
7
+
8
+ def initialize app
9
+ @app = app
10
+ end
11
+
12
+ def request_black_list
13
+ @black_list ||= /^\/(#{Rails.configuration.tophold_rack_request_black_list.join('|')})/
14
+ end
15
+
16
+ def call env
17
+ request = Rack::Request.new env
18
+ if request.get?
19
+ path, query = request.path, request.query_string
20
+ scope = env["rack.session"]["warden.user.#{Rails.configuration.tophold_rack_devise_scope}.key"]
21
+ user_id = scope ? scope[1][0] : nil
22
+ unless path =~ request_black_list
23
+ str = "#{path.blank? ? '/' : path}?query=#{query}&user_id=#{user_id}"
24
+ url = Rails.configuration.tophold_rack_tracking_url+"?request_url=#{CGI.escape str}"
25
+ open url
26
+ end
27
+ end
28
+ @app.call env #pass the buckets
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module TopholdRack
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "tophold_rack/version"
2
+
3
+ module TopholdRack
4
+
5
+ mattr_accessor :app_root
6
+
7
+ def self.setup
8
+ yield self
9
+ end
10
+
11
+ end
12
+
13
+ require "tophold_rack/engine"
14
+ require "tophold_rack/redispatcher"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tophold_rack/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tophold_rack"
8
+ gem.version = TopholdRack::VERSION
9
+ gem.authors = ["tteng"]
10
+ gem.email = ["tim.rubist@gmail.com"]
11
+ gem.description = %q{A rack based middleware to redispatch requested uri path to 3rd party tracking server}
12
+ gem.summary = %q{A rack based middleware to redispatch requested uri path to 3rd party tracking server}
13
+ gem.homepage = "http://github.com/tteng"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tophold_rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tteng
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A rack based middleware to redispatch requested uri path to 3rd party
15
+ tracking server
16
+ email:
17
+ - tim.rubist@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - .rvmrc
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/generators/tophold_rack/templates/tophold_rack.rb
29
+ - lib/generators/tophold_rack/tophold_rack_generator.rb
30
+ - lib/tophold_rack.rb
31
+ - lib/tophold_rack/engine.rb
32
+ - lib/tophold_rack/redispatcher.rb
33
+ - lib/tophold_rack/version.rb
34
+ - tophold_rack.gemspec
35
+ homepage: http://github.com/tteng
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A rack based middleware to redispatch requested uri path to 3rd party tracking
59
+ server
60
+ test_files: []