is-gd-shrinker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jeff Sutherland
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.
@@ -0,0 +1,14 @@
1
+ = is-gd-shrinker
2
+
3
+ A small gem to shorten URLs using is.gd.
4
+
5
+ == Usage
6
+ require "shrinker"
7
+
8
+ <tt>Shrinker.shrink("http://www.google.com")</tt>
9
+
10
+ <tt>Shrinker.expand("http://is.gd/xxx")</tt> where xxx is your shrunken URL
11
+
12
+ == Copyright
13
+
14
+ Copyright (c) 2010 Jeff Sutherland. See LICENSE for details.
@@ -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 = "is-gd-shrinker"
8
+ gem.summary = %Q{A simple is.gd URL shrinker and expander for Ruby}
9
+ gem.description = %Q{Use is.gd to shrink and expand URLs for your Ruby application. A one-liner does it all.}
10
+ gem.email = "jefferey.sutherland@gmail.com"
11
+ gem.homepage = "http://github.com/drpentode/is-gd-shrinker"
12
+ gem.authors = ["Jeff Sutherland"]
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 = "is-gd-shrinker #{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
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{is-gd-shrinker}
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 = ["Jeff Sutherland"]
12
+ s.date = %q{2010-06-17}
13
+ s.description = %q{Use is.gd to shrink and expand URLs for your Ruby application. A one-liner does it all.}
14
+ s.email = %q{jefferey.sutherland@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "is-gd-shrinker.gemspec",
25
+ "lib/shrinker.rb",
26
+ "spec/is-gd-shrinker_spec.rb",
27
+ "spec/spec.opts",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/drpentode/is-gd-shrinker}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.7}
34
+ s.summary = %q{A simple is.gd URL shrinker and expander for Ruby}
35
+ s.test_files = [
36
+ "spec/is-gd-shrinker_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
46
+ else
47
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,54 @@
1
+ class Shrinker
2
+ require "open-uri"
3
+ require "net/http"
4
+
5
+ IS_GD_URL = "http://is.gd/api.php?longurl="
6
+
7
+ def self.shrink(url)
8
+ unless url.match(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix)
9
+ raise ShrinkError.new("Supplied URL was not formatted as a URL. Please format it as http://www.domain.com/")
10
+ end
11
+
12
+ encoded_url = URI.encode(IS_GD_URL + url)
13
+
14
+ begin
15
+ shrunken_url = OpenURI.open_uri(encoded_url, 'r') { |file| file.read }
16
+ rescue OpenURI::HTTPError => e
17
+ shrunken_url = nil
18
+ raise ShrinkError.new("No URL was returned from is.gd. There was an error accessing the service.")
19
+ rescue Exception => e
20
+ raise ShrinkError.new("No URL was returned from is.gd. #{e.message} #{e.backtrace}")
21
+ end
22
+
23
+ # We might not get an exception, but the service doesn't return a URL.
24
+ unless shrunken_url == nil || shrunken_url == ""
25
+ return shrunken_url
26
+ else
27
+ raise ShrinkError.new("No URL was returned from is.gd")
28
+ end
29
+ end
30
+
31
+ def self.expand(url)
32
+ url_obj = URI.parse(url)
33
+ unless url.match(/^(http|https):\/\/is.gd\/[a-z0-9]*/)
34
+ raise ShrinkError.new("Supplied URL was not an is.gd URL. Please use a valid is.gd URL.")
35
+ end
36
+
37
+ expanded_url = nil
38
+
39
+ Net::HTTP.start(url_obj.host, url_obj.port) do |http|
40
+ response = http.request_head(url_obj.request_uri)
41
+
42
+ if response.kind_of?(Net::HTTPMovedPermanently)
43
+ expanded_url = response["location"]
44
+ else
45
+ raise ShrinkError.new("Supplied URL was not in the is.gd system. Please us a valid is.gd URL.")
46
+ end
47
+ end
48
+
49
+ return expanded_url
50
+ end
51
+ end
52
+
53
+ class ShrinkError < StandardError
54
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Shrinker" do
4
+ it "should shorten a URL" do
5
+ new_url = Shrinker.shrink("http://www.google.com/")
6
+ new_url.should match(/http:\/\/is.gd\/[a-z]*/)
7
+ end
8
+
9
+ it "should raise a ShrinkError if input URL is not valid" do
10
+ lambda {Shrinker.shrink("boogers")}.should raise_error(ShrinkError, "Supplied URL was not formatted as a URL. Please format it as http://www.domain.com/")
11
+ end
12
+
13
+ # Any Rspec Gurus know how to stub OpenURI.open_uri without passing around a mocked kernel?
14
+ # it "should raise an error if is.gd cannot be reached" do
15
+ # OpenURI.stub!(:open_uri).with(any_args()).and_raise(OpenURI::HTTPError)
16
+ # lambda {Shrinker.shrink("http://www.google.com/")}.should raise_error(ShrinkError, "No URL was returned from is.gd. There was an error accessing the service.")
17
+ # end
18
+
19
+ it "should expand a URL" do
20
+ shrunken_url = Shrinker.shrink("http://www.on-site.com/")
21
+ new_url = Shrinker.expand(shrunken_url)
22
+ new_url.should == "http://www.on-site.com/"
23
+ end
24
+
25
+ it "should raise a ShrinkError if the input URL is not valid" do
26
+ lambda {Shrinker.expand("boogers")}.should raise_error(ShrinkError, "Supplied URL was not an is.gd URL. Please use a valid is.gd URL.")
27
+ end
28
+ end
@@ -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 'shrinker'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is-gd-shrinker
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jeff Sutherland
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-17 00:00:00 -06:00
19
+ default_executable:
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
+ description: Use is.gd to shrink and expand URLs for your Ruby application. A one-liner does it all.
38
+ email: jefferey.sutherland@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - is-gd-shrinker.gemspec
52
+ - lib/shrinker.rb
53
+ - spec/is-gd-shrinker_spec.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/drpentode/is-gd-shrinker
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A simple is.gd URL shrinker and expander for Ruby
90
+ test_files:
91
+ - spec/is-gd-shrinker_spec.rb
92
+ - spec/spec_helper.rb