em-shorturl 0.1.0.pre → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/em-shorturl/bitly.rb +31 -1
- data/lib/em-shorturl/google.rb +8 -0
- data/lib/em-shorturl/tinyurl.rb +12 -3
- data/lib/em-shorturl/version.rb +1 -1
- metadata +4 -4
data/lib/em-shorturl/bitly.rb
CHANGED
@@ -5,13 +5,32 @@ module EventMachine
|
|
5
5
|
module ShortURL
|
6
6
|
|
7
7
|
##
|
8
|
-
# Driver for bitly.com URL Shortening service.
|
8
|
+
# Driver for bitly.com URL Shortening service. Bitly requires API users
|
9
|
+
# to have an account. This can be provided to the driver as either a
|
10
|
+
# pre-obtained access token or as a username and password pair
|
11
|
+
# (optionally with a client id and secret). If a username and password
|
12
|
+
# are provided instead of an access token, login is differed until
|
13
|
+
# needed (first call to +shorten+) or can be explicitly called via the
|
14
|
+
# +login+ function.
|
9
15
|
|
10
16
|
class Bitly
|
11
17
|
include EventMachine::Deferrable
|
12
18
|
API_OAUTH_URL = 'https://api-ssl.bitly.com/oauth/access_token'
|
13
19
|
API_SHORTEN_URL = 'https://api-ssl.bitly.com/v3/shorten'
|
14
20
|
|
21
|
+
|
22
|
+
##
|
23
|
+
# Initialize the driver with account details. Despite being
|
24
|
+
# required by bitly, the driver does not currently verify that
|
25
|
+
# proper account details are given.
|
26
|
+
#
|
27
|
+
# +account+ takes the following options:
|
28
|
+
# :client_id The client ID of the registered application
|
29
|
+
# :client_secret The secret for the client id
|
30
|
+
# :username A username to use for basic authentication
|
31
|
+
# :password The password to use for basic authentication
|
32
|
+
# :token A previously obtained access token to use
|
33
|
+
|
15
34
|
def initialize(account={})
|
16
35
|
@client_id = account[:client_id]
|
17
36
|
@client_secret = account[:client_secret]
|
@@ -21,6 +40,11 @@ module EventMachine
|
|
21
40
|
@deferrable_args = [self]
|
22
41
|
end
|
23
42
|
|
43
|
+
|
44
|
+
##
|
45
|
+
# Shortens the given URL. If no access token is known, will call
|
46
|
+
# the login function and upon success will call shorten again.
|
47
|
+
|
24
48
|
def shorten(url)
|
25
49
|
if @access_token.nil?
|
26
50
|
login { shorten(url) }
|
@@ -34,6 +58,12 @@ module EventMachine
|
|
34
58
|
self
|
35
59
|
end
|
36
60
|
|
61
|
+
|
62
|
+
##
|
63
|
+
# Uses the given username and password to obtain an access token
|
64
|
+
# from bitly. If authentication fails, sets the driver's deferrable
|
65
|
+
# status to failed.
|
66
|
+
|
37
67
|
def login
|
38
68
|
params = {
|
39
69
|
:head => { 'authorization' => [@username, @password] },
|
data/lib/em-shorturl/google.rb
CHANGED
@@ -80,6 +80,14 @@ module EventMachine
|
|
80
80
|
def on_success(http)
|
81
81
|
response = nil
|
82
82
|
|
83
|
+
# Handle HTTP Status other than 200
|
84
|
+
if http.response_header.status != 200
|
85
|
+
error = http.response_header.http_reason
|
86
|
+
fail(error, *@deferrable_args)
|
87
|
+
return
|
88
|
+
end
|
89
|
+
|
90
|
+
|
83
91
|
# Handle JSON Failure
|
84
92
|
begin
|
85
93
|
response = JSON.parse(http.response)
|
data/lib/em-shorturl/tinyurl.rb
CHANGED
@@ -5,7 +5,7 @@ module EventMachine
|
|
5
5
|
class TinyURL
|
6
6
|
include EventMachine::Deferrable
|
7
7
|
|
8
|
-
# TinyURL API
|
8
|
+
# TinyURL API URL
|
9
9
|
API_URL = 'http://tinyurl.com/api-create.php'
|
10
10
|
|
11
11
|
|
@@ -18,6 +18,10 @@ module EventMachine
|
|
18
18
|
end
|
19
19
|
|
20
20
|
|
21
|
+
##
|
22
|
+
# Shortens the given URL, returning self. The shortened URL is
|
23
|
+
# passed to the success callback as the first parameter.
|
24
|
+
|
21
25
|
def shorten(url)
|
22
26
|
params = { :query => { :url => url } }
|
23
27
|
request = EM::HttpRequest.new(API_URL).post(params)
|
@@ -25,6 +29,7 @@ module EventMachine
|
|
25
29
|
request.errback(&method(:on_error))
|
26
30
|
self
|
27
31
|
end
|
32
|
+
|
28
33
|
private
|
29
34
|
|
30
35
|
|
@@ -33,8 +38,12 @@ module EventMachine
|
|
33
38
|
# just be the plaintest link.
|
34
39
|
|
35
40
|
def on_success(http)
|
36
|
-
|
37
|
-
|
41
|
+
if http.response_header.status != 200
|
42
|
+
fail(http.response_header.http_reason, *@deferrable_args)
|
43
|
+
else
|
44
|
+
short_url = http.response
|
45
|
+
succeed(short_url, *@deferrable_args)
|
46
|
+
end
|
38
47
|
end
|
39
48
|
|
40
49
|
|
data/lib/em-shorturl/version.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-shorturl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nik Johnson
|
@@ -59,9 +59,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
60
|
none: false
|
61
61
|
requirements:
|
62
|
-
- - ! '
|
62
|
+
- - ! '>='
|
63
63
|
- !ruby/object:Gem::Version
|
64
|
-
version:
|
64
|
+
version: '0'
|
65
65
|
requirements: []
|
66
66
|
rubyforge_project:
|
67
67
|
rubygems_version: 1.8.24
|