shorty 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .DS_Store
2
2
  doc/
3
3
  features/
4
+ pkg/
data/README.rdoc CHANGED
@@ -11,7 +11,7 @@ shorty makes interfacing with url shortening services easy, so far the following
11
11
 
12
12
  == Install
13
13
 
14
- sudo gem install drcapulet-shorty
14
+ sudo gem install shorty
15
15
 
16
16
  == Usage
17
17
 
@@ -21,7 +21,7 @@ Yeah just read the rdocs. But to get started, make sure the gem is installed. Re
21
21
 
22
22
  or with Rails:
23
23
 
24
- config.gem 'drcapulet-shorty', :lib => 'shorty'
24
+ config.gem 'shorty'
25
25
 
26
26
  == Possible Additions
27
27
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/shorty.rb CHANGED
@@ -1,11 +1,20 @@
1
1
  require 'httparty'
2
2
  require 'crack'
3
+ require 'ostruct'
3
4
 
4
5
  module Shorty
5
- class API
6
- include HTTParty
6
+ class SimpleAPI
7
+
8
+ def shorten(url)
9
+ self.class.get(self.class::API_URL, :query => self.class.prep_query(url))
10
+ end
11
+
12
+ def self.shorten(url)
13
+ get(self::API_URL, :query => self.prep_query(url))
14
+ end
7
15
  end
8
16
 
17
+ require File.dirname(__FILE__) + '/shorty/to_openstruct'
9
18
  require File.dirname(__FILE__) + '/shorty/trim'
10
19
  require File.dirname(__FILE__) + '/shorty/bitly'
11
20
  require File.dirname(__FILE__) + '/shorty/tinyurl'
data/lib/shorty/bitly.rb CHANGED
@@ -32,21 +32,19 @@ module Shorty
32
32
  def shorten( longurl )
33
33
  query = {:longUrl => longurl}
34
34
  query.merge!(@options)
35
- short = self.class.get('/shorten', :query => query)
36
- short = Crack::JSON.parse(short)
37
- short["errorCode"].zero? ? short["results"][longurl]["shortUrl"] : raise_error(short["errorCode"], short["errorMessage"])
35
+ short = Crack::JSON.parse(self.class.get('/shorten', :query => query))
36
+ short["errorCode"].zero? ? short["results"][longurl]["shortUrl"] : raise_error(short)
38
37
  end
39
38
 
40
39
  # expand- given a bit.ly url, returns long source url, takes:
41
40
  #
42
41
  # - shorturl: the bit.ly url, can either be the full url or missing http://bit.ly
43
42
  def expand(shorturl)
44
- shorturl = gsub_url(shorturl)
45
- query = {:hash => shorturl}
43
+ hash = gsub_url(shorturl)
44
+ query = {:hash => hash}
46
45
  query.merge!(@options)
47
- expand = self.class.get('/expand', :query => query)
48
- expand = Crack::JSON.parse(expand)
49
- expand["errorCode"].zero? ? expand["results"][shorturl]["longUrl"] : raise_error(expand["errorCode"], expand["errorMessage"])
46
+ expand = Crack::JSON.parse(self.class.get('/expand', :query => query))
47
+ expand["errorCode"].zero? ? expand["results"][hash]["longUrl"] : raise_error(expand)
50
48
  end
51
49
 
52
50
  # info - Given a bit.ly url or hash, return information about that page, such as the long source url, ...
@@ -58,9 +56,8 @@ module Shorty
58
56
  query = {:hash => urlhash}
59
57
  end
60
58
  query.merge!(@options)
61
- stats = self.class.get('/info', :query => query)
62
- stats = Crack::JSON.parse(stats)
63
- stats["errorCode"].zero? ? stats["results"][urlhash] : raise_error(stats["errorCode"], stats["errorMessage"])
59
+ stats = Crack::JSON.parse(self.class.get('/info', :query => query))
60
+ stats["errorCode"].zero? ? stats["results"][urlhash] : raise_error(stats)
64
61
  end
65
62
 
66
63
  # stats - get stats on clicks and reffers, pass either:
@@ -70,17 +67,16 @@ module Shorty
70
67
  #
71
68
  # Example:
72
69
  # bitly = Shorty::Bitly.new('login', 'apikey')
73
- # bitly.expand('1RmnUT')
70
+ # bitly.stats('1RmnUT')
74
71
  # Or:
75
72
  # bitly = Shorty::Bitly.new('login', 'apikey')
76
- # bitly.expand('http://bit.ly/1RmnUT')
73
+ # bitly.stats('http://bit.ly/1RmnUT')
77
74
  def stats(urlorhash)
78
75
  urlhash = gsub_url(urlorhash)
79
76
  query = {:hash => urlhash}
80
77
  query.merge!(@options)
81
- stats = self.class.get('/stats', :query => query)
82
- stats = Crack::JSON.parse(stats)
83
- stats["errorCode"].zero? ? stats["results"] : raise_error(stats["errorCode"], stats["errorMessage"])
78
+ stats = Crack::JSON.parse(self.class.get('/stats', :query => query))
79
+ stats["errorCode"].zero? ? stats["results"] : raise_error(stats)
84
80
  end
85
81
 
86
82
 
@@ -88,14 +84,26 @@ module Shorty
88
84
 
89
85
  def gsub_url(shorturl)
90
86
  shorturl.split('/').last
91
- # shorturl = shorturl.gsub(/http:\/\//, '') if shorturl.include?('http://')
92
- # shorturl = shorturl.gsub(/bit.ly\//, '') if shorturl.include?('bit.ly/')
93
87
  end
94
88
 
95
- def raise_error(code, message = '(no error message)')
89
+ def raise_error(hash)
90
+ code = hash["errorCode"]
91
+ message = hash["errorMessage"] || '(no error message)'
96
92
  error = message + " (error code: #{code})"
97
93
  raise Shorty::Bitly::Error, error
98
94
  end
99
95
 
96
+ # We need to work on how to handle this and working with openstruct
97
+ # def handle_response(resp, url)
98
+ # r = {
99
+ # "error" => {
100
+ # "code" => resp["errorCode"],
101
+ # "message" => resp["errorMessage"]
102
+ # },
103
+ # "hash" => resp["results"][url]["hash"]
104
+ # }
105
+ # resp.to_openstruct
106
+ # end
107
+
100
108
  end
101
109
  end
data/lib/shorty/cligs.rb CHANGED
@@ -49,7 +49,7 @@ module Shorty
49
49
 
50
50
  def raise_error(code, message = '(no error message)')
51
51
  error = message + " (error code: #{code})"
52
- raise Shorty::Bitly::Error, error
52
+ raise Shorty::Cligs::Error, error
53
53
  end
54
54
 
55
55
  end
data/lib/shorty/isgd.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  module Shorty
2
2
  # is.gd API as defined http://is.gd/api_info.php
3
- class Isgd
3
+ class Isgd < SimpleAPI
4
4
  include HTTParty
5
5
 
6
- def shorten(url)
7
- self.class.get('http://is.gd/api.php', :query => {:longurl => url})
8
- end
9
-
10
- def self.shorten(url)
11
- get('http://is.gd/api.php', :query => {:longurl => url})
6
+ API_URL = 'http://is.gd/api.php'
7
+
8
+ def self.prep_query(url)
9
+ {:longurl => url}
12
10
  end
13
11
  end
14
12
  end
data/lib/shorty/supr.rb CHANGED
@@ -29,71 +29,66 @@ module Shorty
29
29
  # - url: The long URL you wish to shorten
30
30
  # Will return the full url unless you pass false for the second parameter, then it only returns the hash
31
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
32
+ self.class.get_and_parse_shorten(url, full)
41
33
  end
42
34
 
43
35
  # self.shorten. see shorten
44
36
  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
37
+ get_and_parse_shorten(url, full)
54
38
  end
55
39
 
56
40
  # expand. pass either:
57
41
  # - shortUrl: he Su.pr URL you wish to expand
58
42
  # - hash: The six character hash you wish to expand
59
43
  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"])
44
+ self.class.get_and_parse_expand(urlorhash)
66
45
  end
67
46
 
68
47
  # self.expand. see expand
69
48
  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"])
49
+ get_and_parse_expand(urlorhash)
76
50
  end
77
51
 
78
52
 
79
53
  protected
80
54
 
81
- def gsub_url(shorturl)
82
- shorturl.split('/').last
83
- end
84
-
85
55
  def self.gsub_url(shorturl)
86
56
  shorturl.split('/').last
87
57
  end
88
58
 
89
- def raise_error(code, message = '(no error message)')
59
+ def self.raise_error(hash)
60
+ code = hash["errorCode"]
61
+ message = hash["errorMessage"] || '(no error message)'
90
62
  error = message + " (error code: #{code})"
91
63
  raise Shorty::Supr::Error, error
92
64
  end
93
65
 
94
- def self.raise_error(code, message = '(no error message)')
95
- error = message + " (error code: #{code})"
96
- raise Shorty::Supr::Error, error
66
+ def self.handle_full_or_hash_option(short, url, full)
67
+ short["errorCode"].zero? ? (full ? short["results"][url]["shortUrl"] : short["results"][url]["hash"]) : self.raise_error(short)
68
+ end
69
+
70
+ def self.prep_shorten_request(url)
71
+ query = {:longUrl => url}
72
+ query.merge!(@options) if @options
73
+ query
74
+ end
75
+
76
+ def self.prep_expand_request(urlorhash)
77
+ hash = self.gsub_url(urlorhash)
78
+ query = {:hash => hash}
79
+ query.merge!(@options) if @options
80
+ [query, hash]
81
+ end
82
+
83
+ def self.get_and_parse_shorten(url, full)
84
+ short = Crack::JSON.parse(get('/shorten', :query => prep_shorten_request(url)))
85
+ handle_full_or_hash_option(short, url, full)
86
+ end
87
+
88
+ def self.get_and_parse_expand(urlorhash)
89
+ query, hash = prep_expand_request(urlorhash)
90
+ expand = Crack::JSON.parse(get('/expand', :query => query))
91
+ expand["errorCode"].zero? ? expand["results"][hash]["longUrl"] : raise_error(expand)
97
92
  end
98
93
 
99
94
  end
@@ -1,14 +1,12 @@
1
1
  module Shorty
2
2
  # The tinyurl.com API. Not much here, undocumented API
3
- class Tinyurl
3
+ class Tinyurl < SimpleAPI
4
4
  include HTTParty
5
5
 
6
- def shorten(url)
7
- self.class.get('http://tinyurl.com/api-create.php', :query => {:url => url})
8
- end
6
+ API_URL = 'http://tinyurl.com/api-create.php'
9
7
 
10
- def self.shorten(url)
11
- get('http://tinyurl.com/api-create.php', :query => {:url => url})
8
+ def self.prep_query(url)
9
+ {:url => url}
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,19 @@
1
+ class Object
2
+ def to_openstruct
3
+ self
4
+ end
5
+ end
6
+
7
+ class Array
8
+ def to_openstruct
9
+ map{ |el| el.to_openstruct }
10
+ end
11
+ end
12
+
13
+ class Hash
14
+ def to_openstruct
15
+ mapped = {}
16
+ each{ |key,value| mapped[key] = value.to_openstruct }
17
+ OpenStruct.new(mapped)
18
+ end
19
+ end
data/lib/shorty/trim.rb CHANGED
@@ -67,9 +67,8 @@ module Shorty
67
67
  def trim_url( options = {} )
68
68
  options.merge!(@options)
69
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']
70
+ response = handle_response(Crack::XML.parse(response))
71
+ return response.url
73
72
  end
74
73
 
75
74
  TRIM_ERRORS = {
@@ -104,6 +103,14 @@ module Shorty
104
103
  # raise Shorty::Trim::APIError.new(TRIM_ERRORS[code])
105
104
  raise TRIM_ERRORS[code], message
106
105
  end
106
+
107
+ # Process the response
108
+ def handle_response(resp)
109
+ response = resp['trim'].to_openstruct
110
+ raise_error(response.status.code, response.status.message) if response.status.code >= '205'
111
+ response
112
+ end
113
+
107
114
  end
108
115
  end
109
116
 
data/shorty.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{shorty}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Coomans"]
12
- s.date = %q{2009-10-10}
12
+ s.date = %q{2009-11-23}
13
13
  s.description = %q{Makes it easy to shorten URLs}
14
14
  s.email = %q{alex@alexcoomans.com}
15
15
  s.extra_rdoc_files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/shorty/isgd.rb",
36
36
  "lib/shorty/supr.rb",
37
37
  "lib/shorty/tinyurl.rb",
38
+ "lib/shorty/to_openstruct.rb",
38
39
  "lib/shorty/trim.rb",
39
40
  "lib/shorty/twurl.rb",
40
41
  "shorty.gemspec",
@@ -93,3 +94,4 @@ Gem::Specification.new do |s|
93
94
  s.add_dependency(%q<crack>, [">= 0.1.4"])
94
95
  end
95
96
  end
97
+
data/test/supr_test.rb CHANGED
@@ -49,6 +49,10 @@ class SuprTest < Test::Unit::TestCase
49
49
  should "return a shortened url" do
50
50
  assert_equal 'http://su.pr/2yw2PP', Shorty::Supr.shorten('http://cnn.com')
51
51
  end
52
+
53
+ should "return a shortened hash" do
54
+ assert_equal '2yw2PP', Shorty::Supr.shorten('http://cnn.com', false)
55
+ end
52
56
 
53
57
  should "return an expanded url when passed a hash" do
54
58
  assert_equal 'http://cnn.com/', Shorty::Supr.expand('2yw2PP')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shorty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Coomans
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-10 00:00:00 -07:00
12
+ date: 2009-11-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -60,6 +60,7 @@ files:
60
60
  - lib/shorty/isgd.rb
61
61
  - lib/shorty/supr.rb
62
62
  - lib/shorty/tinyurl.rb
63
+ - lib/shorty/to_openstruct.rb
63
64
  - lib/shorty/trim.rb
64
65
  - lib/shorty/twurl.rb
65
66
  - shorty.gemspec