url_job 0.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.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in url_job.gemspec
4
+ gemspec
data/NOTES ADDED
@@ -0,0 +1,3 @@
1
+ 0.1.0
2
+ - Ability to create jobs
3
+ - Url redirects / render (text)
@@ -0,0 +1,83 @@
1
+ = UrlJob
2
+
3
+ Rails gem that provides an engine which allows for creating urls that execute jobs
4
+
5
+ == Install
6
+
7
+ gem install url_job
8
+
9
+ == Usage
10
+
11
+ Lets assume you have an class like the following:
12
+
13
+ class Redirector < Struct.new(:redirect_to)
14
+ def initialize(redirect)
15
+ self.redirect_to = redirect
16
+ end
17
+
18
+ def perform(args={})
19
+ self
20
+ end
21
+ end
22
+
23
+ By creating a UrlJob using the following:
24
+ uj = UrlJob::Job.from_object(Redirector.new("http://frison.ca"))
25
+
26
+ And visiting:
27
+ http://localhost:3000/uj/#{uj.token}
28
+
29
+ The result will be deserializing the Redirector job, and calling it's perform method.
30
+ The perform method should return an object that responds to either: 'redirect_to' or 'render', and the corresponding action will occur. The perform method will be passed in a dictionary with the following keys:
31
+ :ip_address => The ip address of the url visitor
32
+ :user_agent => The user agent of the url visitor
33
+ :referrer => The referrer of the url visitor
34
+ :token => The token used to visit the url.
35
+
36
+ A more interesting example might be an object like:
37
+
38
+ class Tracker
39
+ def initialize
40
+ end
41
+
42
+ def perform(params={})
43
+ @url_job = UrlJob::Job.find_by_token(params[:token])
44
+ UrlTrack.create(:url_job => @url_job,
45
+ :referrer => params[:referrer],
46
+ :ip_address => params[:ip_address],
47
+ :user_agent => params[:user_agent])
48
+
49
+ OpenStruct.new({:render => " "})
50
+ end
51
+ end
52
+
53
+ This way you could do something like:
54
+ uj = UrlJob::Job.from_object(Tracker.new)
55
+
56
+ And embed a image tag like:
57
+ <img src="http://localhost:3000/uj/#{uj.token}" height="1px" width="1px">
58
+
59
+ Inside some emails to track some email opens
60
+
61
+ == Licence
62
+
63
+ Copyright (c) 2011 Timothy Frison
64
+
65
+ Permission is hereby granted, free of charge, to any person obtaining
66
+ a copy of this software and associated documentation files (the
67
+ "Software"), to deal in the Software without restriction, including
68
+ without limitation the rights to use, copy, modify, merge, publish,
69
+ distribute, sublicense, and/or sell copies of the Software, and to
70
+ permit persons to whom the Software is furnished to do so, subject to
71
+ the following conditions:
72
+
73
+ The above copyright notice and this permission notice shall be
74
+ included in all copies or substantial portions of the Software.
75
+
76
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
77
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
78
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
79
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
80
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
81
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
82
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
83
+
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,44 @@
1
+ module UrlJob
2
+ class UrlJobController < ApplicationController
3
+ before_filter :set_token
4
+
5
+ def perform
6
+ method_parameters = {:token => @token,
7
+ :user_agent => request.env['HTTP_USER_AGENT'],
8
+ :referrer => request.referrer,
9
+ :ip_address => request.env['REMOTE_ADDR']
10
+ }
11
+
12
+ delegator = UrlJob::Job.find_by_token(@token)
13
+
14
+ if delegator.nil? || delegator.limit_reached?
15
+ render_not_found
16
+ return
17
+ end
18
+
19
+
20
+ ac = delegator.payload_object.try(:perform, method_parameters)
21
+
22
+ if ac.respond_to?(:redirect_to) and ac.redirect_to
23
+ redirect_to ac.redirect_to
24
+ return
25
+ elsif ac.respond_to?(:render) and ac.render
26
+ render :text => ac.render
27
+ return
28
+ end
29
+
30
+ render_not_found
31
+ end
32
+
33
+
34
+ protected
35
+ def set_token
36
+ @token = params[:token]
37
+ render_not_found if !@token
38
+ end
39
+
40
+ def render_not_found
41
+ render :text => '404 Not Found', :status => :not_found
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do |map|
2
+ match '/uj/:token', :action => 'perform', :controller => "UrlJob/UrlJob"
3
+ end
@@ -0,0 +1,6 @@
1
+ module UrlJob
2
+ require 'url_job/engine' if defined?(Rails)
3
+
4
+ autoload :DeserializationError, 'url_job/deserialization_error'
5
+ autoload :Job, 'url_job/job'
6
+ end
@@ -0,0 +1,3 @@
1
+ module UrlJob
2
+ class DeserializationError < StandardError; end
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'url_job'
2
+ require 'rails'
3
+
4
+ module UrlJob
5
+ class Engine < Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ module UrlJob
2
+ require 'uniquify'
3
+
4
+ class Job < ActiveRecord::Base
5
+ set_table_name :url_jobs
6
+
7
+ uniquify :token do
8
+ SecureRandom.hex(40)
9
+ end
10
+
11
+
12
+
13
+ validates :handler, :presence => true
14
+
15
+ class << self
16
+ def from_object(object, &block)
17
+ unless object.respond_to?(:perform)
18
+ raise ArgumentError, 'Cannot create a UrlJob from an object that does not respond to perform'
19
+ end
20
+
21
+ create(:payload_object => object)
22
+ end
23
+ end
24
+
25
+ def limit_reached?
26
+ return false if no_limit?
27
+ action_count >= action_limit
28
+ end
29
+
30
+ def no_limit?
31
+ 0 == action_limit
32
+ end
33
+
34
+ ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/
35
+ def name
36
+ @name ||= payload_object.respond_to?(:display_name) ?
37
+ payload_object.display_name :
38
+ payload_object.class.name
39
+ rescue DeserializationError
40
+ ParseObjectFromYaml.match(handler)[1]
41
+ end
42
+
43
+ def payload_object=(object)
44
+ @payload_object = object
45
+ self.handler = object.to_yaml
46
+ end
47
+
48
+ def payload_object
49
+ @payload_object ||= YAML.load(self.handler)
50
+ rescue TypeError, LoadError, NameError, ArgumentError => e
51
+ raise DeserializationError,
52
+ "UrlJob failed to load: #{e.message}. Handler: #{handler.inspect}"
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module UrlJob
2
+ VERSION = "0.0.1".freeze
3
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "url_job/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "url_job"
7
+ s.version = UrlJob::VERSION.dup
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Timothy Frison"]
10
+ s.email = ["tim@frison.ca"]
11
+ s.homepage = "http://frison.ca/"
12
+ s.summary = %q{A rails engine for creating urls that run jobs}
13
+ s.description = <<-EOF
14
+ Need to track if an email was opened? If a link was clicked? If a page was viewed?
15
+ Too lazy to setup cron (or is it impossible?)?
16
+
17
+ Easy -- Just create a url_job that when visited does things (like the above) for you.
18
+ EOF
19
+
20
+ s.rubyforge_project = "url_job"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+
27
+ s.add_dependency('uniquify', '>= 0.1.0')
28
+ s.add_dependency('rails', '>= 3.0.0')
29
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url_job
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Timothy Frison
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-18 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: uniquify
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 0
34
+ version: 0.1.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rails
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 3
48
+ - 0
49
+ - 0
50
+ version: 3.0.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: " Need to track if an email was opened? If a link was clicked? If a page was viewed?\n Too lazy to setup cron (or is it impossible?)?\n\n Easy -- Just create a url_job that when visited does things (like the above) for you.\n"
54
+ email:
55
+ - tim@frison.ca
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - NOTES
66
+ - README.rdoc
67
+ - Rakefile
68
+ - app/controllers/url_job/url_job_controller.rb
69
+ - config/routes.rb
70
+ - lib/url_job.rb
71
+ - lib/url_job/deserialization_error.rb
72
+ - lib/url_job/engine.rb
73
+ - lib/url_job/job.rb
74
+ - lib/url_job/version.rb
75
+ - url_job.gemspec
76
+ has_rdoc: true
77
+ homepage: http://frison.ca/
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project: url_job
106
+ rubygems_version: 1.5.0
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: A rails engine for creating urls that run jobs
110
+ test_files: []
111
+