corto 0.60.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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -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 Paolo Perego
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 ADDED
File without changes
@@ -0,0 +1,17 @@
1
+ = corto
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (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)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Paolo Perego. See LICENSE for details.
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "corto"
8
+ gem.summary = %Q{corto is an url shortenizer gem}
9
+ gem.description = File.read(File.join(File.dirname(__FILE__), 'README'))
10
+ gem.email = "thesp0nge@gmail.com"
11
+ gem.version = File.read(File.join(File.dirname(__FILE__), 'VERSION'))
12
+ gem.homepage = "http://github.com/thesp0nge/corto"
13
+ gem.authors = ["Paolo Perego"]
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ gem.add_development_dependency "yard", ">= 0"
16
+ gem.add_dependency "sqlite3", ">=0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ begin
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
43
+ rescue LoadError
44
+ task :yardoc do
45
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
46
+ end
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.60.0
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lib/corto'
4
+ require 'getoptlong'
5
+
6
+ opts = GetoptLong.new(
7
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
8
+ [ '--version', '-v', GetoptLong::NO_ARGUMENT ]
9
+ )
10
+
11
+ begin
12
+ opts.each do |opt, arg|
13
+ case opt
14
+ when '--help'
15
+ puts 'usage: corto [-h|-v] url_to_shrink'
16
+ exit 0
17
+ when '--version'
18
+ @version = File.exist?('VERSION') ? File.read('VERSION') : ""
19
+ puts @version
20
+ exit 0
21
+ end
22
+ end
23
+ rescue
24
+ exit 0
25
+ end
26
+ if ARGV.length != 1
27
+ raise 'usage: corto [-h|-v] url_to_shrink'
28
+ exit 0
29
+ end
30
+
31
+ corto = Corto.new
32
+ s = corto.shrink(ARGV[0])
33
+ if (!s.nil?)
34
+ puts 'corto: ' + ARGV[0] + ' shrinked as ' + s
35
+ else
36
+ puts 'corto: it seems ' + ARGV[0] + ' is not a valid url to shrink'
37
+ end
38
+ exit 0
Binary file
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'dm-core'
3
+ require 'sqlite3'
4
+
5
+ class Corto
6
+
7
+ attr_accessor :db_name
8
+ attr_reader :db
9
+
10
+ def initialize (db_name = nil)
11
+ @db_name= db_name
12
+ @db_name= './db/corto.db' unless db_name
13
+ init_db
14
+ end
15
+
16
+
17
+ def purge
18
+ if File.exists?(@db_name)
19
+ File.delete(@db_name)
20
+ end
21
+ @db = SQLite3::Database.new(@db_name)
22
+ @db.execute("CREATE TABLE urls (id integer primary key, url varchar2(256), original varchar2(256));")
23
+ @db.close
24
+ end
25
+
26
+ def count
27
+ @db = SQLite3::Database.new(@db_name)
28
+ count = @db.execute("SELECT COUNT(*) FROM urls;")
29
+ @db.close
30
+
31
+ # output is an array of arrays... so [[int]]
32
+ count.first.first
33
+ end
34
+
35
+ def shrink(url)
36
+ uri = URI::parse(url)
37
+ return nil unless uri.kind_of? URI::HTTP or uri.kind_of? URI::HTTPS
38
+
39
+ @db = SQLite3::Database.new(@db_name)
40
+ count = @db.execute("SELECT COUNT(*) FROM urls WHERE url='" +url.hash.abs.to_s(36)+"'")
41
+ if count.first.first == 0
42
+ @db.execute("INSERT INTO urls (url, original) VALUES ('"+url.hash.abs.to_s(36)+ "', '"+url+"')")
43
+ end
44
+ @db.close
45
+
46
+ url.hash.abs.to_s(36)
47
+ end
48
+
49
+ def deflate(shrinked_url)
50
+ @db = SQLite3::Database.new(@db_name)
51
+ result = @db.execute("SELECT original FROM urls WHERE url='" +shrinked_url+"'")
52
+ (! result.nil?) ? result.first.first : ''
53
+ end
54
+
55
+ private
56
+ def init_db
57
+ if ! File.exists?(@db_name)
58
+ @db = SQLite3::Database.new(@db_name)
59
+ @db.execute("CREATE TABLE urls (id integer primary key, url varchar2(256), original varchar2(256));")
60
+ @db.close
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Corto" do
4
+ let(:corto) {Corto.new('spec/data/test_db.db')}
5
+ it "should recreate a shortned url db" do
6
+ corto.purge
7
+ corto.count.should == 0
8
+ end
9
+
10
+ it "should shrink an url" do
11
+ a = corto.shrink('http://www.armoredcode.com')
12
+ a.should.nil? == false
13
+ end
14
+
15
+ it "should able to reverse a shrink" do
16
+ a = corto.shrink('http://www.armoredcode.com')
17
+ b = corto.deflate(a)
18
+ b.should == "http://www.armoredcode.com"
19
+ end
20
+ end
Binary file
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'corto'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: corto
3
+ version: !ruby/object:Gem::Version
4
+ hash: 239
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 60
9
+ - 0
10
+ version: 0.60.0
11
+ platform: ruby
12
+ authors:
13
+ - Paolo Perego
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-09 00:00:00 +01:00
19
+ default_executable: corto
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: sqlite3
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ description: ""
66
+ email: thesp0nge@gmail.com
67
+ executables:
68
+ - corto
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README
74
+ - README.rdoc
75
+ files:
76
+ - .document
77
+ - .gitignore
78
+ - LICENSE
79
+ - README
80
+ - README.rdoc
81
+ - Rakefile
82
+ - VERSION
83
+ - bin/corto
84
+ - db/corto.db
85
+ - lib/corto.rb
86
+ - spec/corto_spec.rb
87
+ - spec/data/test_db.db
88
+ - spec/spec.opts
89
+ - spec/spec_helper.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/thesp0nge/corto
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options:
96
+ - --charset=UTF-8
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: corto is an url shortenizer gem
124
+ test_files:
125
+ - spec/corto_spec.rb
126
+ - spec/spec_helper.rb