em-shorturl 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 67a5cd1bf46ff96a3c6a00d651b954229f11e7b2
4
+ data.tar.gz: 778cb1c98b3a1aad091abc7a5eae7db018c6c387
5
+ SHA512:
6
+ metadata.gz: 86bc8edf6aa87d139397e3c0f1369d44590e63f958a525f147f59afde00e858cedb2eda16e59e8b3eb492156a16fed2046c47c6d93e53139288ee922785a1856
7
+ data.tar.gz: 4a9d75b5909ae617b607afdadc5390c9ccc2765777f4ed3c91315be05431aabf5cff944e2ebac6c85abc45497ff7d179ff11ca3ca93abc24383b59f4ca5a3c3b
@@ -0,0 +1,111 @@
1
+ require 'em-http-request'
2
+ require 'json'
3
+
4
+ module EventMachine
5
+ module ShortURL
6
+
7
+ ##
8
+ # The Google class is the driver for URLShortener to use Google's API
9
+ # for URL shortening.
10
+
11
+ class Google
12
+ include EventMachine::Deferrable
13
+
14
+ # Specifies the API URL for shortening URLs
15
+ API_URL = "https://www.googleapis.com/urlshortener/v1/url"
16
+
17
+
18
+ ##
19
+ # Takes the URL to shorten and optionally two callbacks for success
20
+ # and error. Does not immediately shorten the URL.
21
+ #
22
+ # The +account+ argument may take the following options:
23
+ # :apikey Defines an apikey to use for the request
24
+
25
+ def initialize(account={})
26
+ @deferrable_args = [self]
27
+ @account_apikey = account[:apikey]
28
+ end
29
+
30
+
31
+ ##
32
+ # Performs the request of shortening a URL asynchronously.
33
+
34
+ def shorten(url)
35
+ params = get_request_parameters(url)
36
+ request = EventMachine::HttpRequest.new(API_URL).post(params)
37
+ request.callback(&method(:on_success))
38
+ request.errback(&method(:on_error))
39
+ self
40
+ end
41
+
42
+
43
+ private
44
+
45
+
46
+ ##
47
+ # Creates the parameter hash for the HTTP Request to Google
48
+
49
+ def get_request_parameters(url)
50
+ options = {
51
+ :head => { "Content-Type" => "application/json" },
52
+ :body => { "longUrl" => url }.to_json
53
+ }
54
+
55
+ if @account_apikey
56
+ options[:query] = { "key" => @account_apikey }
57
+ end
58
+
59
+ options
60
+ end
61
+
62
+ ##
63
+ # Callback for HttpRequest object upon success. Parses the response
64
+ # as JSON, checks for a Google API error, and finally saves the
65
+ # short URL as an instance variable. If any of the above fail,
66
+ # sets the error message and sets the deferrable status to failure.
67
+
68
+ def on_success(http)
69
+ response = nil
70
+
71
+ # Handle JSON Failure
72
+ begin
73
+ response = JSON.parse(http.response)
74
+ rescue JSON::ParseError => e
75
+ error = e.message
76
+ fail(error, *@deferrable_args)
77
+ return
78
+ end
79
+
80
+ # Handle google API Error
81
+ if response['error']
82
+ error = response['error']['message']
83
+ fail(error, *@deferrable_args)
84
+ return
85
+ end
86
+
87
+ # Odd case if response['id'] doesn't exist (Should't happen)
88
+ if response['id'].nil?
89
+ error = "No short URL was returned (Google API Change?)"
90
+ fail(error, *@deferrable_args)
91
+ end
92
+
93
+ # Return the short URL
94
+ short_url = response['id']
95
+ succeed(short_url, *@deferrable_args)
96
+ end
97
+
98
+
99
+ ##
100
+ # Callback for an error from HttpRequest (caused by a server
101
+ # outage of lack of connectivity). Simply forwards the error
102
+ # value and sets the deferrable status to fail.
103
+
104
+ def on_error(http)
105
+ error = http.error
106
+ fail(error, *@deferrable_args)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
@@ -0,0 +1,5 @@
1
+ module EventMachine
2
+ module ShortURL
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'eventmachine'
2
+ require 'em-shorturl/version'
3
+ require 'em-shorturl/google'
4
+
5
+ module EventMachine
6
+ module ShortURL
7
+ DEFAULT_DRIVER = :google
8
+ DRIVERS = {
9
+ :google => ShortURL::Google
10
+ }
11
+
12
+ def self.shorten(url, driver=DEFAULT_DRIVER, account={})
13
+ raise KeyError, "Driver does not exist" unless DRIVERS[driver]
14
+
15
+ r = DRIVERS[driver].new(account)
16
+ r.shorten(url)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ require "test/unit"
2
+ require "em-shorturl/google"
3
+
4
+ class TestGoogle < Test::Unit::TestCase
5
+ TEST_URL = 'http://google.com'
6
+ API_ENV_VAR = 'google_apikey'
7
+
8
+ def test_no_auth
9
+ EM.run do
10
+ google = EM::ShortURL::Google.new
11
+ set_asserts(google)
12
+ google.shorten(TEST_URL)
13
+ end
14
+ end
15
+
16
+ if ENV[API_ENV_VAR]
17
+ def test_with_apikey
18
+
19
+ EM.run do
20
+
21
+ google = EM::ShortURL::Google.new(:apikey => ENV[API_ENV_VAR])
22
+ set_asserts(google)
23
+ google.shorten(TEST_URL)
24
+ end
25
+ end
26
+ else
27
+ warn "Skipping test_with_apikey: No '#{API_ENV_VAR}' environment variable set"
28
+ end
29
+
30
+ private
31
+ def set_asserts(deferrable)
32
+ deferrable.callback { |s| assert_match(/^http:\/\/goo.gl\/\S+$/, s); EM.stop }
33
+ deferrable.errback { |e| assert(false, e); EM.stop}
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ require 'eventmachine'
2
+ require "test/unit"
3
+ require "em-shorturl"
4
+
5
+ class TestURLShortener < Test::Unit::TestCase
6
+ def test_bad_driver_should_raise_KeyError
7
+ assert_raise(KeyError) do
8
+ EM.run do
9
+ r = EM::ShortURL.shorten('http://google.com', :nonexistent_driver)
10
+ EM.stop
11
+ end
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-shorturl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nik Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: em-http-request
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: An EventMachine module for shortening urls
28
+ email:
29
+ - nikjohnson08@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/em-shorturl/version.rb
35
+ - lib/em-shorturl/google.rb
36
+ - lib/em-shorturl.rb
37
+ - test/test_google.rb
38
+ - test/test_shorturl.rb
39
+ homepage: https://github.com/jumpandspintowin/em-shorturl
40
+ licenses:
41
+ - Simplified BSD
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: An EventMachine module for shortening urls
63
+ test_files:
64
+ - test/test_google.rb
65
+ - test/test_shorturl.rb