Class: BitlyQuickly

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly_quickly.rb,
lib/bitly_quickly/error.rb,
lib/bitly_quickly/version.rb

Overview

Wrapper around Bitly V3 API.

Examples:

client         = BitlyQuickly.new(access_token: 'token')
shortened_url  = client.shorten('http://www.google.com/')
shortened_urls = client.shorten([
  'https://www.google.com/',
  'https://www.youtube.com/',
  'https://www.yahoo.com/'
])

shortened_url == 'http://pht.io/1eyUhFo'

shortened_urls == {
  'https://www.yahoo.com/'   => 'http://pht.io/1ezI6Z6',
  'https://www.youtube.com/' => 'http://pht.io/1ezI8Qz',
  'https://www.google.com/'  => 'http://pht.io/1ezI8QA'
}

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_API_ADDRESS =

Bitly API server.

'https://api-ssl.bitly.com'
VERSION =

Version number, happy now?

'0.0.8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BitlyQuickly

Init with access token and api address.

Parameters:

  • config (Hash)

    Config settings.

Options Hash (config):

  • :access_token (String)

    API Access Token.

  • :api_address (String)

    Alternate API server URL, optional.



44
45
46
47
48
49
50
# File 'lib/bitly_quickly.rb', line 44

def initialize(config)
  @access_token = config.delete(:access_token) || fail(
    ArgumentError, 'Missing access_token option'
  )

  @api_address = config.delete(:api_address) || DEFAULT_API_ADDRESS
end

Instance Attribute Details

#access_tokenString (readonly)

API Access Token.

Returns:

  • (String)


32
33
34
# File 'lib/bitly_quickly.rb', line 32

def access_token
  @access_token
end

#api_addressString (readonly)

Alternate API server URL.

Returns:

  • (String)


37
38
39
# File 'lib/bitly_quickly.rb', line 37

def api_address
  @api_address
end

Instance Method Details

#endpoint_url(path) ⇒ String

Create endpoint URL.

Parameters:

  • path (String)

    Endpoint path.

Returns:

  • (String)

    Endpoint URL.



72
73
74
# File 'lib/bitly_quickly.rb', line 72

def endpoint_url(path)
  URI.join(api_address, path).to_s
end

#get_many_responses(array_of_long_urls) ⇒ Hash<String, String> (private)

Shorten each URL and return a hash where keys are original URLs and values are shortened URLs.

rubocop:disable Metrics/MethodLength

Parameters:

  • array_of_long_urls (String, Array<String>)

Returns:

  • (Hash<String, String>)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bitly_quickly.rb', line 90

def get_many_responses(array_of_long_urls)
  hydra     = Typhoeus::Hydra.new
  responses = {}

  array_of_long_urls.each do |long_url|
    request = make_shorten_request(long_url)

    request.on_complete do |response|
      json_response       = response_to_json(response)
      responses[long_url] = json_response[:data][:url]
    end

    hydra.queue request
  end

  hydra.run
  responses
end

#get_single_response(long_url) ⇒ String (private)

Shorten single URL.

Parameters:

  • long_url (String)

    Original URL.

Returns:

  • (String)

    Shortened URL.



114
115
116
117
118
119
120
# File 'lib/bitly_quickly.rb', line 114

def get_single_response(long_url)
  request       = make_shorten_request(long_url)
  response      = request.run
  json_response = response_to_json(response)

  json_response[:data][:url]
end

#make_shorten_request(long_url) ⇒ Typhoues::Request (private)

Prepare Typhoeus shorten request.

Parameters:

  • long_url (String)

    Original URL.

Returns:

  • (Typhoues::Request)


127
128
129
130
131
132
133
134
135
# File 'lib/bitly_quickly.rb', line 127

def make_shorten_request(long_url)
  Typhoeus::Request.new(
    endpoint_url('/v3/shorten'),
    params: {
      access_token: access_token,
      longUrl:      long_url
    }
  )
end

#parse_json(json) ⇒ Hash (private)

Parse JSON string into Ruby hash.

Parameters:

  • json (String)

    JSON string.

Returns:

  • (Hash)


167
168
169
# File 'lib/bitly_quickly.rb', line 167

def parse_json(json)
  MultiJson.load(json, symbolize_keys: true)
end

#response_to_json(response) ⇒ Hash (private)

Check response code and raise an appropriate error. Otherwise return parsed JSON body.

Parameters:

  • response (Typhoues::Response)

Returns:

  • (Hash)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/bitly_quickly.rb', line 143

def response_to_json(response)
  json_response = parse_json(response.body)

  case json_response[:status_code]
  when 200
    return json_response
  when 403
    fail Error::RateLimitExceeded, json_response[:status_txt]
  when 404
    fail Error::NotFound, json_response[:status_txt]
  when 500
    fail Error::InvalidRequestOrResponse, json_response[:status_txt]
  when 503
    fail Error::TemporarilyUnavailable, json_response[:status_txt]
  else
    fail Error::UnknownError, json_response[:status_txt]
  end
end

#shorten(long_url_or_array) ⇒ String, Hash<String, String>

Shorten URL or array of URLs. In case single URL is passed, it returns the shortened URL. If an array is passed, it returns a hash where keys are original URLs and values are shortened URLs.

Parameters:

  • long_url_or_array (String, Array<String>)

Returns:

  • (String, Hash<String, String>)


59
60
61
62
63
64
65
# File 'lib/bitly_quickly.rb', line 59

def shorten(long_url_or_array)
  if long_url_or_array.respond_to?(:each)
    get_many_responses(long_url_or_array)
  else
    get_single_response(long_url_or_array)
  end
end