A simple bit.ly ruby client.

Build
Status

Configuration

You need to load the gem:

require 'ruby-bitly'

Set global configuration:

Bitly.config do |c|
  c.login   = 'login-here'
  c.api_key = 'key-here'
  c.key     = 'key-here' # deprecated: use api_key instead
  c.use_ssl = false # read more below
  c.proxy   = 'http://localhost:8888' # read more below
end

Or set them individualy:

Bitly.login   = 'login-here'
Bitly.api_key = 'key-here'
Bitly.key     = 'key-here' # deprecated: use api_key instead
Bitly.use_ssl = false # read more below
Bitly.proxy   = 'http://localhost:8888' # read more below

Or set them on methods if you prefer (see it below).

Shorten

# old way
bitly = Bitly.shorten("https://dx7.github.io/")

# old way setting local configuration
bitly = Bitly.shorten("https://dx7.github.io/", "login-here", "key-here")

# new way
bitly = Bitly.shorten(long_url: "https://dx7.github.io/")

# new way setting local configuration
bitly = Bitly.shorten(long_url: "https://dx7.github.io/", login: "login-here", api_key: "key-here", domain: "my.do")

# result
bitly.url #=> "http://bit.ly/2dAjjfo"

y bitly
  :long_url: http://dx7.github.io/
  :url: http://bit.ly/2dAjjfo
  :hash: 2dAjjfo
  :global_hash: micSj7
  :new_hash: 0
  :hash_path: 2dAjjfo
  :status_code: 200
  :status_txt: OK

Expand

# old way
bitly = Bitly.expand("http://bit.ly/2dAjjfo")

# old way setting local configuration
bitly = Bitly.expand("http://bit.ly/2dAjjfo", "login-here", "key-here")

# new way
bitly = Bitly.expand(short_url: "http://bit.ly/2dAjjfo")

# new way setting local configuration
bitly = Bitly.expand(short_url: "http://bit.ly/2dAjjfo", login: "login-here", api_key: "key-here")

# result
y bitly
  :short_url: http://bit.ly/2dAjjfo
  :long_url: https://dx7.github.io/
  :user_hash: 2dAjjfo
  :global_hash: 2dAkyet
  :status_code: 200
  :status_txt: OK

Get Clicks

# old way
bitly = Bitly.get_clicks("http://bit.ly/2dAjjfo")

# old way setting local configuration
bitly = Bitly.get_clicks("http://bit.ly/2dAjjfo", "login-here", "key-here")

# new way
bitly = Bitly.get_clicks(short_url: "http://bit.ly/2dAjjfo")

# new way setting local configuration
bitly = Bitly.get_clicks(short_url: "http://bit.ly/2dAjjfo", login: "login-here", api_key: "key-here")

# result
y bitly
  :short_url: https://bit.ly/2dAjjfo
  :global_hash: 2dAkyet
  :user_clicks: 0
  :user_hash: 2dAjjfo
  :global_clicks: 0
  :status_code: 200
  :status_txt: OK

Proxy

All calls will use the proxy specified by environment variable "http_proxy" by default.

You can set the proxy directly if you prefer:

Bitly.proxy = 'http://localhost:8888'

SSL

All calls will use SSL by default. You can disable it:

Bitly.use_ssl = false

Command Line

Usage
  bitly [options] url

  For help: bitly -h

Basic examples:
  bitly -s http://dx7.github.io
  bitly -e http://bit.ly/2dAjjfo
  bitly --user-clicks http://bit.ly/2dAjjfo
  bitly --global-clicks http://bit.ly/2dAjjfo

Other examples:
  bitly -q -s http://dx7.github.io
  bitly --verbose -e http://bit.ly/2dAjjfo

Options
  -s, --shorten        Shorten a long url (default option)
  -e, --expand         Expand a short bitly url
  -u, --user-clicks    Show user clicks from the short bitly url
  -g, --global-clicks  Show global clicks from the short bitly url

  -l, --login LOGIN    Your bit.ly login
  -k, --key KEY        Your bit.ly API key

  -h, --help           Displays help message, then exit
  -v, --version        Display the version, then exit
  -q, --quiet          Output as little as possible, overrides verbose
  -V, --verbose        Verbose output

Authentication

bit.ly API requires authentication credentials.

You'll need a free bit.ly login and apiKey. Sign up here: http://bit.ly/a/sign_up
If you've already signed up, you can find your apiKey here: http://bit.ly/a/your_api_key

Using commmand line you can supply credentials as parameters. For example:
  bitly -l <login-here> -k <key-here> -s http://dx7.github.io

Or you can create the file ~/.bitly (YAML format) with that content:
  login: <login-here>
  key: <key-here>

Author

dx7 ~ dx7(a)protonmail.ch

Copyright

Copyright (c) 2010 dx7. Licensed under the MIT License:
http://www.opensource.org/licenses/mit-license.php