Class: EventMachine::ShortURL::Bitly
- Inherits:
-
Object
- Object
- EventMachine::ShortURL::Bitly
- Includes:
- Deferrable
- Defined in:
- lib/em-shorturl/bitly.rb
Overview
Driver for bitly.com URL Shortening service. Bitly requires API users to have an account. This can be provided to the driver as either a pre-obtained access token or as a username and password pair (optionally with a client id and secret). If a username and password are provided instead of an access token, login is differed until needed (first call to shorten) or can be explicitly called via the login function.
Constant Summary collapse
- API_OAUTH_URL =
'https://api-ssl.bitly.com/oauth/access_token'- API_SHORTEN_URL =
'https://api-ssl.bitly.com/v3/shorten'
Instance Method Summary collapse
-
#initialize(account = {}) ⇒ Bitly
constructor
Initialize the driver with account details.
-
#login ⇒ Object
Uses the given username and password to obtain an access token from bitly.
-
#shorten(url) ⇒ Object
Shortens the given URL.
Constructor Details
#initialize(account = {}) ⇒ Bitly
Initialize the driver with account details. Despite being required by bitly, the driver does not currently verify that proper account details are given.
account takes the following options:
:client_id The client ID of the registered application
:client_secret The secret for the client id
:username A username to use for basic authentication
:password The password to use for basic authentication
:token A previously obtained access token to use
34 35 36 37 38 39 40 41 |
# File 'lib/em-shorturl/bitly.rb', line 34 def initialize(account={}) @client_id = account[:client_id] @client_secret = account[:client_secret] @username = account[:username] @password = account[:password] @access_token = account[:token] @deferrable_args = [self] end |
Instance Method Details
#login ⇒ Object
Uses the given username and password to obtain an access token from bitly. If authentication fails, sets the driver’s deferrable status to failed.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/em-shorturl/bitly.rb', line 67 def login params = { :head => { 'authorization' => [@username, @password] }, :body => {} } params[:body]['client_id'] = @client_id if @client_id params[:body]['client_secret'] = @client_secret if @client_secret http = EM::HttpRequest.new(API_OAUTH_URL).post(params) http.errback do |http| fail(http.error, *@deferrable_args) end http.callback do |http| if http.response_header.status != 200 fail(http.response, *@deferrable_args) next end @access_token = http.response if @access_token.nil? fail('OAuth request did not return an access token', *@deferrable_args) next else yield if block_given? end end end |
#shorten(url) ⇒ Object
Shortens the given URL. If no access token is known, will call the login function and upon success will call shorten again.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/em-shorturl/bitly.rb', line 48 def shorten(url) if @access_token.nil? login { shorten(url) } return self end params = get_request_parameters(url) request = EM::HttpRequest.new(API_SHORTEN_URL).get(params) request.callback(&method(:on_success)) request.errback(&method(:on_error)) self end |