expurrel 0.1.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jaap van der Meer
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,37 @@
1
+ = Expurrel
2
+
3
+ Expurrel is a mini library to does tiny url decoding. It supports most of the tiny url
4
+ providers by doing a request to the tiny url provider and getting back the redirect code. In
5
+ this implementation the urls are cached.
6
+
7
+ == Install
8
+
9
+
10
+ == Usage
11
+
12
+
13
+ e = Expurrel.new('http://bit.ly/aanv6P')
14
+ e.decode.should eql('http://www.jaapvandermeer.com/2010/02/12/introducing-myself/')
15
+
16
+ == Notes
17
+
18
+ The cache size is standard 1000 urls, to make it bigger:
19
+
20
+ Expurrel.cache = SimpleCache.new(:max_size => 10000)
21
+
22
+
23
+
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so I don't break it in a
30
+ future version unintentionally.
31
+ * Commit, do not mess with rakefile, version, or history.
32
+ (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)
33
+ * Send me a pull request. Bonus points for topic branches.
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2010 Jaap van der Meer. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "expurrel"
8
+ gem.summary = %Q{Library to decode tiny urls in a efficient way}
9
+ gem.description = %Q{Expurrel decodes tiny urls in a efficient way.}
10
+ gem.email = "jaapvandermeer@gmail.com"
11
+ gem.homepage = "http://github.com/japetheape/expurrel"
12
+ gem.authors = ["Jaap van der Meer"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "expurrel #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/expurrel.rb ADDED
@@ -0,0 +1,93 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+ require 'simple_cache'
4
+
5
+ # Expurrel is a tiny url that can be decoded.
6
+ class Expurrel
7
+ PROVIDERS = []
8
+ PROVIDERS_LIST_FILE = File.join(File.dirname(__FILE__), '..', 'tinyurlproviders.txt')
9
+
10
+
11
+ @@cache = SimpleCache.new(:max_size => 1000)
12
+
13
+ # Give expurrel a new url
14
+ def initialize(url)
15
+ @url = url
16
+ end
17
+
18
+
19
+ # This method checks first if it's a tiny url domain from the list.
20
+ # 1. Is it a tinyurl?
21
+ # 2. It is cached? (tinyurls won't change we can cache forever)
22
+ # 3. Decode (slow!), does http request.
23
+ def decode
24
+ return begin
25
+ if !self.is_tiny_url?
26
+ @url
27
+ elsif @@cache.has_key?(@url)
28
+ @@cache.value(@url)
29
+ else
30
+ # TODO add it to the cache
31
+ decoded_url = self.class.reverse_tinyurl(@url)
32
+ @@cache.cache!(@url, decoded_url)
33
+ decoded_url
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ # Is this a tiny url
40
+ def is_tiny_url?
41
+ is_tiny_domain?(self.domain)
42
+ end
43
+
44
+
45
+ # Is it a tiny domain?
46
+ def is_tiny_domain?(domain)
47
+ PROVIDERS.each do |l|
48
+ # TODO regular expression matching
49
+ if domain.include?(l)
50
+ return true
51
+ end
52
+ end
53
+ false
54
+ end
55
+
56
+
57
+
58
+ # Get the domain
59
+ def domain
60
+ return @domain if !@domain.nil?
61
+ unpacked_link = @url.scan(/(https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/)
62
+ @domain = unpacked_link.first[1]
63
+ return @domain
64
+ end
65
+
66
+
67
+ class << self
68
+
69
+ # Loads the list with providers into
70
+ # the PROVIDERS constant.
71
+ def load_providers!
72
+ return if !PROVIDERS.empty?
73
+ File.open(PROVIDERS_LIST_FILE, "r") .each_line do |line|
74
+ PROVIDERS << line
75
+ end
76
+ end
77
+
78
+
79
+
80
+
81
+ # Uses the header file sent back to decode
82
+ # the tiny url.
83
+ # Trick provided by:
84
+ # http://garrickvanburen.com/archive/how-to-decode-tinyurls-with-ruby
85
+ def reverse_tinyurl(tinyurl)
86
+ Net::HTTP.get_response(URI.parse(tinyurl)).to_hash['location'].to_s
87
+ end
88
+
89
+ end
90
+ end
91
+
92
+
93
+ Expurrel.load_providers!
@@ -0,0 +1,35 @@
1
+
2
+ # SimpleCache is a very simple cache store.
3
+ class SimpleCache
4
+ attr_accessor :options
5
+ attr_accessor :store
6
+
7
+ # Create a new cache store:
8
+ # SimpleCache.new(:max_size => 100)
9
+ # to set the size of the cache
10
+ def initialize(options = {:max_size => 10})
11
+ @options = options
12
+ @store = {}
13
+ end
14
+
15
+ # Does it have this key
16
+ def has_key?(key)
17
+ !@store[key].nil?
18
+ end
19
+
20
+ # Cache an item
21
+ def cache!(key, value)
22
+ if @store.size >= @options[:max_size]
23
+ @store.delete(@store.keys.first)
24
+ end
25
+
26
+ @store[key] = value
27
+ end
28
+
29
+
30
+ # Get value of an key
31
+ def value(key)
32
+ @store[key]
33
+ end
34
+
35
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Expurrel" do
4
+ it "fails" do
5
+ #fail "hey buddy, you should probably rename this file and start specing for real"
6
+ 1.should eql(1)
7
+ end
8
+
9
+ it "should load the text file into a constant" do
10
+ Expurrel::PROVIDERS.include?('bit.ly').should be true
11
+ end
12
+
13
+ it "should not decode a normal url" do
14
+ e = Expurrel.new('http://www.jaapvandermeer.com/test')
15
+ e.decode.should eql('http://www.jaapvandermeer.com/test')
16
+ end
17
+
18
+ it "should decode a url that is on the tinyurlproviders.txt list" do
19
+ e = Expurrel.new('http://bit.ly/aanv6P')
20
+ e.decode.should eql('http://www.jaapvandermeer.com/2010/02/12/introducing-myself/')
21
+ end
22
+
23
+
24
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SimpleCache" do
4
+
5
+ it 'should do proper caching' do
6
+ s = SimpleCache.new
7
+ s.cache!('hoi', 'jo')
8
+ s.has_key?('hoi').should be true
9
+ s.value('hoi').should eql('jo')
10
+
11
+ end
12
+
13
+ it 'should not exceed the maximum caching' do
14
+ s = SimpleCache.new
15
+ 20.times do |i|
16
+ s.cache!(i, 'something to cache')
17
+ end
18
+ s.store.size.should <= 10
19
+ end
20
+
21
+
22
+
23
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'expurrel'
4
+ require 'simple_cache'
5
+ require 'rubygems'
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
@@ -0,0 +1 @@
1
+ bit.ly
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expurrel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jaap van der Meer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-12 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: Expurrel decodes tiny urls in a efficient way.
26
+ email: jaapvandermeer@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/expurrel.rb
42
+ - lib/simple_cache.rb
43
+ - spec/expurrel_spec.rb
44
+ - spec/simple_cache_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ - tinyurlproviders.txt
48
+ has_rdoc: true
49
+ homepage: http://github.com/japetheape/expurrel
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Library to decode tiny urls in a efficient way
76
+ test_files:
77
+ - spec/expurrel_spec.rb
78
+ - spec/simple_cache_spec.rb
79
+ - spec/spec_helper.rb