class BitlyQuickly

Wrapper around Bitly V3 API.

@example

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'
}

Constants

DEFAULT_API_ADDRESS

Bitly API server.

VERSION

Version number, happy now?

Attributes

access_token[R]

API Access Token.

@return [String]

api_address[R]

Alternate API server URL.

@return [String]

Public Class Methods

new(config) click to toggle source

Init with access token and api address.

@param config [Hash] Config settings. @option config :access_token [String] API Access Token. @option config :api_address [String] Alternate API server URL, optional.

# 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

Public Instance Methods

endpoint_url(path) click to toggle source

Create endpoint URL.

@param path [String] Endpoint path.

@return [String] Endpoint URL.

# File lib/bitly_quickly.rb, line 72
def endpoint_url(path)
  URI.join(api_address, path).to_s
end
shorten(long_url_or_array) click to toggle source

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.

@param long_url_or_array [String, Array<String>]

@return [String, Hash<String, String>]

# 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

Private Instance Methods

get_many_responses(array_of_long_urls) click to toggle source

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

@param array_of_long_urls [String, Array<String>]

@return [Hash<String, String>]

rubocop:disable Metrics/MethodLength

# 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) click to toggle source

Shorten single URL.

@param long_url [String] Original URL.

@return [String] Shortened URL.

# 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) click to toggle source

Prepare Typhoeus shorten request.

@param long_url [String] Original URL.

@return [Typhoues::Request]

# 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) click to toggle source

Parse JSON string into Ruby hash.

@param json [String] JSON string.

@return [Hash]

# File lib/bitly_quickly.rb, line 167
def parse_json(json)
  MultiJson.load(json, symbolize_keys: true)
end
response_to_json(response) click to toggle source

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

@param response [Typhoues::Response]

@return [Hash]

# 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