shorty 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +7 -0
  3. data/README.rdoc +76 -0
  4. data/Rakefile +68 -0
  5. data/VERSION +1 -0
  6. data/features/bitly.feature +4 -0
  7. data/features/step_definitions/api.rb +18 -0
  8. data/features/step_definitions/common_steps.rb +163 -0
  9. data/features/support/common.rb +29 -0
  10. data/features/support/env.rb +14 -0
  11. data/features/support/matchers.rb +11 -0
  12. data/features/trim.feature +8 -0
  13. data/lib/shorty/bitly.rb +101 -0
  14. data/lib/shorty/cligs.rb +56 -0
  15. data/lib/shorty/isgd.rb +14 -0
  16. data/lib/shorty/supr.rb +100 -0
  17. data/lib/shorty/tinyurl.rb +14 -0
  18. data/lib/shorty/trim.rb +110 -0
  19. data/lib/shorty/twurl.rb +32 -0
  20. data/lib/shorty.rb +17 -0
  21. data/shorty.gemspec +95 -0
  22. data/test/bitly_test.rb +32 -0
  23. data/test/cligs_test.rb +18 -0
  24. data/test/fixtures/bitly-expand-cnn.json +10 -0
  25. data/test/fixtures/bitly-shorten-cnn.json +1 -0
  26. data/test/fixtures/bitly-stats-cnn.json +1 -0
  27. data/test/fixtures/cligs-shorten-google.txt +1 -0
  28. data/test/fixtures/isgd-shorten-google.txt +1 -0
  29. data/test/fixtures/supr-expand-cnn-error.json +1 -0
  30. data/test/fixtures/supr-expand-cnn.json +1 -0
  31. data/test/fixtures/supr-shorten-cnn.json +1 -0
  32. data/test/fixtures/tinyurl-shorten-google.txt +1 -0
  33. data/test/fixtures/trim-trim-simple.txt +1 -0
  34. data/test/fixtures/trim-trim-url.xml +7 -0
  35. data/test/fixtures/twurl-shorten-google.txt +1 -0
  36. data/test/isgd_test.rb +18 -0
  37. data/test/shory_test.rb +7 -0
  38. data/test/supr_test.rb +69 -0
  39. data/test/test_helper.rb +9 -0
  40. data/test/tinyurl_test.rb +18 -0
  41. data/test/trim_test.rb +24 -0
  42. data/test/twurl_test.rb +18 -0
  43. metadata +124 -0
@@ -0,0 +1,100 @@
1
+ module Shorty
2
+ # The su.pr API class. API documentation: http://www.stumbleupon.com/developers/Supr:API_documentation/
3
+ class Supr
4
+ attr_reader :options
5
+ include HTTParty
6
+
7
+ # Standard Error Class
8
+ class Error < StandardError; end
9
+
10
+ API_URL = 'http://su.pr/api'
11
+ API_VERSION = '1.0'
12
+ base_uri API_URL
13
+
14
+ # Options to pass, optional, but both must be passed when used
15
+ # - login: Your Su.pr username (also your StumbleUpon username)
16
+ # - apikey: Your private API key. This can be generated on your Su.pr settings page.
17
+ def initialize(login = nil, apikey = nil)
18
+ @options = {
19
+ :version => API_VERSION
20
+ }
21
+ if login && apikey
22
+ @options.merge({:login => login, :apiKey => apikey})
23
+ elsif (login || apikey)
24
+ raise Shorty::Supr::Error, 'Both the API key and login values must be passed when you want to use authentication'
25
+ end
26
+ end
27
+
28
+ # shorten. pass:
29
+ # - url: The long URL you wish to shorten
30
+ # Will return the full url unless you pass false for the second parameter, then it only returns the hash
31
+ def shorten(url, full = true)
32
+ query = {:longUrl => url}
33
+ query.merge!(@options)
34
+ short = self.class.get('/shorten', :query => query)
35
+ short = Crack::JSON.parse(short)
36
+ if full
37
+ short["errorCode"].zero? ? short["results"][url]["shortUrl"] : raise_error(short["errorCode"], short["errorMessage"])
38
+ else
39
+ short["errorCode"].zero? ? short["results"][url]["hash"] : raise_error(short["errorCode"], short["errorMessage"])
40
+ end
41
+ end
42
+
43
+ # self.shorten. see shorten
44
+ def self.shorten(url, full = true)
45
+ query = {:longUrl => url}
46
+ query.merge!(@options) if @options
47
+ short = get('/shorten', :query => query)
48
+ short = Crack::JSON.parse(short)
49
+ if full
50
+ short["errorCode"].zero? ? short["results"][url]["shortUrl"] : raise_error(short["errorCode"], short["errorMessage"])
51
+ else
52
+ short["errorCode"].zero? ? short["results"][url]["hash"] : raise_error(short["errorCode"], short["errorMessage"])
53
+ end
54
+ end
55
+
56
+ # expand. pass either:
57
+ # - shortUrl: he Su.pr URL you wish to expand
58
+ # - hash: The six character hash you wish to expand
59
+ def expand(urlorhash)
60
+ hash = gsub_url(urlorhash)
61
+ query = {:hash => hash}
62
+ query.merge!(@options)
63
+ expand = self.class.get('/expand', :query => query)
64
+ expand = Crack::JSON.parse(expand)
65
+ expand["errorCode"].zero? ? expand["results"][hash]["longUrl"] : raise_error(expand["errorCode"], expand["errorMessage"])
66
+ end
67
+
68
+ # self.expand. see expand
69
+ def self.expand(urlorhash)
70
+ hash = gsub_url(urlorhash)
71
+ query = {:hash => hash}
72
+ query.merge!(@options) if @options
73
+ expand = get('/expand', :query => query)
74
+ expand = Crack::JSON.parse(expand)
75
+ expand["errorCode"].zero? ? expand["results"][hash]["longUrl"] : raise_error(expand["errorCode"], expand["errorMessage"])
76
+ end
77
+
78
+
79
+ protected
80
+
81
+ def gsub_url(shorturl)
82
+ shorturl.split('/').last
83
+ end
84
+
85
+ def self.gsub_url(shorturl)
86
+ shorturl.split('/').last
87
+ end
88
+
89
+ def raise_error(code, message = '(no error message)')
90
+ error = message + " (error code: #{code})"
91
+ raise Shorty::Supr::Error, error
92
+ end
93
+
94
+ def self.raise_error(code, message = '(no error message)')
95
+ error = message + " (error code: #{code})"
96
+ raise Shorty::Supr::Error, error
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,14 @@
1
+ module Shorty
2
+ # The tinyurl.com API. Not much here, undocumented API
3
+ class Tinyurl
4
+ include HTTParty
5
+
6
+ def shorten(url)
7
+ self.class.get('http://tinyurl.com/api-create.php', :query => {:url => url})
8
+ end
9
+
10
+ def self.shorten(url)
11
+ get('http://tinyurl.com/api-create.php', :query => {:url => url})
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,110 @@
1
+
2
+ module Shorty
3
+ # The tr.im API class. API documentation: http://tr.im/website/api
4
+ class Trim
5
+ include HTTParty
6
+ attr_reader :options
7
+ # API Error
8
+ class APIError < StandardError; end
9
+ class APIKeyInvalid < StandardError; end
10
+ class APIKeyRequired < StandardError; end
11
+ # Submitted URL Invalid.
12
+ class URLInvalid < StandardError; end
13
+ # Submitted URL is Already a Shortened URL.
14
+ class AlreadyShortened < StandardError; end
15
+ class URLMissing < StandardError; end
16
+ class URLDoesntExist < StandardError; end
17
+ class URLAlreadyClaimed < StandardError; end
18
+ # The URL has been Flagged as Spam and Rejected.
19
+ class FlaggedSpam < StandardError; end
20
+ # The Custom tr.im URL Requested is Already in Us.
21
+ class AlreadyInUse < StandardError; end
22
+ class AuthenticationInvalid < StandardError; end
23
+ class ReferenceCodeNonExistent< StandardError; end
24
+ # Media
25
+ class MediaTypeUnsupported < StandardError; end
26
+ class MediaTooLarge < StandardError; end
27
+ class MediaInvalidDimensions < StandardError; end
28
+ # Invalid Errors
29
+ class InvalidCharacters < StandardError; end
30
+ class InvalidParameter < StandardError; end
31
+ class UnknownError < StandardError; end
32
+ # Required Parameter URL Not Submitted.
33
+ class MissingParameter < StandardError; end
34
+
35
+
36
+ base_uri 'api.tr.im/api'
37
+
38
+ # TODO: We should take the api_key, username and or password here and authenticate them if needed (need to look into that)
39
+ def initialize(api_key = nil)
40
+ @options = {}
41
+ @options << {:api_key => api_key} if api_key
42
+ end
43
+
44
+ # The trim_simple API method as defined: http://tr.im/website/api#trim_simple
45
+ # Can only be passed a site URL, only returns a URL, and no error handling occurs
46
+ # api = Shorty::Trim.new
47
+ # api.shorten('http://google.com') => http://tr.im/szMj
48
+ def shorten( website_url )
49
+ self.class.get( '/trim_simple', :query => { :url => website_url } )
50
+ end
51
+
52
+
53
+ # The trim_url API method as defined: http://tr.im/website/api#trim_url
54
+ # The following options can be passed:
55
+ #
56
+ # - url: the URL you want to shorten
57
+ # - custom: A custom URL that is preferred to an auto-generated URL.
58
+ # - searchtags: A search string value to attach to a tr.im URL.
59
+ # - privacycode: A string value that must be appended after the URL.
60
+ # - newtrim: If present with any value, it will force the creation of a new tr.im URL.
61
+ # - sandbox: If present with any value a test data set will be returned, and no URL created. This is intended for testing so that you do not consume API calls or insert pointless data while in development.
62
+ #
63
+ # require 'shorty'
64
+ # api = Shorty::Trim.new
65
+ # api.trim_url(:url => 'http://google.com', :custom => 'thisismygoogle', :sandbox => 'true') # => http://tr.im/szMj
66
+
67
+ def trim_url( options = {} )
68
+ options.merge!(@options)
69
+ response = self.class.get( '/trim_url.xml', :query => options )
70
+ response = Crack::XML.parse(response)
71
+ raise_error(response['trim']['status']['code'], response['trim']['status']['message']) if response['trim']['status']['code'] >= '205'
72
+ return response['trim']['url']
73
+ end
74
+
75
+ TRIM_ERRORS = {
76
+ 400 => Shorty::Trim::MissingParameter, # Required Parameter URL Not Submitted.
77
+ 401 => Shorty::Trim::URLInvalid, # Submitted URL Invalid.
78
+ 402 => Shorty::Trim::AlreadyShortened, # Submitted URL is Already a Shortened URL.
79
+ 403 => Shorty::Trim::FlaggedSpam, # The URL has been Flagged as Spam and Rejected.
80
+ 404 => Shorty::Trim::AlreadyInUse, # The Custom tr.im URL Requested is Already in Us.
81
+ 405 => Shorty::Trim::InvalidCharacters, # Requested Custom URL Contains Invalid Characters.
82
+ 406 => Shorty::Trim::InvalidCharacters, # Requested Privacy Code Contains Invalid Characters.
83
+ 407 => Shorty::Trim::InvalidCharacters, # Requested Search Tags Contains Invalid Characters.
84
+ 410 => Shorty::Trim::AuthenticationInvalid, # Required Authentication Not Submitted or Invalid.
85
+ 411 => Shorty::Trim::MissingParameter, # URL Reference Code Not Submitted.
86
+ 412 => Shorty::Trim::ReferenceCodeNonExistent, # URL Reference Code Not Does Not Exist.
87
+ 413 => Shorty::Trim::URLMissing, # tr.im URL Path Not Submitted.
88
+ 414 => Shorty::Trim::URLDoesntExist, # tr.im URL Does Not Exist.
89
+ 415 => Shorty::Trim::URLAlreadyClaimed, # tr.im URL Referenced Already Claimed.
90
+ 420 => Shorty::Trim::MediaTypeUnsupported, # Media Type Uploaded Not Supported.
91
+ 421 => Shorty::Trim::MediaTooLarge, # Media Uploaded too Large.
92
+ 422 => Shorty::Trim::MediaInvalidDimensions, # Media Uploaded XY Dimensions too Small.
93
+ 425 => Shorty::Trim::APIError, # API Rate Limit Exceeded.
94
+ 426 => Shorty::Trim::APIKeyInvalid, # API Key Submitted Does Not Exist or is Invalid.
95
+ 427 => Shorty::Trim::APIKeyRequired, # API Key Required.
96
+ 445 => Shorty::Trim::InvalidParameter, # Parameter Data Within the Request is Invalid.
97
+ 446 => Shorty::Trim::MissingParameter, # "Required Parameter Missing within the Request."
98
+ 450 => Shorty::Trim::UnknownError # "An Unknown Error Occurred. Please email api@tr.im."
99
+ }
100
+
101
+ private
102
+ # Raise an error depending on the problem
103
+ def raise_error(code, message = '(no error message)')
104
+ # raise Shorty::Trim::APIError.new(TRIM_ERRORS[code])
105
+ raise TRIM_ERRORS[code], message
106
+ end
107
+ end
108
+ end
109
+
110
+
@@ -0,0 +1,32 @@
1
+ module Shorty
2
+ # The twurl.nl API class. API documentation: http://tweetburner.com/api
3
+ class Twurl
4
+ attr_reader :options
5
+ include HTTParty
6
+
7
+ # Standard Error Class
8
+ class Error < StandardError; end
9
+
10
+ API_URL = 'http://tweetburner.com'
11
+ base_uri API_URL
12
+
13
+ # shorten. pass:
14
+ # - url: the url to be shortened
15
+ # So to use:
16
+ # twurl = Shorty::Truwl.new
17
+ # twurl.shorten('http://google.com')
18
+ def shorten(url)
19
+ query = { :'link[url]' => url }
20
+ self.class.post("/links", :query => query)
21
+ end
22
+
23
+ # Same as the shorten method, you can just call the following instead:
24
+ #
25
+ # Shorty::Truwl.shorten('http://google.com')
26
+ def self.shorten(url)
27
+ query = { :'link[url]' => url }
28
+ post("/links", :query => query)
29
+ end
30
+
31
+ end
32
+ end
data/lib/shorty.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'httparty'
2
+ require 'crack'
3
+
4
+ module Shorty
5
+ class API
6
+ include HTTParty
7
+ end
8
+
9
+ require File.dirname(__FILE__) + '/shorty/trim'
10
+ require File.dirname(__FILE__) + '/shorty/bitly'
11
+ require File.dirname(__FILE__) + '/shorty/tinyurl'
12
+ require File.dirname(__FILE__) + '/shorty/isgd'
13
+ require File.dirname(__FILE__) + '/shorty/cligs'
14
+ require File.dirname(__FILE__) + '/shorty/twurl'
15
+ require File.dirname(__FILE__) + '/shorty/supr'
16
+ end
17
+
data/shorty.gemspec ADDED
@@ -0,0 +1,95 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
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{shorty}
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 = ["Alex Coomans"]
12
+ s.date = %q{2009-10-10}
13
+ s.description = %q{Makes it easy to shorten URLs}
14
+ s.email = %q{alex@alexcoomans.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "features/bitly.feature",
26
+ "features/step_definitions/api.rb",
27
+ "features/step_definitions/common_steps.rb",
28
+ "features/support/common.rb",
29
+ "features/support/env.rb",
30
+ "features/support/matchers.rb",
31
+ "features/trim.feature",
32
+ "lib/shorty.rb",
33
+ "lib/shorty/bitly.rb",
34
+ "lib/shorty/cligs.rb",
35
+ "lib/shorty/isgd.rb",
36
+ "lib/shorty/supr.rb",
37
+ "lib/shorty/tinyurl.rb",
38
+ "lib/shorty/trim.rb",
39
+ "lib/shorty/twurl.rb",
40
+ "shorty.gemspec",
41
+ "test/bitly_test.rb",
42
+ "test/cligs_test.rb",
43
+ "test/fixtures/bitly-expand-cnn.json",
44
+ "test/fixtures/bitly-shorten-cnn.json",
45
+ "test/fixtures/bitly-stats-cnn.json",
46
+ "test/fixtures/cligs-shorten-google.txt",
47
+ "test/fixtures/isgd-shorten-google.txt",
48
+ "test/fixtures/supr-expand-cnn-error.json",
49
+ "test/fixtures/supr-expand-cnn.json",
50
+ "test/fixtures/supr-shorten-cnn.json",
51
+ "test/fixtures/tinyurl-shorten-google.txt",
52
+ "test/fixtures/trim-trim-simple.txt",
53
+ "test/fixtures/trim-trim-url.xml",
54
+ "test/fixtures/twurl-shorten-google.txt",
55
+ "test/isgd_test.rb",
56
+ "test/shory_test.rb",
57
+ "test/supr_test.rb",
58
+ "test/test_helper.rb",
59
+ "test/tinyurl_test.rb",
60
+ "test/trim_test.rb",
61
+ "test/twurl_test.rb"
62
+ ]
63
+ s.homepage = %q{http://github.com/drcapulet/shorty}
64
+ s.rdoc_options = ["--charset=UTF-8"]
65
+ s.require_paths = ["lib"]
66
+ s.rubygems_version = %q{1.3.5}
67
+ s.summary = %q{Gem that talks to APIs for shortening urls}
68
+ s.test_files = [
69
+ "test/bitly_test.rb",
70
+ "test/cligs_test.rb",
71
+ "test/isgd_test.rb",
72
+ "test/shory_test.rb",
73
+ "test/supr_test.rb",
74
+ "test/test_helper.rb",
75
+ "test/tinyurl_test.rb",
76
+ "test/trim_test.rb",
77
+ "test/twurl_test.rb"
78
+ ]
79
+
80
+ if s.respond_to? :specification_version then
81
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
82
+ s.specification_version = 3
83
+
84
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
85
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.4"])
86
+ s.add_runtime_dependency(%q<crack>, [">= 0.1.4"])
87
+ else
88
+ s.add_dependency(%q<httparty>, [">= 0.4.4"])
89
+ s.add_dependency(%q<crack>, [">= 0.1.4"])
90
+ end
91
+ else
92
+ s.add_dependency(%q<httparty>, [">= 0.4.4"])
93
+ s.add_dependency(%q<crack>, [">= 0.1.4"])
94
+ end
95
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class BitlyTest < Test::Unit::TestCase
4
+ context "A bit.ly instance" do
5
+ setup do
6
+ FakeWeb.register_uri(:get, "http://api.bit.ly/shorten?apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&longUrl=http%3A%2F%2Fcnn.com&login=bitlyapidemo", :body => File.join(File.dirname(__FILE__), 'fixtures', 'bitly-shorten-cnn.json'))
7
+ FakeWeb.register_uri(:get, "http://api.bit.ly/expand?apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&login=bitlyapidemo&hash=15DlK", :body => File.join(File.dirname(__FILE__), 'fixtures', 'bitly-expand-cnn.json'))
8
+ FakeWeb.register_uri(:get, "http://api.bit.ly/stats?hash=15DlK&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&login=bitlyapidemo", :body => File.join(File.dirname(__FILE__), 'fixtures', 'bitly-stats-cnn.json'))
9
+ @bitly = Shorty::Bitly.new('bitlyapidemo', 'R_0da49e0a9118ff35f52f629d2d71bf07')
10
+ end
11
+
12
+ should "exist" do
13
+ assert @bitly
14
+ end
15
+
16
+ should "return a shortened url" do
17
+ assert_equal 'http://bit.ly/15DlK', @bitly.shorten('http://cnn.com')
18
+ end
19
+
20
+ should "return an expanded url when passed a hash" do
21
+ assert_equal 'http://cnn.com/', @bitly.expand('15DlK')
22
+ end
23
+
24
+ should "return an expanded url when passed a full url" do
25
+ assert_equal 'http://cnn.com/', @bitly.expand('http://bit.ly/15DlK')
26
+ end
27
+
28
+ should "return the stats for a url" do
29
+ assert_equal 3046, @bitly.stats('http://bit.ly/15DlK')["clicks"]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class CligsTest < Test::Unit::TestCase
4
+ context "A cli.gs instance" do
5
+ setup do
6
+ FakeWeb.register_uri(:get, "http://cli.gs/api/v1/cligs/create?url=http%3A%2F%2Fgoogle.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'cligs-shorten-google.txt'))
7
+ @cligs = Shorty::Cligs.new
8
+ end
9
+
10
+ should "exist" do
11
+ assert @cligs
12
+ end
13
+
14
+ should "return a shortened url" do
15
+ assert_equal 'http://cli.gs/g5nmE', @cligs.shorten('http://google.com')
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "errorCode": 0,
3
+ "errorMessage": "",
4
+ "results": {
5
+ "15DlK": {
6
+ "longUrl": "http://cnn.com/"
7
+ }
8
+ },
9
+ "statusCode": "OK"
10
+ }
@@ -0,0 +1 @@
1
+ { "errorCode": 0, "errorMessage": "", "results": { "http://cnn.com": { "hash": "31IqMl", "shortKeywordUrl": "", "shortUrl": "http://bit.ly/15DlK", "userHash": "15DlK" } }, "statusCode": "OK" }
@@ -0,0 +1 @@
1
+ { "errorCode": 0, "errorMessage": "", "results": { "clicks": 3046, "hash": "31IqMl", "referrers": { "": { "/index.html": 1, "None": 5, "direct": 2255 }, "api.bit.ly": { "/shorten": 2 }, "benny-web": { "/jseitel/bitly/install.html": 1 }, "bit.ly": { "/": 3, "/app/demos/info.html": 102, "/app/demos/stats.html": 402, "/app/demos/statsModule.html": 2, "/info/14tP6": 1, "/info/15DlK": 1, "/info/27I9Ll": 1, "/info/31IqMl": 4, "/info/J066u": 1, "/info/SiNn6": 1, "/info/hic4E": 1 }, "code.google.com": { "/p/bitly-api/wiki/ApiDocumentation": 1 }, "dev.chartbeat.com": { "/static/bitly.html": 1 }, "dev.unhub.com": { "/pnG8/": 1 }, "klout.net": { "/profiledetail.php": 1 }, "localhost": { "/New Folder/test1.php": 1, "/index2.html": 1 }, "mail.google.com": { "/mail/": 1 }, "partners.bit.ly": { "/td": 9 }, "powertwitter.me": { "/": 1 }, "quietube.com": { "/getbitly.php": 1 }, "search.twitter.com": { "/search": 3 }, "sfbay.craigslist.org": { "/sfc/rnr/891043787.html": 8 }, "spreadsheets.google.com": { "/ccc": 27, "/ccc2": 1 }, "strat.corp.advertising.com": { "/brandmaker/bitly.php": 4 }, "taggytext.com": { "/ganja": 1 }, "twitter.com": { "/": 11, "/SuperTestAcct": 2, "/TattoosOn": 1, "/WilliamWoods": 1, "/home": 6, "/ibiboisms": 2, "/kshanns": 1, "/matraji": 1, "/nathanfolkman": 1, "/pantaleonescu": 1, "/rubyisbeautiful": 1, "/williamwoods": 1 }, "twitter.mattz.dev.buddymedia.com": { "/twitter/": 1 }, "twitturls.com": { "/": 21 }, "twitturly.com": { "": 17, "/urlinfo/url/2168a5e81280538cdbf6ad4c4ab019db/": 1 }, "uploaddownloadperform.net": { "/Test/TestPage2": 1 }, "url.ly": { "/": 19, "/info/27I9Ll": 1, "/info/31IqMl": 3 }, "urly.local:3600": { "/": 7 }, "v2.blogg.no": { "/test.cfm": 1 }, "www.facebook.com": { "/home.php": 1 }, "www.longurlplease.com": { "/": 3 }, "www.microblogbuzz.com": { "": 5 } }, "userClicks": 373, "userHash": "15DlK", "userReferrers": { "": { "None": 1, "direct": 366 }, "api.bit.ly": { "/shorten": 2 }, "code.google.com": { "/p/bitly-api/wiki/ApiDocumentation": 1 }, "twitter.com": { "/TattoosOn": 1, "/rubyisbeautiful": 1 }, "www.longurlplease.com": { "/": 1 } } }, "statusCode": "OK" }
@@ -0,0 +1 @@
1
+ http://cli.gs/g5nmE
@@ -0,0 +1 @@
1
+ http://is.gd/2iE2G
@@ -0,0 +1 @@
1
+ {"errorCode":1204,"errorMessage":"Invalid Su.pr hash","statusCode":"ERROR"}
@@ -0,0 +1 @@
1
+ {"errorCode":0,"errorMessage":"","results":{"2yw2PP":{"longUrl":"http:\/\/cnn.com\/"}},"statusCode":"OK"}
@@ -0,0 +1 @@
1
+ {"errorCode":0,"errorMessage":"","results":{"http:\/\/cnn.com":{"hash":"2yw2PP","shortUrl":"http:\/\/su.pr\/2yw2PP"}},"statusCode":"OK"}
@@ -0,0 +1 @@
1
+ http://tinyurl.com/2x6rgl
@@ -0,0 +1 @@
1
+ http://tr.im/w5Pm
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <trim>
3
+ <status result="OK" code="200" message="tr.im URL Added."/>
4
+ <url>http://tr.im/thisismygoogletesting</url>
5
+ <reference>CRzMDvkYP7VYKrpBWsUAU4fAbT7MUg</reference>
6
+ <trimpath>thisismygoogletesting</trimpath>
7
+ </trim>
@@ -0,0 +1 @@
1
+ http://twurl.nl/jnlwyb
data/test/isgd_test.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class IsgdTest < Test::Unit::TestCase
4
+ context "Using is.gd" do
5
+ setup do
6
+ FakeWeb.register_uri(:get, "http://is.gd/api.php?longurl=http%3A%2F%2Fgoogle.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'isgd-shorten-google.txt'))
7
+ end
8
+
9
+ should "return a shortened url (class method)" do
10
+ assert_equal 'http://is.gd/2iE2G', Shorty::Isgd.shorten('http://google.com')
11
+ end
12
+
13
+ should "return a shortened url (instance method)" do
14
+ @isgd = Shorty::Isgd.new
15
+ assert_equal 'http://is.gd/2iE2G', @isgd.shorten('http://google.com')
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ShortyTest < Test::Unit::TestCase
4
+ should "do nothing" do
5
+ true
6
+ end
7
+ end
data/test/supr_test.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class SuprTest < Test::Unit::TestCase
4
+ context "A su.pr instance" do
5
+ setup do
6
+ FakeWeb.register_uri(:get, "http://su.pr/api/shorten?version=1.0&longUrl=http%3A%2F%2Fcnn.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'supr-shorten-cnn.json'))
7
+ FakeWeb.register_uri(:get, "http://su.pr/api/shorten?longUrl=http%3A%2F%2Fcnn.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'supr-shorten-cnn.json'))
8
+ FakeWeb.register_uri(:get, "http://su.pr/api/expand?hash=2yw2PP&version=1.0", :body => File.join(File.dirname(__FILE__), 'fixtures', 'supr-expand-cnn.json'))
9
+ FakeWeb.register_uri(:get, "http://su.pr/api/expand?hash=2yw2PP", :body => File.join(File.dirname(__FILE__), 'fixtures', 'supr-expand-cnn.json'))
10
+ FakeWeb.register_uri(:get, "http://su.pr/api/expand?hash=15DlK&version=1.0", :body => File.join(File.dirname(__FILE__), 'fixtures', 'supr-expand-cnn-error.json'))
11
+ FakeWeb.register_uri(:get, "http://su.pr/api/expand?hash=15DlK", :body => File.join(File.dirname(__FILE__), 'fixtures', 'supr-expand-cnn-error.json'))
12
+ end
13
+
14
+ context "when using instance methods" do
15
+ setup do
16
+ @supr = Shorty::Supr.new
17
+ end
18
+
19
+ should "exist" do
20
+ assert @supr
21
+ end
22
+
23
+ should "return a shortened url" do
24
+ assert_equal 'http://su.pr/2yw2PP', @supr.shorten('http://cnn.com')
25
+ end
26
+
27
+ should "return an expanded url when passed a hash" do
28
+ assert_equal 'http://cnn.com/', @supr.expand('2yw2PP')
29
+ end
30
+
31
+ should "return an expanded url when passed a full url" do
32
+ assert_equal 'http://cnn.com/', @supr.expand('http://su.pr/2yw2PP')
33
+ end
34
+
35
+ should "raise an error when passed an invalid hash to expand" do
36
+ assert_raise Shorty::Supr::Error do
37
+ @supr.expand('15DlK')
38
+ end
39
+ end
40
+
41
+ should "raise an error when an instance is being created and is missing a paramter" do
42
+ assert_raise Shorty::Supr::Error do
43
+ Shorty::Supr.new('dummyapi')
44
+ end
45
+ end
46
+ end
47
+
48
+ context "when using class methods" do
49
+ should "return a shortened url" do
50
+ assert_equal 'http://su.pr/2yw2PP', Shorty::Supr.shorten('http://cnn.com')
51
+ end
52
+
53
+ should "return an expanded url when passed a hash" do
54
+ assert_equal 'http://cnn.com/', Shorty::Supr.expand('2yw2PP')
55
+ end
56
+
57
+ should "return an expanded url when passed a full url" do
58
+ assert_equal 'http://cnn.com/', Shorty::Supr.expand('http://su.pr/2yw2PP')
59
+ end
60
+
61
+ should "return an error when passed an invalid hash to expand" do
62
+ assert_raise Shorty::Supr::Error do
63
+ Shorty::Supr.expand('15DlK')
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'fakeweb'
5
+ FakeWeb.allow_net_connect = false
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'shorty'
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class TinyurlTest < Test::Unit::TestCase
4
+ context "Using tinyurl.com" do
5
+ setup do
6
+ FakeWeb.register_uri(:get, "http://tinyurl.com/api-create.php?url=http%3A%2F%2Fgoogle.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'tinyurl-shorten-google.txt'))
7
+ end
8
+
9
+ should "return a shortened url (class method)" do
10
+ assert_equal 'http://tinyurl.com/2x6rgl', Shorty::Tinyurl.shorten('http://google.com')
11
+ end
12
+
13
+ should "return a shortened url (instance method)" do
14
+ @tinyurl = Shorty::Tinyurl.new
15
+ assert_equal 'http://tinyurl.com/2x6rgl', @tinyurl.shorten('http://google.com')
16
+ end
17
+ end
18
+ end
data/test/trim_test.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class TrimTest < Test::Unit::TestCase
4
+ context "A tr.im instance" do
5
+ setup do
6
+ FakeWeb.register_uri(:get, "http://api.tr.im/api/trim_simple?url=http%3A%2F%2Fgoogle.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'trim-trim-simple.txt'))
7
+ FakeWeb.register_uri(:get, "http://api.tr.im/api/trim_url.xml?url=http%3A%2F%2Fgoogle.com&custom=thisismygoogletesting", :body => File.join(File.dirname(__FILE__), 'fixtures', 'trim-trim-url.xml'))
8
+ # FakeWeb.register_uri(:get, "http://api.bit.ly/stats?hash=15DlK&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&version=2.0.1&login=bitlyapidemo", :body => File.join(File.dirname(__FILE__), 'fixtures', 'bitly-stats-cnn.json'))
9
+ @trim = Shorty::Trim.new
10
+ end
11
+
12
+ should "exist" do
13
+ assert @trim
14
+ end
15
+
16
+ should "return a shortened url using trim_simple" do
17
+ assert_equal 'http://tr.im/w5Pm', @trim.shorten('http://google.com')
18
+ end
19
+
20
+ should "return a shortened url using trim_url" do
21
+ assert_equal 'http://tr.im/thisismygoogletesting', @trim.trim_url(:url => 'http://google.com', :custom => 'thisismygoogletesting')
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class TwurlTest < Test::Unit::TestCase
4
+ context "Using twurl.nl" do
5
+ setup do
6
+ FakeWeb.register_uri(:post, "http://tweetburner.com/links?link[url]=http%3A%2F%2Fgoogle.com", :body => File.join(File.dirname(__FILE__), 'fixtures', 'twurl-shorten-google.txt'))
7
+ end
8
+
9
+ should "return a shortened url (class method)" do
10
+ assert_equal 'http://twurl.nl/jnlwyb', Shorty::Twurl.shorten('http://google.com')
11
+ end
12
+
13
+ should "return a shortened url (instance method)" do
14
+ @twurl = Shorty::Twurl.new
15
+ assert_equal 'http://twurl.nl/jnlwyb', @twurl.shorten('http://google.com')
16
+ end
17
+ end
18
+ end