class Bitly

Constants

ACTION_PATH
REST_API_URL
REST_API_URL_SSL

Attributes

api_key[RW]
key[RW]
key=[RW]
login[RW]
use_ssl[W]

Public Class Methods

config() { |self| ... } click to toggle source
# File lib/ruby-bitly.rb, line 32
def config
  yield self
end
expand(short_url, login = self.login, api_key = self.api_key) click to toggle source

Old API:

expand(short_url, login = self.login, api_key = self.api_key)

New API:

expand(options)

Options can have:

:long_url :login :api_key

# File lib/ruby-bitly.rb, line 79
def expand(short_url, login = self.login, api_key = self.api_key)
  options = ensure_options(url: short_url, login: login, api_key: api_key)
  options.select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) }

  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:expand], options)

  bitly = new(response["data"]["expand"].first)
  bitly.status_code = response["status_code"]
  bitly.status_txt = response["status_txt"]
  bitly.long_url = bitly.error if bitly.error

  bitly
end
get_clicks(short_url, login = self.login, api_key = self.api_key) click to toggle source

Old API:

get_clicks(short_url, login = self.login, api_key = self.api_key)

New API:

get_clicks(options)

Options can have:

:long_url :login :api_key

# File lib/ruby-bitly.rb, line 106
def get_clicks(short_url, login = self.login, api_key = self.api_key)
  options = ensure_options(url: short_url, login: login, api_key: api_key)
  options.select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) }
  options[:shortUrl] = options.delete(:shortURL)

  response = JSON.parse RestClient.get("#{rest_api_url}#{ACTION_PATH[:clicks]}", params: options)

  bitly = new(response["data"]["clicks"].first)
  bitly.status_code = response["status_code"]
  bitly.status_txt = response["status_txt"]

  bitly
end
proxy() click to toggle source
# File lib/ruby-bitly.rb, line 28
def proxy
  RestClient.proxy
end
proxy=(addr) click to toggle source
# File lib/ruby-bitly.rb, line 24
def proxy=(addr)
  RestClient.proxy = addr
end
shorten(long_url, login = self.login, api_key = self.api_key) click to toggle source

Old API:

shorten(long_url, login = self.login, api_key = self.api_key)

New API:

shorten(options)

Options can have:

:long_url :login :api_key :domain

# File lib/ruby-bitly.rb, line 50
def shorten(long_url, login = self.login, api_key = self.api_key)
  options = ensure_options(url: long_url, login: login, api_key: api_key)
  options.select! { |k,_v| [:longURL, :domain, :login, :apiKey].include?(k) }

  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:shorten], options)
  response.delete("data") if response["data"].empty?

  bitly = new response["data"]

  bitly.hash_path = response["data"]["hash"] if response["status_code"] == 200
  bitly.status_code = response["status_code"]
  bitly.status_txt = response["status_txt"]

  bitly
end
use_ssl() click to toggle source
# File lib/ruby-bitly.rb, line 20
def use_ssl
  instance_variable_defined?(:@use_ssl) ? @use_ssl : true
end

Private Class Methods

ensure_options(options) click to toggle source
# File lib/ruby-bitly.rb, line 122
def ensure_options(options)
  options = options[:url] if options[:url].is_a?(Hash)

  response = {}
  response[:shortURL] = options[:short_url] || options[:url]
  response[:longURL] = options[:long_url] || options[:url]
  response[:login] = options[:login] || self.login
  response[:apiKey] = options[:api_key] || self.api_key
  response[:domain] = options[:domain]
  response.reject { |_k,v| v.nil? }
end
rest_api_url() click to toggle source
# File lib/ruby-bitly.rb, line 134
def rest_api_url
  use_ssl ? REST_API_URL_SSL : REST_API_URL
end