rubenfonseca-whitme 1.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ruben Fonseca
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,31 @@
1
+ = whitme
2
+
3
+ whitme is a Ruby library for the URL shortener service whit.me
4
+
5
+ The whit.me URL service is available at http://whit.me and it's API at
6
+ http://www.whit.me/api/docs
7
+
8
+ == Features
9
+
10
+ * shortens and expands (multiple) URLs
11
+ * 100% API features implemented
12
+ * json (json-pure for JRuby) as the only external dependency
13
+ * Ruby 1.8, 1.9 and JRuby compatible
14
+
15
+ == Install
16
+
17
+ $ gem sources -a http://gems.github.com
18
+ $ sudo gem install rubenfonseca-whitme
19
+
20
+ == Example
21
+
22
+ require 'whitme'
23
+ url = Whitme.short(:url => 'http://www.google.com', :note => 'foo')
24
+ puts url.hash # the shortened URL
25
+
26
+ url = Whitme.short(:url => ['http://google.com', 'http://sapo.pt'])
27
+ puts url.hash # the shortened URL for multiple URLs
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2009 Ruben Fonseca. See LICENSE for details.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 1
4
+ :patch: 0
data/lib/whitme.rb ADDED
@@ -0,0 +1,169 @@
1
+ # -*- coding: utf-8 -*-
2
+ # vim:tabstop=4:expandtab:sw=4:softtabstop=4
3
+
4
+ require 'rubygems'
5
+ require 'json'
6
+ require 'net/http'
7
+ require 'uri'
8
+ require 'cgi'
9
+
10
+ # == MalformedURLError
11
+ #
12
+ # Raised when the user submits a malformed URL to whit.me
13
+ #
14
+ class MalformedURLError < ArgumentError
15
+ end
16
+
17
+ # == MalformedAliasError
18
+ #
19
+ # Raised when the user submits a malformed alias/hash to whit.me
20
+ #
21
+ class MalformedAliasError < ArgumentError
22
+ end
23
+
24
+ # == WhitmeURL
25
+ #
26
+ # Object that holds the different results of a Whit.me response
27
+ #
28
+ class WhitmeURL
29
+ SHORT_URL = 'http://www.whit.me/api/short?'
30
+ EXPAND_URL = 'http://www.whit.me/api/expand?'
31
+
32
+ # The list of urls
33
+ attr_accessor :url
34
+
35
+ # The list of url notes. Should be the same size of urls
36
+ attr_accessor :urlnote
37
+
38
+ # The note associated with this url
39
+ attr_accessor :note
40
+
41
+ # Contains the shortened URL
42
+ attr_accessor :hash
43
+
44
+ def initialize
45
+ self.url = []
46
+ self.urlnote = []
47
+ end
48
+
49
+ def to_short_url
50
+ params = self.url.map { |u| "url=#{CGI.escape(u)}" }
51
+ params |= self.urlnote.map { |u| "urlnote=#{CGI.escape(u)}" }
52
+ params << "note=#{CGI.escape(self.note)}" if self.note
53
+ params << "hash=#{CGI.escape(self.hash)}" if self.hash
54
+
55
+ SHORT_URL + params.join('&')
56
+ end
57
+
58
+ def to_expand_url
59
+ EXPAND_URL + "hash=#{CGI.escape(self.hash)}"
60
+ end
61
+ end
62
+
63
+ # == Whitme
64
+ #
65
+ # Holds the methods available on the Whit.me API
66
+ #
67
+ # (see http://www.whit.me/api/docs)
68
+ #
69
+ class Whitme
70
+ # Shortens a URL
71
+ #
72
+ # Returns a Whitme instance with the shortened version under the
73
+ # <tt>hash</tt> property.
74
+ #
75
+ # === Parameters
76
+ #
77
+ # * <tt>:url</tt> - a single string or an array of strings containing urls to shorten
78
+ # * <tt>:urlnote</tt> - a single string or an array of strings containing notes associated with the urls
79
+ # * <tt>:note</tt> - a general note to be associated with all the urls shortened
80
+ # * <tt>:hash</tt> - a custom URL alias (for readable shortened URLs). whit.me will return a generated alias if the supplied parameter already exists or is invalid
81
+ #
82
+ # The only really required field is <tt>:url</tt>. If <tt>:urlnotes</tt> is
83
+ # supplied, it must have the same size of <tt>:url</tt>. Otherwise an
84
+ # ArgumentError is raised.
85
+ #
86
+ # === Example
87
+ #
88
+ # require 'whitme'
89
+ # url = Whitme.short(:url => 'http://www.google.com', :note => 'foo')
90
+ # puts url.hash.inspect
91
+ #
92
+ # url = Whitme.short(:url => ['http://google.com', 'http://sapo.pt'])
93
+ # puts url.hash.inspect
94
+ #
95
+ def self.short(options = {})
96
+ # we must have at least on url to short
97
+ raise ArgumentError unless options[:url]
98
+
99
+ # if we have multiple notes, we should have the same number of urls
100
+ if options[:urlnote]
101
+ raise ArgumentError unless options[:urlnote].class == options[:url].class
102
+ if(options[:url].class == Array)
103
+ raise ArgumentError unless options[:url].size == options[:urlnote].size
104
+ end
105
+ end
106
+
107
+ url = WhitmeURL.new
108
+ url.url << options[:url] if options[:url].class == String
109
+ url.url |= options[:url] if options[:url].class == Array
110
+
111
+ if options[:urlnote]
112
+ url.urlnote << options[:urlnote] if options[:urlnote].class == String
113
+ url.urlnote |= options[:urlnote] if options[:urlnote].class == Array
114
+ end
115
+
116
+ url.note = options[:note]
117
+ url.hash = options[:hash]
118
+
119
+ uri = URI.parse(url.to_short_url)
120
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
121
+ http.get(uri.request_uri, { 'Accept' => 'application/json' })
122
+ end
123
+
124
+ json = JSON.parse(res.body) rescue nil
125
+ if json
126
+ if json['error']
127
+ case json['error']
128
+ when 0
129
+ raise MalformedURLError
130
+ when 1
131
+ raise MalformedAliasError
132
+ else
133
+ raise ArgumentError
134
+ end
135
+ else
136
+ url.hash = json['url']
137
+ return url
138
+ end
139
+ end
140
+ end
141
+
142
+ # Expands an URL
143
+ #
144
+ # Returns a Whitme instance with all the (possible) fields filled. Accepts
145
+ # a unique argument with either a hash (e.g. XP45xe3) or a full whit.me
146
+ # URL.
147
+ #
148
+ # === Example
149
+ #
150
+ # require 'whitme'
151
+ # url = Whitme.expand('HQX4P9')
152
+ # puts url.url.inspect
153
+ #
154
+ def self.expand(hash)
155
+ url = WhitmeURL.new
156
+
157
+ if(hash.include? 'http://')
158
+ hash =~ /hash=(.+)/
159
+ hash = $1
160
+ end
161
+ url.hash = hash
162
+
163
+ json = JSON.parse(Net::HTTP.get(URI.parse(url.to_expand_url)))
164
+ url.url = json['urls']
165
+ url.urlnote = json['urlnotes']
166
+ url.note = json['note']
167
+ url
168
+ end
169
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec'
2
+ require 'spec/expectations'
3
+ require 'spec/matchers'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'whitme'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
@@ -0,0 +1,128 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'spec_helper'
3
+
4
+ describe Whitme do
5
+ describe "short operation" do
6
+ describe "with one url" do
7
+ before(:all) do
8
+ @url = 'http://www.google.com'
9
+ @res = Whitme.short(:url => @url)
10
+ end
11
+
12
+ it "should short url" do
13
+ @res.should_not be_nil
14
+ @res.class.should == WhitmeURL
15
+ @res.url.first.should == @url
16
+ @res.urlnote.should == []
17
+ @res.note.should be_nil
18
+ @res.hash.should_not be_nil
19
+ end
20
+ end
21
+
22
+ describe "with one url and a note and a urlnote" do
23
+ before(:all) do
24
+ @url = 'http://www.google.com'
25
+ @note = 'Shortening Google'
26
+ @urlnote = 'THE url to start the internet'
27
+ @res = Whitme.short(:url => @url, :urlnote => @urlnote, :note => @note)
28
+ end
29
+
30
+ it "should short url" do
31
+ @res.should_not be_nil
32
+ @res.class.should == WhitmeURL
33
+ @res.url.first.should == @url
34
+ @res.urlnote.first.should == @urlnote
35
+ @res.note.should == @note
36
+ @res.hash.should_not be_nil
37
+ end
38
+ end
39
+
40
+ describe "with multiple urls and multiple urlnotes" do
41
+ before(:all) do
42
+ @urls = ['http://www.google.com', 'http://www.yahoo.com']
43
+ @urlnotes = ['THE url to start', 'Search engine wannabe']
44
+ @res = Whitme.short(:url => @urls, :urlnote => @urlnotes)
45
+ end
46
+
47
+ it "should short url" do
48
+ @res.should_not be_nil
49
+ @res.class.should == WhitmeURL
50
+ @res.url.should == @urls
51
+ @res.urlnote.should == @urlnotes
52
+ @res.note.should be_nil
53
+ @res.hash.should_not be_nil
54
+ end
55
+ end
56
+
57
+ describe "with a hash/alias" do
58
+ before(:all) do
59
+ @urls = 'http://www.google.com'
60
+ @hash = 'google'
61
+ @res = Whitme.short(:url => @urls, :hash => @hash)
62
+ end
63
+
64
+ it "should short with hash" do
65
+ @res.should_not be_nil
66
+ @res.class.should == WhitmeURL
67
+ @res.url.first.should == @urls
68
+ @res.urlnote.should == []
69
+ @res.note.should be_nil
70
+ @res.hash.should_not be_nil
71
+ end
72
+ end
73
+
74
+ describe "without a url" do
75
+ it "should throw a ArgumentError" do
76
+ lambda { Whitme.short(:url => nil) }.should raise_error(ArgumentError)
77
+ lambda { Whitme.short().should raise_error(ArgumentError) }
78
+ end
79
+ end
80
+
81
+ describe "with different url and urlnotes sizes" do
82
+ it "should throw a ArgumentError" do
83
+ lambda {
84
+ Whitme.short(:url => ['http://www.google.com'],
85
+ :urlnote => ['yes', 'no'])
86
+ }.should raise_error(ArgumentError)
87
+ end
88
+ end
89
+
90
+ describe "with a malformed url" do
91
+ it "should throw a MalformedURLError" do
92
+ lambda {
93
+ Whitme.short(:url => '////////////')
94
+ }.should raise_error(MalformedURLError)
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "expand operation" do
100
+ describe "expanding a hash" do
101
+ before(:all) do
102
+ @res = Whitme.expand('HQX4P9')
103
+ end
104
+
105
+ it "should expand" do
106
+ @res.should_not be_nil
107
+ @res.class.should == WhitmeURL
108
+ @res.url.should == ['http://whit.me/', 'http://whit.me/api/docs']
109
+ @res.urlnote.should == ['main page', 'documentation']
110
+ @res.note.should == 'Some links about whit.me'
111
+ end
112
+ end
113
+
114
+ describe "expading a url" do
115
+ before(:all) do
116
+ @res = Whitme.expand('http://www.whit.me/api/expand?hash=HQX4P9')
117
+ end
118
+
119
+ it "should expand" do
120
+ @res.should_not be_nil
121
+ @res.class.should == WhitmeURL
122
+ @res.url.should == ['http://whit.me/', 'http://whit.me/api/docs']
123
+ @res.urlnote.should == ['main page', 'documentation']
124
+ @res.note.should == 'Some links about whit.me'
125
+ end
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubenfonseca-whitme
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruben Fonseca
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json-pure
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: root@cpan.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - LICENSE
34
+ files:
35
+ - README.rdoc
36
+ - VERSION.yml
37
+ - lib/whitme.rb
38
+ - spec/spec_helper.rb
39
+ - spec/whitme_spec.rb
40
+ - LICENSE
41
+ has_rdoc: true
42
+ homepage: http://github.com/rubenfonseca/whitme
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --inline-source
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: whitme is a Ruby library for the URL shortener service whit.me
68
+ test_files: []
69
+