too_short 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +73 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +99 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/ar_base_extensions.rb +55 -0
- data/lib/controller_methods.rb +34 -0
- data/lib/too_short.rb +78 -0
- data/spec/ar_base_extensions_spec.rb +115 -0
- data/spec/controller_methods_spec.rb +36 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/too_short_spec.rb +17 -0
- data/too_short.gemspec +76 -0
- metadata +178 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
gem "activerecord", ">= 3.0.0"
|
5
|
+
gem "actionpack", ">=3.0.0"
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
group :development do
|
10
|
+
gem "rspec-rails", "~> 2.3.0"
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.5.2"
|
13
|
+
gem "rcov", ">= 0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionpack (3.0.3)
|
6
|
+
activemodel (= 3.0.3)
|
7
|
+
activesupport (= 3.0.3)
|
8
|
+
builder (~> 2.1.2)
|
9
|
+
erubis (~> 2.6.6)
|
10
|
+
i18n (~> 0.4)
|
11
|
+
rack (~> 1.2.1)
|
12
|
+
rack-mount (~> 0.6.13)
|
13
|
+
rack-test (~> 0.5.6)
|
14
|
+
tzinfo (~> 0.3.23)
|
15
|
+
activemodel (3.0.3)
|
16
|
+
activesupport (= 3.0.3)
|
17
|
+
builder (~> 2.1.2)
|
18
|
+
i18n (~> 0.4)
|
19
|
+
activerecord (3.0.3)
|
20
|
+
activemodel (= 3.0.3)
|
21
|
+
activesupport (= 3.0.3)
|
22
|
+
arel (~> 2.0.2)
|
23
|
+
tzinfo (~> 0.3.23)
|
24
|
+
activesupport (3.0.3)
|
25
|
+
arel (2.0.7)
|
26
|
+
builder (2.1.2)
|
27
|
+
diff-lcs (1.1.2)
|
28
|
+
erubis (2.6.6)
|
29
|
+
abstract (>= 1.0.0)
|
30
|
+
git (1.2.5)
|
31
|
+
i18n (0.5.0)
|
32
|
+
jeweler (1.5.2)
|
33
|
+
bundler (~> 1.0.0)
|
34
|
+
git (>= 1.2.5)
|
35
|
+
rake
|
36
|
+
rack (1.2.1)
|
37
|
+
rack-mount (0.6.13)
|
38
|
+
rack (>= 1.0.0)
|
39
|
+
rack-test (0.5.7)
|
40
|
+
rack (>= 1.0)
|
41
|
+
railties (3.0.3)
|
42
|
+
actionpack (= 3.0.3)
|
43
|
+
activesupport (= 3.0.3)
|
44
|
+
rake (>= 0.8.7)
|
45
|
+
thor (~> 0.14.4)
|
46
|
+
rake (0.8.7)
|
47
|
+
rcov (0.9.9)
|
48
|
+
rspec (2.3.0)
|
49
|
+
rspec-core (~> 2.3.0)
|
50
|
+
rspec-expectations (~> 2.3.0)
|
51
|
+
rspec-mocks (~> 2.3.0)
|
52
|
+
rspec-core (2.3.1)
|
53
|
+
rspec-expectations (2.3.0)
|
54
|
+
diff-lcs (~> 1.1.2)
|
55
|
+
rspec-mocks (2.3.0)
|
56
|
+
rspec-rails (2.3.1)
|
57
|
+
actionpack (~> 3.0)
|
58
|
+
activesupport (~> 3.0)
|
59
|
+
railties (~> 3.0)
|
60
|
+
rspec (~> 2.3.0)
|
61
|
+
thor (0.14.6)
|
62
|
+
tzinfo (0.3.24)
|
63
|
+
|
64
|
+
PLATFORMS
|
65
|
+
ruby
|
66
|
+
|
67
|
+
DEPENDENCIES
|
68
|
+
actionpack (>= 3.0.0)
|
69
|
+
activerecord (>= 3.0.0)
|
70
|
+
bundler (~> 1.0.0)
|
71
|
+
jeweler (~> 1.5.2)
|
72
|
+
rcov
|
73
|
+
rspec-rails (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Simon Baumgartner
|
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.rdoc
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
==TooShort
|
2
|
+
|
3
|
+
TooShort is a simple short URL service for your resources. And it works without an extra database table.
|
4
|
+
|
5
|
+
----
|
6
|
+
|
7
|
+
==Creating a short URL
|
8
|
+
|
9
|
+
<b>To create a short URL for one resource</b>
|
10
|
+
|
11
|
+
In your model:
|
12
|
+
|
13
|
+
class Foo < ActiveRecord::Base
|
14
|
+
has_a_short_url :host => 'http://2sh.de'
|
15
|
+
end
|
16
|
+
|
17
|
+
@foo.short_url # => http://2sh.de/2n9c
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
<b>To create short URLs for multiple resources:</b>
|
22
|
+
|
23
|
+
In an initializer file (config/initializer/too_short.rb):
|
24
|
+
TooShort.options[:host] = 'http://2sh.de'
|
25
|
+
|
26
|
+
In your models:
|
27
|
+
|
28
|
+
class Foo < ActiveRecord::Base
|
29
|
+
has_a_short_url :scope => 'f'
|
30
|
+
end
|
31
|
+
|
32
|
+
class Bar < ActiveRecord::Base
|
33
|
+
has_as_short_url :scope => 'b'
|
34
|
+
end
|
35
|
+
|
36
|
+
@foo.short_url # => http://2sh.de/*f*/2n9c
|
37
|
+
@bar.short_url # => http://2sh.de/*b*/2n9c
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
==Expanding a short URL
|
42
|
+
|
43
|
+
Add routes:
|
44
|
+
(Rails 2)
|
45
|
+
map.connect '/:scope/:hash', :controller => :short_urls, :action => :expand # for multiple resources
|
46
|
+
map.connect '/:hash', :controller => :short_urls, :action => :expand # for a single resource
|
47
|
+
|
48
|
+
(Rails 3)
|
49
|
+
get '/:scope/:hash' => 'short_urls#expand' # for multiple resources
|
50
|
+
get '/:hash' => 'short_urls#expand' # for a single resource
|
51
|
+
|
52
|
+
|
53
|
+
Create a new controller:
|
54
|
+
|
55
|
+
class ShortUrlController < ApplicationController
|
56
|
+
include TooShort::ControllerMethods
|
57
|
+
end
|
58
|
+
|
59
|
+
The default behavior is
|
60
|
+
* Valid URL -> redirect_to object
|
61
|
+
* Invalid URL -> respond_to with error message
|
62
|
+
|
63
|
+
(see TooShort::ControllerMethods for details)
|
64
|
+
|
65
|
+
This can be overwritten by overwriting two methods
|
66
|
+
respond_to_valid_short_url
|
67
|
+
respond_to_invalid_short_url
|
68
|
+
|
69
|
+
The object that was looked up for the given short URL is stored in
|
70
|
+
@expanded_object
|
71
|
+
|
72
|
+
---
|
73
|
+
|
74
|
+
==How the hashing is done:
|
75
|
+
|
76
|
+
@post = Post.create
|
77
|
+
@post.id # => 123456
|
78
|
+
post.short_url # => http://2sh.de/*2n9c*
|
79
|
+
123456.to_i(36) # => "2n9c"
|
80
|
+
"2n9c".to_s(36) # => 123456
|
81
|
+
|
82
|
+
<em>I borrowed this from http://blog.saush.com/2009/04/13/clone-tinyurl-in-40-lines-of-ruby-code/
|
83
|
+
|
84
|
+
|
85
|
+
== Contributing to too_short
|
86
|
+
|
87
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
88
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
89
|
+
* Fork the project
|
90
|
+
* Start a feature/bugfix branch
|
91
|
+
* Commit and push until you are happy with your contribution
|
92
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
93
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
94
|
+
|
95
|
+
== Copyright
|
96
|
+
|
97
|
+
Copyright (c) 2011 Simon Baumgartner. See LICENSE.txt for
|
98
|
+
further details.
|
99
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
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 = "too_short"
|
16
|
+
gem.homepage = "http://github.com/sens3/too_short"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{A simple short URL service for your resources}
|
19
|
+
gem.description = %Q{A simple short URL service for your resources, no extra database table necessary}
|
20
|
+
gem.email = "makesens3@gmail.com"
|
21
|
+
gem.authors = ["Simon Baumgartner"]
|
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
|
+
spec.rspec_opts = "--color"
|
34
|
+
end
|
35
|
+
|
36
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
37
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
38
|
+
spec.rcov = true
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :spec
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "too_short #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module TooShort
|
2
|
+
module ARBaseExtensions
|
3
|
+
|
4
|
+
def has_a_short_url(options={})
|
5
|
+
short_url_options = TooShort.options.merge(options)
|
6
|
+
short_url_options.symbolize_keys!
|
7
|
+
write_inheritable_attribute(:short_url_options, short_url_options)
|
8
|
+
|
9
|
+
TooShort.register_class(self.to_s, short_url_options[:scope])
|
10
|
+
|
11
|
+
class_eval do
|
12
|
+
include InstanceMethods
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
module InstanceMethods
|
18
|
+
def short_url
|
19
|
+
validate_and_cleanup_short_url_options(short_url_options)
|
20
|
+
hash = TooShort.id_to_hash(self.id)
|
21
|
+
[short_url_host, short_url_scope, hash].compact.join('/')
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def validate_and_cleanup_short_url_options(options)
|
27
|
+
if not @validated_short_url_options
|
28
|
+
if options[:host].blank?
|
29
|
+
raise TooShort::TooShortMissingOptionError.new("You need to specify a short url host")
|
30
|
+
end
|
31
|
+
@validated_short_url_options = true
|
32
|
+
end
|
33
|
+
if not @cleaned_up_short_url_options
|
34
|
+
options[:host].gsub!(/\/$/, '')
|
35
|
+
options[:host] = "http://#{options[:host]}" unless options[:host] =~ /^https?:\/\//
|
36
|
+
options[:scope].gsub!(/\/$/, '') if options[:scope]
|
37
|
+
options[:scope].gsub!(/^\//, '') if options[:scope]
|
38
|
+
@cleaned_up_short_url_options = true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def short_url_scope
|
43
|
+
short_url_options[:scope]
|
44
|
+
end
|
45
|
+
|
46
|
+
def short_url_host
|
47
|
+
short_url_options[:host]
|
48
|
+
end
|
49
|
+
|
50
|
+
def short_url_options
|
51
|
+
self.class.read_inheritable_attribute :short_url_options
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module TooShort
|
2
|
+
module ControllerMethods
|
3
|
+
def expand
|
4
|
+
require_all_model_classes
|
5
|
+
if @expanded_object = TooShort.expand_to_object(params[:scope], params[:hash])
|
6
|
+
respond_to_valid_short_url
|
7
|
+
else
|
8
|
+
respond_to_invalid_short_url
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to_valid_short_url
|
13
|
+
redirect_to @expanded_object
|
14
|
+
end
|
15
|
+
|
16
|
+
def respond_to_invalid_short_url
|
17
|
+
respond_to do |wants|
|
18
|
+
error_hash = {:error => 'Invalid short url'}
|
19
|
+
wants.html { render :text => error_hash[:error]}
|
20
|
+
wants.xml { render :xml => error_hash}
|
21
|
+
wants.json { render :json => error_hash}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def require_all_model_classes
|
28
|
+
# Rails only loads classes when they are used
|
29
|
+
# To populate our registry we require each file in app/models
|
30
|
+
Dir.glob(RAILS_ROOT + '/app/models/*.rb').each { |file| require file } if defined?(RAILS_ROOT)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/too_short.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'ar_base_extensions'
|
3
|
+
require 'controller_methods'
|
4
|
+
require 'singleton'
|
5
|
+
|
6
|
+
module TooShort
|
7
|
+
class TooShortInvalidOptionError < StandardError; end
|
8
|
+
class TooShortMissingOptionError < StandardError; end
|
9
|
+
|
10
|
+
BASE = 36
|
11
|
+
|
12
|
+
# Options are:
|
13
|
+
# <em>host<em>: The short URL host you wish to use, i.e. http://2sh.de (required)
|
14
|
+
def self.options
|
15
|
+
@options ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Used to translate the given hash (and scope) into an object
|
19
|
+
# class_scope is optional.
|
20
|
+
def self.expand_to_object(class_scope, hash)
|
21
|
+
klass = short_url_klass class_scope
|
22
|
+
id = hash_to_id hash
|
23
|
+
klass.find_by_id(id) if klass and id
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.register_class(class_name, class_scope)
|
27
|
+
ClassRegistry.instance.register(class_name, class_scope)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def self.short_url_klass(class_scope)
|
33
|
+
if class_name = ClassRegistry.instance.lookup(class_scope)
|
34
|
+
Kernel.const_get class_name
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.hash_to_id(hash)
|
39
|
+
hash.to_i(BASE)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.id_to_hash(id)
|
43
|
+
id.to_s(BASE)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Holds the model classes that use TooShort
|
47
|
+
# It's basically a map storing :scope => :class_name
|
48
|
+
# I.e. 'p' => 'Post'
|
49
|
+
#
|
50
|
+
# Only used internally.
|
51
|
+
class ClassRegistry
|
52
|
+
include Singleton
|
53
|
+
def registry
|
54
|
+
@registry ||= {}
|
55
|
+
end
|
56
|
+
|
57
|
+
def register(class_name, class_scope)
|
58
|
+
registered_class_name = registry[class_scope]
|
59
|
+
if registered_class_name && registered_class_name != class_name
|
60
|
+
raise TooShortInvalidOptionError.new("Duplicate usage of scope '#{class_scope.to_s}', used in classes #{registered_class_name} and #{class_name}")
|
61
|
+
end
|
62
|
+
registry[class_scope] = class_name
|
63
|
+
end
|
64
|
+
|
65
|
+
def lookup(class_scope)
|
66
|
+
registry[class_scope]
|
67
|
+
end
|
68
|
+
|
69
|
+
# only used for specs
|
70
|
+
def clear
|
71
|
+
@registry = {}
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
ActiveRecord::Base.class_eval { extend TooShort::ARBaseExtensions }
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class ModelStub < ActiveRecord::Base
|
4
|
+
def self.columns() @columns ||= []; end
|
5
|
+
def id() 123456; end
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe "TooShort ActiveRecord::Base extensions" do
|
10
|
+
|
11
|
+
describe "short_url" do
|
12
|
+
|
13
|
+
before do
|
14
|
+
TooShort::ClassRegistry.instance.clear
|
15
|
+
TooShort.options[:host] = 'http://2short.de'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return the short url" do
|
19
|
+
class Foo < ModelStub
|
20
|
+
has_a_short_url
|
21
|
+
end
|
22
|
+
Foo.new.short_url.should == "http://2short.de/2n9c"
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "if the host is not set" do
|
26
|
+
before do
|
27
|
+
TooShort.options[:host] = nil
|
28
|
+
end
|
29
|
+
it "should return an error" do
|
30
|
+
class Foo < ModelStub
|
31
|
+
has_a_short_url
|
32
|
+
end
|
33
|
+
lambda{Foo.new.short_url}.should raise_error(TooShort::TooShortMissingOptionError, "You need to specify a short url host")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "if the host is set on the model" do
|
38
|
+
before do
|
39
|
+
class Foo < ModelStub
|
40
|
+
has_a_short_url :host => 'http://2short.it'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
it "should return the short url for the specified host" do
|
44
|
+
Foo.new.short_url.should == "http://2short.it/2n9c"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "if a scope is set" do
|
49
|
+
before do
|
50
|
+
class Foo < ModelStub
|
51
|
+
has_a_short_url :scope => 'f'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
it "should return the short url scoped by the class hash" do
|
55
|
+
Foo.new.short_url.should == "http://2short.de/f/2n9c"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should remove trailing slashes from the host" do
|
60
|
+
class Foo < ModelStub
|
61
|
+
has_a_short_url :host => 'http://2short.es/'
|
62
|
+
end
|
63
|
+
Foo.new.short_url.should == "http://2short.es/2n9c"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should remove slashes around the scope" do
|
67
|
+
class Foo < ModelStub
|
68
|
+
has_a_short_url :scope => '/p/'
|
69
|
+
end
|
70
|
+
Foo.new.short_url.should == "http://2short.de/p/2n9c"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should prepend http:// if necessary" do
|
74
|
+
TooShort.options[:host] = '2short.it'
|
75
|
+
class Foo < ModelStub
|
76
|
+
has_a_short_url
|
77
|
+
end
|
78
|
+
Foo.new.short_url.should == "http://2short.it/2n9c"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should not prepend http:// if https:// was specified" do
|
82
|
+
TooShort.options[:host] = 'https://2short.it'
|
83
|
+
class Foo < ModelStub
|
84
|
+
has_a_short_url
|
85
|
+
end
|
86
|
+
Foo.new.short_url.should == "https://2short.it/2n9c"
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should return an error when included more than once with no scope" do
|
92
|
+
lambda {
|
93
|
+
class Foo < ModelStub
|
94
|
+
has_a_short_url
|
95
|
+
end
|
96
|
+
|
97
|
+
class Bar < ModelStub
|
98
|
+
has_a_short_url
|
99
|
+
end
|
100
|
+
}.should raise_error(TooShort::TooShortInvalidOptionError, "Duplicate usage of scope '', used in classes Foo and Bar")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should return an error when included more than once with the same scope" do
|
104
|
+
lambda {
|
105
|
+
class Foo < ModelStub
|
106
|
+
has_a_short_url :scope => 'x'
|
107
|
+
end
|
108
|
+
|
109
|
+
class Bar < ModelStub
|
110
|
+
has_a_short_url :scope => 'x'
|
111
|
+
end
|
112
|
+
}.should raise_error(TooShort::TooShortInvalidOptionError, "Duplicate usage of scope 'x', used in classes Foo and Bar")
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'action_controller'
|
4
|
+
|
5
|
+
class ShortUrlController < ActionController::Base
|
6
|
+
include TooShort::ControllerMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "TooShort ControllerMethods" do
|
10
|
+
before do
|
11
|
+
@controller = ShortUrlController.new
|
12
|
+
end
|
13
|
+
describe "expand" do
|
14
|
+
before do
|
15
|
+
@controller.stub!(:respond_to_valid_short_url)
|
16
|
+
@controller.stub!(:respond_to_invalid_short_url)
|
17
|
+
@params = {:scope => 'f', :hash => '2n9c'}
|
18
|
+
@controller.stub!(:params).and_return(@params)
|
19
|
+
end
|
20
|
+
after do
|
21
|
+
@controller.expand
|
22
|
+
end
|
23
|
+
it "should get the object for the short url" do
|
24
|
+
TooShort.should_receive(:expand_to_object).with('f', '2n9c')
|
25
|
+
end
|
26
|
+
it "should respond to a valid short url" do
|
27
|
+
TooShort.stub!(:expand_to_object).and_return(mock('expanded object'))
|
28
|
+
@controller.should_receive(:respond_to_valid_short_url)
|
29
|
+
end
|
30
|
+
it "should respond to an invalid short url" do
|
31
|
+
TooShort.stub!(:expand_to_object).and_return(nil)
|
32
|
+
@controller.should_receive(:respond_to_invalid_short_url)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
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 'too_short'
|
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
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "TooShort" do
|
4
|
+
before do
|
5
|
+
TooShort.options[:host] = 'http://2short.de'
|
6
|
+
end
|
7
|
+
describe "expand" do
|
8
|
+
it "should expand the given hash" do
|
9
|
+
@foo = mock('food')
|
10
|
+
class Foo
|
11
|
+
has_a_short_url
|
12
|
+
end
|
13
|
+
Foo.stub!(:find_by_id).with(123456).and_return(@foo)
|
14
|
+
TooShort.expand_to_object(nil, '2n9c').should == @foo
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/too_short.gemspec
ADDED
@@ -0,0 +1,76 @@
|
|
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{too_short}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Simon Baumgartner"]
|
12
|
+
s.date = %q{2011-01-30}
|
13
|
+
s.description = %q{A simple short URL service for your resources, no extra database table necessary}
|
14
|
+
s.email = %q{makesens3@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/ar_base_extensions.rb",
|
28
|
+
"lib/controller_methods.rb",
|
29
|
+
"lib/too_short.rb",
|
30
|
+
"spec/ar_base_extensions_spec.rb",
|
31
|
+
"spec/controller_methods_spec.rb",
|
32
|
+
"spec/spec_helper.rb",
|
33
|
+
"spec/too_short_spec.rb",
|
34
|
+
"too_short.gemspec"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/sens3/too_short}
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.7}
|
40
|
+
s.summary = %q{A simple short URL service for your resources}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/ar_base_extensions_spec.rb",
|
43
|
+
"spec/controller_methods_spec.rb",
|
44
|
+
"spec/spec_helper.rb",
|
45
|
+
"spec/too_short_spec.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
54
|
+
s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
|
55
|
+
s.add_development_dependency(%q<rspec-rails>, ["~> 2.3.0"])
|
56
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
58
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
61
|
+
s.add_dependency(%q<actionpack>, [">= 3.0.0"])
|
62
|
+
s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
end
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
69
|
+
s.add_dependency(%q<actionpack>, [">= 3.0.0"])
|
70
|
+
s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
73
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
metadata
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: too_short
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Simon Baumgartner
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-30 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
name: activerecord
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 7
|
31
|
+
segments:
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
version: 3.0.0
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
name: actionpack
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 7
|
47
|
+
segments:
|
48
|
+
- 3
|
49
|
+
- 0
|
50
|
+
- 0
|
51
|
+
version: 3.0.0
|
52
|
+
requirement: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
name: rspec-rails
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 2
|
65
|
+
- 3
|
66
|
+
- 0
|
67
|
+
version: 2.3.0
|
68
|
+
requirement: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
name: bundler
|
73
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 23
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 0
|
82
|
+
- 0
|
83
|
+
version: 1.0.0
|
84
|
+
requirement: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
name: jeweler
|
89
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ~>
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 7
|
95
|
+
segments:
|
96
|
+
- 1
|
97
|
+
- 5
|
98
|
+
- 2
|
99
|
+
version: 1.5.2
|
100
|
+
requirement: *id005
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
name: rcov
|
105
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirement: *id006
|
115
|
+
description: A simple short URL service for your resources, no extra database table necessary
|
116
|
+
email: makesens3@gmail.com
|
117
|
+
executables: []
|
118
|
+
|
119
|
+
extensions: []
|
120
|
+
|
121
|
+
extra_rdoc_files:
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.rdoc
|
124
|
+
files:
|
125
|
+
- .document
|
126
|
+
- Gemfile
|
127
|
+
- Gemfile.lock
|
128
|
+
- LICENSE.txt
|
129
|
+
- README.rdoc
|
130
|
+
- Rakefile
|
131
|
+
- VERSION
|
132
|
+
- lib/ar_base_extensions.rb
|
133
|
+
- lib/controller_methods.rb
|
134
|
+
- lib/too_short.rb
|
135
|
+
- spec/ar_base_extensions_spec.rb
|
136
|
+
- spec/controller_methods_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
- spec/too_short_spec.rb
|
139
|
+
- too_short.gemspec
|
140
|
+
has_rdoc: true
|
141
|
+
homepage: http://github.com/sens3/too_short
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
hash: 3
|
155
|
+
segments:
|
156
|
+
- 0
|
157
|
+
version: "0"
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
hash: 3
|
164
|
+
segments:
|
165
|
+
- 0
|
166
|
+
version: "0"
|
167
|
+
requirements: []
|
168
|
+
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 1.3.7
|
171
|
+
signing_key:
|
172
|
+
specification_version: 3
|
173
|
+
summary: A simple short URL service for your resources
|
174
|
+
test_files:
|
175
|
+
- spec/ar_base_extensions_spec.rb
|
176
|
+
- spec/controller_methods_spec.rb
|
177
|
+
- spec/spec_helper.rb
|
178
|
+
- spec/too_short_spec.rb
|