urb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.rdoc +5 -0
- data/Gemfile +15 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +91 -0
- data/Rakefile +10 -0
- data/VERSION +1 -0
- data/app/assets/javascripts/urb.js.erb +55 -0
- data/app/helpers/urb_helper.rb +7 -0
- data/app/views/urb/_include.html.erb +1 -0
- data/demo/Gemfile +13 -0
- data/demo/README.rdoc +28 -0
- data/demo/Rakefile +6 -0
- data/demo/app/assets/images/.keep +0 -0
- data/demo/app/assets/javascripts/application.js +16 -0
- data/demo/app/assets/stylesheets/application.css +13 -0
- data/demo/app/controllers/application_controller.rb +5 -0
- data/demo/app/controllers/concerns/.keep +0 -0
- data/demo/app/controllers/home_controller.rb +2 -0
- data/demo/app/helpers/application_helper.rb +2 -0
- data/demo/app/mailers/.keep +0 -0
- data/demo/app/models/.keep +0 -0
- data/demo/app/models/concerns/.keep +0 -0
- data/demo/app/views/home/index.html.erb +31 -0
- data/demo/app/views/layouts/application.html.erb +13 -0
- data/demo/bin/bundle +3 -0
- data/demo/bin/rails +4 -0
- data/demo/bin/rake +4 -0
- data/demo/config/application.rb +27 -0
- data/demo/config/boot.rb +4 -0
- data/demo/config/database.yml +30 -0
- data/demo/config/environment.rb +5 -0
- data/demo/config/environments/development.rb +29 -0
- data/demo/config/environments/production.rb +80 -0
- data/demo/config/environments/test.rb +36 -0
- data/demo/config/initializers/backtrace_silencers.rb +7 -0
- data/demo/config/initializers/filter_parameter_logging.rb +4 -0
- data/demo/config/initializers/inflections.rb +16 -0
- data/demo/config/initializers/mime_types.rb +5 -0
- data/demo/config/initializers/secret_token.rb +12 -0
- data/demo/config/initializers/session_store.rb +3 -0
- data/demo/config/initializers/u.rb +7 -0
- data/demo/config/initializers/wrap_parameters.rb +14 -0
- data/demo/config/locales/en.yml +23 -0
- data/demo/config/routes.rb +56 -0
- data/demo/config.ru +4 -0
- data/demo/db/migrate/20140402161002_create_urls.rb +9 -0
- data/demo/db/schema.rb +23 -0
- data/demo/db/seeds.rb +7 -0
- data/demo/lib/assets/.keep +0 -0
- data/demo/lib/tasks/.keep +0 -0
- data/demo/log/.keep +0 -0
- data/demo/public/404.html +58 -0
- data/demo/public/422.html +58 -0
- data/demo/public/500.html +57 -0
- data/demo/public/favicon.ico +0 -0
- data/demo/public/robots.txt +5 -0
- data/demo/test/controllers/.keep +0 -0
- data/demo/test/fixtures/.keep +0 -0
- data/demo/test/helpers/.keep +0 -0
- data/demo/test/integration/.keep +0 -0
- data/demo/test/mailers/.keep +0 -0
- data/demo/test/models/.keep +0 -0
- data/demo/test/test_helper.rb +15 -0
- data/demo/vendor/assets/javascripts/.keep +0 -0
- data/demo/vendor/assets/stylesheets/.keep +0 -0
- data/lib/urb/engine.rb +9 -0
- data/lib/urb/middleware.rb +31 -0
- data/lib/urb/version.rb +7 -0
- data/lib/urb.rb +48 -0
- data/script/console +7 -0
- data/test/test_helper/coverage.rb +8 -0
- data/test/test_helper.rb +7 -0
- data/test/unit/test_urb.rb +114 -0
- data/test/unit/urb/test_middleware.rb +70 -0
- data/urb.gemspec +21 -0
- metadata +166 -0
@@ -0,0 +1,114 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
|
3
|
+
module Unit
|
4
|
+
class TestURB < Minitest::Test
|
5
|
+
|
6
|
+
describe URB do
|
7
|
+
before do
|
8
|
+
URB.instance_variable_set :@config, nil
|
9
|
+
URB.instance_variable_set :@paths, nil
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "URB::VERSION" do
|
13
|
+
it "has the current version" do
|
14
|
+
version = File.read File.expand_path("../../../VERSION", __FILE__)
|
15
|
+
assert_equal version, URB::VERSION
|
16
|
+
assert File.read(File.expand_path("../../../CHANGELOG.rdoc", __FILE__)).include?("Version #{version}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".URB" do
|
21
|
+
it "delegates to URB.config" do
|
22
|
+
URB.expects(:config).with(:foo, :bar)
|
23
|
+
URB :foo, :bar
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ".config" do
|
28
|
+
it "memoizes its Moneta backend configuration" do
|
29
|
+
URB.config :File, :dir => "moneta"
|
30
|
+
assert_equal [:File, :dir => "moneta"], URB.instance_variable_get(:@config)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ".paths" do
|
35
|
+
describe "not configured" do
|
36
|
+
it "returns an in-memory Moneta store instance" do
|
37
|
+
store = mock
|
38
|
+
Moneta.expects(:new).with(:Memory).returns(store)
|
39
|
+
assert_equal store, URB.send(:paths)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
describe "configured" do
|
43
|
+
before do
|
44
|
+
URB.instance_variable_set :@config, [:Foo, :bar => "baz"]
|
45
|
+
end
|
46
|
+
it "returns an in-memory Moneta store instance" do
|
47
|
+
store = mock
|
48
|
+
Moneta.expects(:new).with(:Foo, :bar => "baz").returns(store)
|
49
|
+
assert_equal store, URB.send(:paths)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
it "memoizes its Moneta store" do
|
53
|
+
assert_equal URB.send(:paths).object_id, URB.send(:paths).object_id
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe ".generate_key" do
|
58
|
+
it "generates a unique key" do
|
59
|
+
keys = (1..250000).to_a.collect{URB.send(:generate_key)}
|
60
|
+
assert_equal 250000, keys.uniq.size
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe ".store" do
|
65
|
+
before do
|
66
|
+
@key = "abc123"
|
67
|
+
@path = "/foo?bar=baz"
|
68
|
+
@value = "#{URB::PREFIX}#{@path}"
|
69
|
+
end
|
70
|
+
describe "when value not stored yet" do
|
71
|
+
it "stores the passed path in the Moneta store and returns the generated key" do
|
72
|
+
paths = {}
|
73
|
+
|
74
|
+
URB.expects(:paths).at_least_once.returns(paths)
|
75
|
+
URB.expects(:generate_key).returns(@key)
|
76
|
+
|
77
|
+
assert_equal @key, URB.store(@path)
|
78
|
+
assert_equal({
|
79
|
+
@key => @path,
|
80
|
+
@value => @key
|
81
|
+
}, paths)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
describe "when value already stored" do
|
85
|
+
it "returns the key of the already stored value" do
|
86
|
+
paths = {
|
87
|
+
@key => @path,
|
88
|
+
@value => @key
|
89
|
+
}
|
90
|
+
|
91
|
+
URB.expects(:paths).at_least_once.returns(paths)
|
92
|
+
URB.expects(:generate_key).never
|
93
|
+
|
94
|
+
assert_equal @key, URB.store(@path)
|
95
|
+
assert_equal({
|
96
|
+
@key => @path,
|
97
|
+
@value => @key
|
98
|
+
}, paths)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe ".fetch" do
|
104
|
+
it "returns the stored path in the Moneta store" do
|
105
|
+
paths = {"abc" => "/foo", "123" => "/bar"}
|
106
|
+
URB.expects(:paths).at_least_once.returns(paths)
|
107
|
+
assert_equal "/foo", URB.fetch("abc")
|
108
|
+
assert_equal "/bar", URB.fetch("123")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative "../../test_helper"
|
2
|
+
|
3
|
+
module Unit
|
4
|
+
module Urb
|
5
|
+
class TestMiddleware < Minitest::Test
|
6
|
+
|
7
|
+
describe URB::Middleware do
|
8
|
+
before do
|
9
|
+
@key = "123abc"
|
10
|
+
@path = "/foo/bar?very=long&query=string&foo[]=bar&foo[]=baz"
|
11
|
+
@value = "#{URB::PREFIX}#{@path}"
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "when value not stored yet" do
|
15
|
+
it "stores the passed path, redirects to the 'key URL' and alters the request parameters" do
|
16
|
+
URB.instance_variable_set :@paths, {}
|
17
|
+
URB.expects(:generate_key).returns @key
|
18
|
+
|
19
|
+
get "/"
|
20
|
+
assert_equal "{}", last_response.body
|
21
|
+
|
22
|
+
post URB::PATH, {:path => @path}
|
23
|
+
assert_equal 302, last_response.status
|
24
|
+
follow_redirect!
|
25
|
+
|
26
|
+
assert_equal({
|
27
|
+
"very" => "long", "query" => "string", "foo" => %w(bar baz)
|
28
|
+
}.inspect, last_response.body)
|
29
|
+
|
30
|
+
assert_equal({
|
31
|
+
@key => @path,
|
32
|
+
@value => @key
|
33
|
+
}, URB.instance_variable_get(:@paths))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "when value already stored" do
|
38
|
+
it "alters the request parameters" do
|
39
|
+
URB.instance_variable_set :@paths, {
|
40
|
+
@key => @path,
|
41
|
+
@value => @key
|
42
|
+
}
|
43
|
+
URB.expects(:generate_key).never
|
44
|
+
|
45
|
+
get "#{URB::PATH}#{@key}"
|
46
|
+
assert_equal({
|
47
|
+
"very" => "long", "query" => "string", "foo" => %w(bar baz)
|
48
|
+
}.inspect, last_response.body)
|
49
|
+
|
50
|
+
assert_equal({
|
51
|
+
@key => @path,
|
52
|
+
@value => @key
|
53
|
+
}, URB.instance_variable_get(:@paths))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def app
|
63
|
+
Rack::Builder.app do
|
64
|
+
use URB::Middleware
|
65
|
+
run lambda { |env|
|
66
|
+
params = Rack::Request.new(env).params
|
67
|
+
[200, {"Content-Type" => "text/plain"}, [params.inspect]]
|
68
|
+
}
|
69
|
+
end
|
70
|
+
end
|
data/urb.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/urb/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Paul Engel"]
|
6
|
+
gem.email = ["pm_engel@icloud.com"]
|
7
|
+
gem.summary = %q{Shorten URLs in your Rack / Rails app}
|
8
|
+
gem.description = %q{Shorten URLs in your Rack / Rails app}
|
9
|
+
gem.homepage = "https://github.com/archan937/urb"
|
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 = "urb"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = URB::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "moneta"
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "pry"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: urb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Engel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: moneta
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Shorten URLs in your Rack / Rails app
|
56
|
+
email:
|
57
|
+
- pm_engel@icloud.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
|
+
- CHANGELOG.rdoc
|
65
|
+
- Gemfile
|
66
|
+
- MIT-LICENSE
|
67
|
+
- README.rdoc
|
68
|
+
- Rakefile
|
69
|
+
- VERSION
|
70
|
+
- app/assets/javascripts/urb.js.erb
|
71
|
+
- app/helpers/urb_helper.rb
|
72
|
+
- app/views/urb/_include.html.erb
|
73
|
+
- demo/Gemfile
|
74
|
+
- demo/README.rdoc
|
75
|
+
- demo/Rakefile
|
76
|
+
- demo/app/assets/images/.keep
|
77
|
+
- demo/app/assets/javascripts/application.js
|
78
|
+
- demo/app/assets/stylesheets/application.css
|
79
|
+
- demo/app/controllers/application_controller.rb
|
80
|
+
- demo/app/controllers/concerns/.keep
|
81
|
+
- demo/app/controllers/home_controller.rb
|
82
|
+
- demo/app/helpers/application_helper.rb
|
83
|
+
- demo/app/mailers/.keep
|
84
|
+
- demo/app/models/.keep
|
85
|
+
- demo/app/models/concerns/.keep
|
86
|
+
- demo/app/views/home/index.html.erb
|
87
|
+
- demo/app/views/layouts/application.html.erb
|
88
|
+
- demo/bin/bundle
|
89
|
+
- demo/bin/rails
|
90
|
+
- demo/bin/rake
|
91
|
+
- demo/config.ru
|
92
|
+
- demo/config/application.rb
|
93
|
+
- demo/config/boot.rb
|
94
|
+
- demo/config/database.yml
|
95
|
+
- demo/config/environment.rb
|
96
|
+
- demo/config/environments/development.rb
|
97
|
+
- demo/config/environments/production.rb
|
98
|
+
- demo/config/environments/test.rb
|
99
|
+
- demo/config/initializers/backtrace_silencers.rb
|
100
|
+
- demo/config/initializers/filter_parameter_logging.rb
|
101
|
+
- demo/config/initializers/inflections.rb
|
102
|
+
- demo/config/initializers/mime_types.rb
|
103
|
+
- demo/config/initializers/secret_token.rb
|
104
|
+
- demo/config/initializers/session_store.rb
|
105
|
+
- demo/config/initializers/u.rb
|
106
|
+
- demo/config/initializers/wrap_parameters.rb
|
107
|
+
- demo/config/locales/en.yml
|
108
|
+
- demo/config/routes.rb
|
109
|
+
- demo/db/migrate/20140402161002_create_urls.rb
|
110
|
+
- demo/db/schema.rb
|
111
|
+
- demo/db/seeds.rb
|
112
|
+
- demo/lib/assets/.keep
|
113
|
+
- demo/lib/tasks/.keep
|
114
|
+
- demo/log/.keep
|
115
|
+
- demo/public/404.html
|
116
|
+
- demo/public/422.html
|
117
|
+
- demo/public/500.html
|
118
|
+
- demo/public/favicon.ico
|
119
|
+
- demo/public/robots.txt
|
120
|
+
- demo/test/controllers/.keep
|
121
|
+
- demo/test/fixtures/.keep
|
122
|
+
- demo/test/helpers/.keep
|
123
|
+
- demo/test/integration/.keep
|
124
|
+
- demo/test/mailers/.keep
|
125
|
+
- demo/test/models/.keep
|
126
|
+
- demo/test/test_helper.rb
|
127
|
+
- demo/vendor/assets/javascripts/.keep
|
128
|
+
- demo/vendor/assets/stylesheets/.keep
|
129
|
+
- lib/urb.rb
|
130
|
+
- lib/urb/engine.rb
|
131
|
+
- lib/urb/middleware.rb
|
132
|
+
- lib/urb/version.rb
|
133
|
+
- script/console
|
134
|
+
- test/test_helper.rb
|
135
|
+
- test/test_helper/coverage.rb
|
136
|
+
- test/unit/test_urb.rb
|
137
|
+
- test/unit/urb/test_middleware.rb
|
138
|
+
- urb.gemspec
|
139
|
+
homepage: https://github.com/archan937/urb
|
140
|
+
licenses: []
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ! '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ! '>='
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.1.11
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Shorten URLs in your Rack / Rails app
|
162
|
+
test_files:
|
163
|
+
- test/test_helper.rb
|
164
|
+
- test/test_helper/coverage.rb
|
165
|
+
- test/unit/test_urb.rb
|
166
|
+
- test/unit/urb/test_middleware.rb
|