candies 0.0.2

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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in candies.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Marcin Ciunelis
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.md ADDED
@@ -0,0 +1,153 @@
1
+ # Candies[![travis-ci](https://secure.travis-ci.org/martinciu/candies.png?branch=master)](http://travis-ci.org/martinciu/candies)
2
+ Invisible image based tracing service with Redis backend
3
+
4
+ ## Requirements
5
+
6
+ Candies uses redis as a datastore.
7
+
8
+ Candies only supports redis 2.0 or greater.
9
+
10
+ If you're on OS X, Homebrew is the simplest way to install Redis:
11
+
12
+ $ brew install redis
13
+ $ redis-server /usr/local/etc/redis.conf
14
+
15
+ You now have a Redis daemon running on 6379.
16
+
17
+ ## Setup
18
+
19
+ If you are using bundler add candies to your Gemfile:
20
+
21
+ gem 'candies'
22
+
23
+ Then run:
24
+
25
+ bundle install
26
+
27
+ Otherwise install the gem:
28
+
29
+ gem install candies
30
+
31
+ and require it in your project:
32
+
33
+ require 'candies'
34
+
35
+ ## Usage
36
+
37
+ ### Standalone service
38
+
39
+ Candies can be deployed as a standalone service (for example to heroku). To do it create a simple `config.ru` file:
40
+
41
+ require 'rubygems'
42
+ require 'candies'
43
+
44
+ # Candies.redis = ENV['REDISTOGO_URL'], read below about redis configuration
45
+
46
+ run Candies::Server.new
47
+
48
+ and deploy it to any `rack` compatybile environment (passenger, thin, unicorn, etc.)
49
+
50
+ ### Mounted to the Rails app
51
+
52
+ Mount Candies in your `config/routes.rb` file:
53
+
54
+ mount Candies::Server.new => "candies", :as => "candies"
55
+
56
+ It will be available under `http://yourapproot.tld/candies` url
57
+
58
+ ### Image tag
59
+
60
+ When you have Candies server deployed you can add tracking payload by including `img` tag in your HTML code:
61
+
62
+ <a href="http://candies.tld/anyfile.gif?id=tracing-id&foo=bar&baz=foo" />
63
+
64
+ or if you mounted candies in your rails app as `candies` then it will be:
65
+
66
+ <a href="http://yourapproot.tld/candies/anyfile.gif?id=tracing-id&foo=bar&baz=foo" />
67
+
68
+ Also in rails app you can use `candies_image_tag` helper. To do so you have to set `Candies.url` to point to the candies service url. The best way is to put in `config/initializers/candies.rb` file:
69
+
70
+ Candies.host = "http://yourapproot.tld/candies"
71
+
72
+ Now you can use `candies_image_tag` helper in controller views and in mailer views. Example:
73
+
74
+ <%= candies_image_tag(:id => "anyone@example.com", :email_type => "hello") %>
75
+
76
+ Note that `id` parameter is required. It will be used to create a redis key under which paload will be stored. In this case redis key will be: `candies:anyone@example.com:2011-11-10T13:13:09+01:00` and value: `"{\"email_type\":\"hello\"}"`. If you don't specify `id` parameter not value will be stored. Invisible image will by served anyway.
77
+
78
+ ## Configuration
79
+
80
+ ### Redis
81
+
82
+ You may want to change the Redis host and port Candies connects to, or
83
+ set various other options at startup.
84
+
85
+ Candies has a `redis` setter which can be given a string or a Redis
86
+ object. This means if you're already using Redis in your app, Candies
87
+ can re-use the existing connection.
88
+
89
+ String: `Candies.redis = 'localhost:6379'`
90
+
91
+ Redis: `Candies.redis = $redis`
92
+
93
+ For our rails app we have a `config/initializers/candies.rb` file where
94
+ we load `config/candies.yml` by hand and set the Redis information
95
+ appropriately.
96
+
97
+ Here's our `config/redis.yml`:
98
+
99
+ development: localhost:6379
100
+ test: localhost:6379
101
+ staging: redis1.example.com:6379
102
+ fi: localhost:6379
103
+ production: redis1.example.com:6379
104
+
105
+ And our initializer:
106
+
107
+ rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
108
+ rails_env = ENV['RAILS_ENV'] || 'development'
109
+
110
+ redis_config = YAML.load_file(rails_root + '/config/redis.yml')
111
+ Candies.redis = redis_config[rails_env]
112
+
113
+ ## Namespaces
114
+
115
+ If you're running multiple, separate instances of candies you may want
116
+ to namespace the keyspaces so they do not overlap. This is not unlike
117
+ the approach taken by many memcached clients.
118
+
119
+ This feature is provided by the [redis-namespace][rs] library, which
120
+ candies uses by default to separate the keys it manages from other keys
121
+ in your Redis server.
122
+
123
+ Simply use the `Candies.redis.namespace` accessor:
124
+
125
+ Candies.redis.namespace = "candies:blog"
126
+
127
+ We recommend sticking this in your initializer somewhere after Redis
128
+ is configured.
129
+
130
+ ## Results
131
+
132
+ There isn't any dashboard for displaying values (yet). You cen review them by logging into `redis-cli`. Sorry.
133
+
134
+ ## Development
135
+
136
+ Source hosted at [GitHub](http://github.com/martinciu/candies).
137
+ Report Issues/Feature requests on [GitHub Issues](http://github.com/martinciu/candies/issues).
138
+
139
+ Tests can be ran with `rake test`
140
+
141
+ ### Note on Patches/Pull Requests
142
+
143
+ * Fork the project.
144
+ * Make your feature addition or bug fix.
145
+ * Add tests for it. This is important so I don't break it in a
146
+ future version unintentionally.
147
+ * Commit, do not mess with rakefile, version, or history.
148
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
149
+ * Send me a pull request. Bonus points for topic branches.
150
+
151
+ ## Copyright
152
+
153
+ Copyright (c) 2011 Marcin Ciunelis. See [LICENSE](https://github.com/martinciu/candies/blob/master/LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'rake/testtask'
3
+ require 'bundler/gem_tasks'
4
+ Bundler.setup(:default, :test)
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "spec"
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
data/candies.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/candies/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Marcin Ciunelis"]
6
+ gem.email = ["marcin.ciunelis@gmail.com"]
7
+ gem.description = %q{Invisible image based tracing service with Redis backend}
8
+ gem.summary = %q{}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "candies"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Candies::VERSION
17
+
18
+ gem.add_dependency "rack", "~> 1.3.4"
19
+ gem.add_dependency 'redis', '~> 2.2.2'
20
+ gem.add_dependency 'redis-namespace', '>= 1.0.3'
21
+ gem.add_dependency "json", ">= 1.5.3"
22
+
23
+ gem.add_development_dependency "rake", "~> 0.9.2"
24
+ gem.add_development_dependency "minitest", "~> 2.7.0"
25
+ gem.add_development_dependency "rack-test", "~> 0.6"
26
+ gem.add_development_dependency "turn", "~> 0.8.3"
27
+ gem.add_development_dependency "mocha", "~> 0.10.0"
28
+ gem.add_development_dependency 'timecop', '~> 0.3.5'
29
+
30
+ end
data/example/config.ru ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require
5
+
6
+ require 'candies'
7
+
8
+ # Candies.redis = ENV['REDISTOGO_URL']
9
+
10
+ run Candies::Server.new
data/lib/candies.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'redis/namespace'
2
+ require 'time'
3
+ require 'json'
4
+
5
+ require "candies/server"
6
+ require "candies/helper"
7
+
8
+ module Candies
9
+ extend self
10
+
11
+ attr_accessor :url
12
+
13
+ # Accepts:
14
+ # 1. A 'hostname:port' String
15
+ # 2. A 'hostname:port:db' String (to select the Redis db)
16
+ # 3. A 'hostname:port/namespace' String (to set the Redis namespace)
17
+ # 4. A Redis URL String 'redis://host:port'
18
+ # 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
19
+ # or `Redis::Namespace`.
20
+ def redis=(server)
21
+ case server
22
+ when String
23
+ if server =~ /redis\:\/\//
24
+ redis = Redis.connect(:url => server, :thread_safe => true)
25
+ else
26
+ server, namespace = server.split('/', 2)
27
+ host, port, db = server.split(':')
28
+ redis = Redis.new(:host => host, :port => port,
29
+ :thread_safe => true, :db => db)
30
+ end
31
+ namespace ||= :candies
32
+
33
+ @redis = Redis::Namespace.new(namespace, :redis => redis)
34
+ when Redis::Namespace
35
+ @redis = server
36
+ else
37
+ @redis = Redis::Namespace.new(:candies, :redis => server)
38
+ end
39
+ end
40
+
41
+ # Returns the current Redis connection. If none has been created, will
42
+ # create a new one.
43
+ def redis
44
+ return @redis if @redis
45
+ self.redis = Redis.respond_to?(:connect) ? Redis.connect : "localhost:6379"
46
+ self.redis
47
+ end
48
+
49
+ def redis_id
50
+ # support 1.x versions of redis-rb
51
+ if redis.respond_to?(:server)
52
+ redis.server
53
+ else
54
+ redis.client.id
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ if defined?(Rails)
61
+ class ActionController::Base
62
+ ActionController::Base.send :include, Candies::Helper
63
+ ActionController::Base.helper Candies::Helper
64
+ end
65
+ class ActionMailer::Base
66
+ ActionMailer::Base.send :include, Candies::Helper
67
+ ActionMailer::Base.helper Candies::Helper
68
+ end
69
+ end
70
+
@@ -0,0 +1,7 @@
1
+ module Candies
2
+ module Helper
3
+ def candies_image_tag(args)
4
+ image_tag("#{Candies.url}/t.gif?#{args.to_query}", :alt => "", :width => 1, :height => 1) if Candies.url
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ module Candies
2
+ class Server
3
+ def call(env)
4
+ req = Rack::Request.new(env)
5
+ id = req.params.delete("id")
6
+ Candies.redis.set([id, Time.now.iso8601].join(":"), req.params.to_json) if !id.nil?
7
+ [200, headers, [image]]
8
+ end
9
+
10
+ private
11
+ def headers
12
+ {
13
+ "Content-Type" => "image/gif",
14
+ "Content-Length" => "35",
15
+ "Cache-Control" => "no-store, no-cache, must-revalidate, private",
16
+ "Pragma" => "no-cache",
17
+ "Expires" => "Sat, 25 Nov 2000 05:00:00 GMT"
18
+ }
19
+ end
20
+
21
+ def image
22
+ "GIF89a\u0001\u0000\u0001\u0000\x80\xFF\u0000\xFF\xFF\xFF\u0000\u0000\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0002D\u0001\u0000;"
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Candies
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Candies::Server do
4
+ include Rack::Test::Methods
5
+
6
+ before do
7
+ @redis = mock()
8
+ Candies.stubs(:redis).returns(@redis)
9
+ end
10
+
11
+ after(:each) do
12
+ Timecop.return
13
+ end
14
+
15
+ def app
16
+ @app ||= Candies::Server.new
17
+ end
18
+
19
+ it "get /t.gif?id=info@example.com&foo=bar adds record" do
20
+ now = Time.now
21
+ Timecop.freeze(now)
22
+ @redis.expects(:set).with(["info@example.com", now.iso8601].join(":"), {:foo => "bar"}.to_json)
23
+ get '/t.gif?id=info@example.com&foo=bar'
24
+ last_response.status.must_equal 200
25
+ last_response['Content-Type'].must_equal 'image/gif'
26
+ end
27
+
28
+ it "get /t.gif?&foo=bar is ignored" do
29
+ now = Time.now
30
+ Timecop.freeze(now)
31
+ @redis.expects(:set).never
32
+ get '/t.gif?&foo=bar'
33
+ last_response.status.must_equal 200
34
+ last_response['Content-Type'].must_equal 'image/gif'
35
+ end
36
+
37
+ it "get /t.gif?id=info@example.com adds record with empty payload" do
38
+ now = Time.now
39
+ Timecop.freeze(now)
40
+ @redis.expects(:set).with(["info@example.com", now.iso8601].join(":"), {}.to_json)
41
+ get '/t.gif?id=info@example.com'
42
+ last_response.status.must_equal 200
43
+ last_response['Content-Type'].must_equal 'image/gif'
44
+ end
45
+
46
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup(:default, :test)
4
+ Bundler.require(:default, :test)
5
+
6
+ require 'minitest/spec'
7
+ require 'minitest/autorun'
8
+ require 'turn'
9
+ require 'mocha'
10
+ require 'rack/test'
11
+ require 'timecop'
12
+
13
+ dir = File.dirname(File.expand_path(__FILE__))
14
+ $LOAD_PATH.unshift dir + '/../lib'
15
+ $TESTING = true
16
+
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: candies
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcin Ciunelis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &70237840036260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70237840036260
25
+ - !ruby/object:Gem::Dependency
26
+ name: redis
27
+ requirement: &70237840035400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.2.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70237840035400
36
+ - !ruby/object:Gem::Dependency
37
+ name: redis-namespace
38
+ requirement: &70237840034480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.3
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70237840034480
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: &70237840032740 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.3
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70237840032740
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &70237840047340 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.9.2
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70237840047340
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: &70237840045180 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.7.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70237840045180
80
+ - !ruby/object:Gem::Dependency
81
+ name: rack-test
82
+ requirement: &70237840042840 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '0.6'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70237840042840
91
+ - !ruby/object:Gem::Dependency
92
+ name: turn
93
+ requirement: &70237840039340 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 0.8.3
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70237840039340
102
+ - !ruby/object:Gem::Dependency
103
+ name: mocha
104
+ requirement: &70237840054200 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.10.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70237840054200
113
+ - !ruby/object:Gem::Dependency
114
+ name: timecop
115
+ requirement: &70237840052480 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ~>
119
+ - !ruby/object:Gem::Version
120
+ version: 0.3.5
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70237840052480
124
+ description: Invisible image based tracing service with Redis backend
125
+ email:
126
+ - marcin.ciunelis@gmail.com
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - .gitignore
132
+ - .travis.yml
133
+ - Gemfile
134
+ - LICENSE
135
+ - README.md
136
+ - Rakefile
137
+ - candies.gemspec
138
+ - example/config.ru
139
+ - lib/candies.rb
140
+ - lib/candies/helper.rb
141
+ - lib/candies/server.rb
142
+ - lib/candies/version.rb
143
+ - spec/candies/server_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: ''
146
+ licenses: []
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ segments:
158
+ - 0
159
+ hash: -4015693725382346057
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ segments:
167
+ - 0
168
+ hash: -4015693725382346057
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 1.8.10
172
+ signing_key:
173
+ specification_version: 3
174
+ summary: ''
175
+ test_files:
176
+ - spec/candies/server_spec.rb
177
+ - spec/spec_helper.rb