Class: EventMachine::ShortURL::Google
- Inherits:
-
Object
- Object
- EventMachine::ShortURL::Google
- Includes:
- Deferrable
- Defined in:
- lib/em-shorturl/google.rb
Overview
The Google class is the driver for URLShortener to use Google’s API for URL shortening.
As a deferrable, instances accept callback procs in calls to callback() and errback(). Callbacks are provided arguments shorturl and self, while errbacks are provided err_string and self.
Constant Summary collapse
- API_URL =
Specifies the API URL for shortening URLs
"https://www.googleapis.com/urlshortener/v1/url"
Instance Method Summary collapse
-
#initialize(account = {}) ⇒ Google
constructor
Initializes the driver instance, optionally with account information for making requests on behalf of an account.
-
#shorten(url) ⇒ Object
Performs the request of shortening a URL asynchronously.
Constructor Details
#initialize(account = {}) ⇒ Google
Initializes the driver instance, optionally with account information for making requests on behalf of an account. Benefits of using an api key include higher request limits and stat tracking.
The account argument may take the following options:
:apikey Defines an apikey to use for the request
31 32 33 34 |
# File 'lib/em-shorturl/google.rb', line 31 def initialize(account={}) @deferrable_args = [self] @account_apikey = account[:apikey] end |
Instance Method Details
#shorten(url) ⇒ Object
Performs the request of shortening a URL asynchronously. Returns self, which is a deferrable. This allows a usage like the following:
EM::ShortURL::Google.new.shorten('http://google.com').callback do |u,d|
puts "Shorturl: #{u}"
end
45 46 47 48 49 50 51 |
# File 'lib/em-shorturl/google.rb', line 45 def shorten(url) params = get_request_parameters(url) request = EventMachine::HttpRequest.new(API_URL).post(params) request.callback(&method(:on_success)) request.errback(&method(:on_error)) self end |