getformidable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.md +13 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/bin/formidable +4 -0
- data/getformidable.gemspec +80 -0
- data/init.rb +7 -0
- data/lib/formidable.rb +181 -0
- data/lib/formidable/attempt.rb +40 -0
- data/lib/formidable/commands.rb +68 -0
- data/lib/formidable/config.rb +54 -0
- data/lib/formidable/controller.rb +17 -0
- data/lib/formidable/model.rb +51 -0
- data/lib/formidable/railtie.rb +12 -0
- data/lib/formidable/remote.rb +35 -0
- data/lib/formidable/timer.rb +18 -0
- data/lib/formidable/view_helpers.rb +9 -0
- data/lib/getformidable.rb +1 -0
- data/rails/init.rb +1 -0
- data/spec/getformidable_spec.rb +7 -0
- data/spec/spec_helper.rb +12 -0
- metadata +137 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
json (1.4.6)
|
11
|
+
rake (0.8.7)
|
12
|
+
rcov (0.9.9)
|
13
|
+
rspec (2.3.0)
|
14
|
+
rspec-core (~> 2.3.0)
|
15
|
+
rspec-expectations (~> 2.3.0)
|
16
|
+
rspec-mocks (~> 2.3.0)
|
17
|
+
rspec-core (2.3.1)
|
18
|
+
rspec-expectations (2.3.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.3.0)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
bundler (~> 1.0.0)
|
27
|
+
jeweler (~> 1.5.2)
|
28
|
+
json
|
29
|
+
rcov
|
30
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Andrew Kane
|
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,13 @@
|
|
1
|
+
# Formidable
|
2
|
+
|
3
|
+
Real-time form analytics - track conversion rates, top errors, and confusing fields. See what's keeping you from maximizing user participation and revenue.
|
4
|
+
|
5
|
+
See [getformidable.com](http://www.getformidable.com) to get a free account.
|
6
|
+
|
7
|
+
## Documentation
|
8
|
+
|
9
|
+
[Rails 3](http://www.getformidable.com/docs/rails3)
|
10
|
+
|
11
|
+
[Rails 2](http://www.getformidable.com/docs/rails2)
|
12
|
+
|
13
|
+
[Ruby](http://www.getformidable.com/docs/ruby)
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "getformidable"
|
16
|
+
gem.homepage = "http://github.com/ankane/getformidable"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Formidable}
|
19
|
+
gem.description = %Q{Real-time form analytics}
|
20
|
+
gem.email = "andrew@getformidable.com"
|
21
|
+
gem.authors = ["Andrew Kane"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "getformidable #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/formidable
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
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{getformidable}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Andrew Kane"]
|
12
|
+
s.date = %q{2011-02-13}
|
13
|
+
s.default_executable = %q{formidable}
|
14
|
+
s.description = %q{Real-time form analytics}
|
15
|
+
s.email = %q{andrew@getformidable.com}
|
16
|
+
s.executables = ["formidable"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.md",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"bin/formidable",
|
29
|
+
"getformidable.gemspec",
|
30
|
+
"init.rb",
|
31
|
+
"lib/formidable.rb",
|
32
|
+
"lib/formidable/attempt.rb",
|
33
|
+
"lib/formidable/commands.rb",
|
34
|
+
"lib/formidable/config.rb",
|
35
|
+
"lib/formidable/controller.rb",
|
36
|
+
"lib/formidable/model.rb",
|
37
|
+
"lib/formidable/railtie.rb",
|
38
|
+
"lib/formidable/remote.rb",
|
39
|
+
"lib/formidable/timer.rb",
|
40
|
+
"lib/formidable/view_helpers.rb",
|
41
|
+
"lib/getformidable.rb",
|
42
|
+
"rails/init.rb",
|
43
|
+
"spec/getformidable_spec.rb",
|
44
|
+
"spec/spec_helper.rb"
|
45
|
+
]
|
46
|
+
s.homepage = %q{http://github.com/ankane/getformidable}
|
47
|
+
s.licenses = ["MIT"]
|
48
|
+
s.require_paths = ["lib"]
|
49
|
+
s.rubygems_version = %q{1.5.2}
|
50
|
+
s.summary = %q{Formidable}
|
51
|
+
s.test_files = [
|
52
|
+
"spec/getformidable_spec.rb",
|
53
|
+
"spec/spec_helper.rb"
|
54
|
+
]
|
55
|
+
|
56
|
+
if s.respond_to? :specification_version then
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
62
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
64
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<json>, [">= 0"])
|
67
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
68
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
69
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
70
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
s.add_dependency(%q<json>, [">= 0"])
|
74
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
75
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
76
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
77
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
data/init.rb
ADDED
data/lib/formidable.rb
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
require "formidable/config"
|
2
|
+
require "formidable/remote"
|
3
|
+
require "formidable/attempt"
|
4
|
+
require "formidable/timer"
|
5
|
+
require "formidable/controller"
|
6
|
+
require "formidable/view_helpers"
|
7
|
+
require "formidable/commands"
|
8
|
+
require "formidable/model"
|
9
|
+
|
10
|
+
if defined?(Rails::Railtie)
|
11
|
+
require "formidable/railtie"
|
12
|
+
end
|
13
|
+
|
14
|
+
if defined?(ActiveRecord::Base)
|
15
|
+
ActiveRecord::Base.send :include, Formidable::Model
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?(ActionView::Base)
|
19
|
+
ActionView::Base.send :include, Formidable::ViewHelpers
|
20
|
+
end
|
21
|
+
|
22
|
+
if defined?(ActionController::Base)
|
23
|
+
ActionController::Base.send :include, Formidable::Controller
|
24
|
+
end
|
25
|
+
|
26
|
+
module Formidable
|
27
|
+
|
28
|
+
HOST = "www.getformidable.com"
|
29
|
+
VERSION = 1
|
30
|
+
CONFIG_PATH = "config/formidable.yml"
|
31
|
+
|
32
|
+
@@filtered_params = []
|
33
|
+
|
34
|
+
class << self
|
35
|
+
|
36
|
+
def track(args)
|
37
|
+
return if Config.api_key.empty?
|
38
|
+
|
39
|
+
raise "Must define a form." unless args[:form]
|
40
|
+
|
41
|
+
errors = args[:errors] || {}
|
42
|
+
errors = {:base => errors} if errors.any? and errors.kind_of?(Array)
|
43
|
+
|
44
|
+
# values will be nil if errors and no args[:values]
|
45
|
+
# we want this for later
|
46
|
+
values =
|
47
|
+
if Config.track_values and errors.any?
|
48
|
+
args[:values]
|
49
|
+
else
|
50
|
+
{}
|
51
|
+
end
|
52
|
+
|
53
|
+
times = args[:times]
|
54
|
+
total_time = args[:total_time]
|
55
|
+
attempt = args[:attempt]
|
56
|
+
|
57
|
+
if args[:timing_data]
|
58
|
+
tt, t = Timer.parse(args[:timing_data])
|
59
|
+
total_time ||= tt
|
60
|
+
times ||= t
|
61
|
+
end
|
62
|
+
|
63
|
+
# get as much data as we can from the request object
|
64
|
+
# don't override anything that was set
|
65
|
+
request = Thread.current[:formidable_request]
|
66
|
+
if request
|
67
|
+
# Rails 3
|
68
|
+
if request.env["action_dispatch.parameter_filter"]
|
69
|
+
filter_parameters(request.env["action_dispatch.parameter_filter"])
|
70
|
+
end
|
71
|
+
|
72
|
+
# if values aren't set, get them from request and delete
|
73
|
+
values ||= request.params.reject{|k, v| [:utf8, :action, :controller, :authenticity_token, :commit, :formidable].include?(k.to_sym) }
|
74
|
+
|
75
|
+
if timing_data = request.params[:formidable]
|
76
|
+
tt, t = Timer.parse(request.params[:formidable])
|
77
|
+
total_time ||= tt
|
78
|
+
times ||= t
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
times ||= {}
|
83
|
+
values ||= {}
|
84
|
+
|
85
|
+
cookies = Thread.current[:formidable_cookies]
|
86
|
+
if cookies
|
87
|
+
attempt ||= Attempt.parse(cookies, args[:form], errors.empty?)
|
88
|
+
end
|
89
|
+
|
90
|
+
# filter values
|
91
|
+
values.delete_if{|k,v| filter?(k)}
|
92
|
+
|
93
|
+
# flatten everything
|
94
|
+
flatten(errors, true)
|
95
|
+
flatten(values)
|
96
|
+
flatten(times)
|
97
|
+
|
98
|
+
# delete values if no errors for field
|
99
|
+
values.delete_if{|k,v| !errors[k] or errors[k].empty?}
|
100
|
+
|
101
|
+
# time to strip the prefix
|
102
|
+
prefix = args[:prefix]
|
103
|
+
if prefix
|
104
|
+
regex = Regexp.new('\A' + prefix.to_s + '\[([^\]]+)\]')
|
105
|
+
remove_prefix(errors, regex)
|
106
|
+
remove_prefix(values, regex)
|
107
|
+
remove_prefix(times, regex)
|
108
|
+
end
|
109
|
+
|
110
|
+
data = {
|
111
|
+
:form => args[:form],
|
112
|
+
:errors => errors,
|
113
|
+
:values => values,
|
114
|
+
:times => times,
|
115
|
+
:total_time => total_time,
|
116
|
+
:attempt => attempt
|
117
|
+
}
|
118
|
+
|
119
|
+
if Config.thread
|
120
|
+
Thread.new do
|
121
|
+
Remote.send(data)
|
122
|
+
end
|
123
|
+
true
|
124
|
+
else
|
125
|
+
Remote.send(data)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def configure(args)
|
130
|
+
Config.load(args)
|
131
|
+
end
|
132
|
+
|
133
|
+
def filter_parameters(*params)
|
134
|
+
@@filtered_params = @@filtered_params | params.flatten
|
135
|
+
end
|
136
|
+
|
137
|
+
def clear_filtered_parameters
|
138
|
+
@@filtered_params = []
|
139
|
+
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
def remove_prefix(data, regex)
|
144
|
+
return unless data
|
145
|
+
data.dup.each do |k, v|
|
146
|
+
key = k.to_s.gsub(regex, '\1')
|
147
|
+
data[key] = v
|
148
|
+
data.delete(k)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def filter?(k)
|
153
|
+
@@filtered_params.each do |param|
|
154
|
+
if Regexp.new(param.to_s).match(k)
|
155
|
+
return true
|
156
|
+
end
|
157
|
+
end
|
158
|
+
false
|
159
|
+
end
|
160
|
+
|
161
|
+
def flatten(ret, arr=false, name=nil, values=nil)
|
162
|
+
values = ret.dup unless values
|
163
|
+
values.each do |k, v|
|
164
|
+
k = "#{name}[#{k}]" if name
|
165
|
+
unless v.kind_of?(Hash)
|
166
|
+
if !arr or v.kind_of?(Array)
|
167
|
+
ret[k] = v
|
168
|
+
else
|
169
|
+
ret[k] = [] unless v.kind_of?(Array)
|
170
|
+
ret[k] << v
|
171
|
+
end
|
172
|
+
else
|
173
|
+
flatten(ret, arr, k, v)
|
174
|
+
ret.delete(k) unless name
|
175
|
+
end
|
176
|
+
end
|
177
|
+
ret
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Formidable
|
2
|
+
class Attempt
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def parse(cookies, form, valid)
|
6
|
+
hash = Digest::MD5.hexdigest("#{Config::api_key}.#{form}")
|
7
|
+
|
8
|
+
cookie = cookies["formidable"]
|
9
|
+
cookie_data = cookie ? Marshal.load(cookie) : {} rescue {}
|
10
|
+
|
11
|
+
cookie_data[hash] ||= 1
|
12
|
+
attempt = cookie_data[hash]
|
13
|
+
|
14
|
+
unless valid
|
15
|
+
cookie_data[hash] += 1
|
16
|
+
save_cookie(cookies, cookie_data)
|
17
|
+
else
|
18
|
+
cookie_data.delete hash
|
19
|
+
if cookie_data.empty?
|
20
|
+
cookies.delete "formidable"
|
21
|
+
else
|
22
|
+
save_cookie(cookies, cookie_data)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
attempt
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def save_cookie(cookies, data)
|
32
|
+
cookies["formidable"] = {
|
33
|
+
:value => Marshal.dump(data),
|
34
|
+
:expires => Time.now + 86400
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Formidable
|
2
|
+
class Commands
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def run(args)
|
6
|
+
|
7
|
+
if args[0] == "install" and api_key = args[1]
|
8
|
+
|
9
|
+
config = <<CONFIG
|
10
|
+
# Leave api_key blank to disable Formidable for a specific environment.
|
11
|
+
|
12
|
+
development:
|
13
|
+
api_key: #{api_key}
|
14
|
+
|
15
|
+
production:
|
16
|
+
api_key: #{api_key}
|
17
|
+
CONFIG
|
18
|
+
|
19
|
+
Dir.mkdir("config") unless File.exists?("config")
|
20
|
+
File.open(CONFIG_PATH, "w") {|f| f.write(config)}
|
21
|
+
|
22
|
+
puts "Created config file at #{CONFIG_PATH}."
|
23
|
+
elsif args[0] == "test"
|
24
|
+
begin
|
25
|
+
Config.load_file(CONFIG_PATH)
|
26
|
+
Config.thread = false
|
27
|
+
|
28
|
+
Formidable.track(
|
29
|
+
:form => "Test",
|
30
|
+
:errors => {:email => "is invalid"},
|
31
|
+
:values => {:email => "test@formidable"},
|
32
|
+
:attempt => 1,
|
33
|
+
:total_time => 10.1,
|
34
|
+
:times => {:username => 2.4, :email => 5.6}
|
35
|
+
)
|
36
|
+
|
37
|
+
Formidable.track(:form => "Test", :attempt => 2)
|
38
|
+
|
39
|
+
Formidable.track(
|
40
|
+
:form => "Test",
|
41
|
+
:errors => {:email => "is invalid", :username => "is already taken"},
|
42
|
+
:attempt => 1
|
43
|
+
)
|
44
|
+
|
45
|
+
Formidable.track(:form => "Test", :attempt => 2)
|
46
|
+
|
47
|
+
Formidable.track(:form => "Test", :attempt => 1)
|
48
|
+
|
49
|
+
Formidable.track(:form => "Test", :attempt => 1)
|
50
|
+
|
51
|
+
puts "Test successful! Login to http://www.getformidable.com to see it."
|
52
|
+
rescue Exception => e
|
53
|
+
puts "Test failed:\n #{e.message}"
|
54
|
+
end
|
55
|
+
else
|
56
|
+
help =<<HELP
|
57
|
+
Usage:
|
58
|
+
formidable install <api-key>
|
59
|
+
formidable test
|
60
|
+
HELP
|
61
|
+
puts help
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
module Formidable
|
4
|
+
class Config
|
5
|
+
class << self
|
6
|
+
|
7
|
+
DEFAULTS = {
|
8
|
+
:api_key => "",
|
9
|
+
:use_ssl => false,
|
10
|
+
:track_values => false,
|
11
|
+
:thread => true
|
12
|
+
}
|
13
|
+
|
14
|
+
attr_accessor :api_key, :use_ssl, :track_values, :thread
|
15
|
+
|
16
|
+
def load_file(config_file)
|
17
|
+
begin
|
18
|
+
config = YAML::load_file(config_file)
|
19
|
+
env_config = config[app_env] || {}
|
20
|
+
settings = config.merge(env_config)
|
21
|
+
load(settings)
|
22
|
+
rescue Exception => e
|
23
|
+
raise "Configuration error: #{e.message}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def load(settings)
|
28
|
+
# symbolize keys
|
29
|
+
settings = settings.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
30
|
+
|
31
|
+
# ensure we have default settings
|
32
|
+
settings = DEFAULTS.merge(settings)
|
33
|
+
|
34
|
+
[:api_key, :use_ssl, :track_values, :thread].each do |setting|
|
35
|
+
self.send("#{setting}=", settings[setting])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def app_env
|
42
|
+
ENV["RACK_ENV"] || ENV["RAILS_ENV"]|| "development"
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_key(key, default)
|
46
|
+
val = @env_config["track-values"]
|
47
|
+
val = @config["track-values"] if val.nil?
|
48
|
+
val = default if val.nil?
|
49
|
+
val
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Formidable
|
2
|
+
module Controller
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def included(base)
|
6
|
+
base.class_eval do
|
7
|
+
before_filter { |c|
|
8
|
+
Formidable.clear_filtered_parameters
|
9
|
+
Thread.current[:formidable_request] = c.request
|
10
|
+
Thread.current[:formidable_cookies] = c.send(:cookies)
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Formidable
|
2
|
+
module Model
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def included(base)
|
6
|
+
base.class_eval do
|
7
|
+
|
8
|
+
base.send :alias_method, :original_valid?, :valid?
|
9
|
+
|
10
|
+
def valid?(*args)
|
11
|
+
valid = original_valid?(*args)
|
12
|
+
return valid unless @formidable
|
13
|
+
|
14
|
+
model_name = self.class.name.underscore
|
15
|
+
|
16
|
+
if valid
|
17
|
+
errors = {}
|
18
|
+
else
|
19
|
+
error_fix = {}
|
20
|
+
self.errors.each do |k,v|
|
21
|
+
error_fix[k] ||= []
|
22
|
+
error_fix[k] << v
|
23
|
+
end
|
24
|
+
errors = {"#{model_name}" => error_fix}
|
25
|
+
end
|
26
|
+
|
27
|
+
request = Thread.current[:formidable_request]
|
28
|
+
values = request ? request.params : {"#{model_name}" => self.attributes}
|
29
|
+
|
30
|
+
data = {
|
31
|
+
:form => @formidable[:form],
|
32
|
+
:errors => errors,
|
33
|
+
:values => values,
|
34
|
+
:prefix => model_name
|
35
|
+
}
|
36
|
+
|
37
|
+
Formidable.track(data)
|
38
|
+
|
39
|
+
valid
|
40
|
+
end # valid?
|
41
|
+
|
42
|
+
def make_formidable(form)
|
43
|
+
@formidable = {:form => form}
|
44
|
+
self
|
45
|
+
end # make_formidable
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "formidable"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
module Formidable
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
initializer "formidable.middleware" do |app|
|
8
|
+
Formidable::Config.load_file(File.join(Rails.root, "/#{CONFIG_PATH}"))
|
9
|
+
app.config.filter_parameters += [:formidable]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "net/https"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Formidable
|
6
|
+
class Remote
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def send(data)
|
10
|
+
use_ssl = Config.use_ssl
|
11
|
+
|
12
|
+
protocol = use_ssl ? "https://" : "http://"
|
13
|
+
uri = URI.parse("#{protocol}#{HOST}/api/track")
|
14
|
+
|
15
|
+
begin
|
16
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
17
|
+
http.open_timeout = 1
|
18
|
+
|
19
|
+
if use_ssl
|
20
|
+
http.use_ssl = true
|
21
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
22
|
+
end
|
23
|
+
|
24
|
+
query = "api_key=#{Config.api_key}&version=#{VERSION}"
|
25
|
+
res = http.post("#{uri.path}?#{query}", data.to_json)
|
26
|
+
|
27
|
+
(res.code.to_i == 200)
|
28
|
+
rescue
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "base64"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module Formidable
|
5
|
+
class Timer
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def parse(timing_data)
|
9
|
+
js_data = JSON.parse(Base64.decode64(timing_data)) rescue nil
|
10
|
+
if js_data
|
11
|
+
return js_data["total_time"], js_data["times"]
|
12
|
+
end
|
13
|
+
return nil, {}
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "formidable"
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__) , "../init.rb")
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'getformidable'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: getformidable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Kane
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-13 00:00:00 -08:00
|
14
|
+
default_executable: formidable
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: json
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.3.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bundler
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: jeweler
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.5.2
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rcov
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
description: Real-time form analytics
|
72
|
+
email: andrew@getformidable.com
|
73
|
+
executables:
|
74
|
+
- formidable
|
75
|
+
extensions: []
|
76
|
+
|
77
|
+
extra_rdoc_files:
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
files:
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- VERSION
|
87
|
+
- bin/formidable
|
88
|
+
- getformidable.gemspec
|
89
|
+
- init.rb
|
90
|
+
- lib/formidable.rb
|
91
|
+
- lib/formidable/attempt.rb
|
92
|
+
- lib/formidable/commands.rb
|
93
|
+
- lib/formidable/config.rb
|
94
|
+
- lib/formidable/controller.rb
|
95
|
+
- lib/formidable/model.rb
|
96
|
+
- lib/formidable/railtie.rb
|
97
|
+
- lib/formidable/remote.rb
|
98
|
+
- lib/formidable/timer.rb
|
99
|
+
- lib/formidable/view_helpers.rb
|
100
|
+
- lib/getformidable.rb
|
101
|
+
- rails/init.rb
|
102
|
+
- spec/getformidable_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
has_rdoc: true
|
105
|
+
homepage: http://github.com/ankane/getformidable
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
hash: 3983227052732178720
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 1.5.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Formidable
|
135
|
+
test_files:
|
136
|
+
- spec/getformidable_spec.rb
|
137
|
+
- spec/spec_helper.rb
|