Skip to content

dx7/ruby-bitly

Repository files navigation

A simple bit.ly ruby client.

<img src=“https://travis-ci.org/dx7/ruby-bitly.svg?branch=master” alt=“Build Status” /> <img src=“https://codeclimate.com/github/dx7/ruby-bitly/badges/gpa.svg” />

Configuration

Loading the gem:

require 'ruby-bitly'

Set global configuration:

Bitly.config do |c|
  c.login   = 'login-here'
  c.api_key = 'api-key-here'
  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 = 'api-key-here'
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).

Shortening a long URL

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

# setting credentials or a custom domain
bitly = Bitly.shorten(long_url: "https://dx7.github.io/", domain: "my.do", login: "login-here", api_key: "api-key-here")

# result
bitly.short_url   #=> "http://bit.ly/2dAjjfo"
bitly.long_url    #=> "https://dx7.github.io/"
bitly.new_hash?   #=> true
bitly.global_hash #=> "2dAkyet"
bitly.user_hash   #=> "2dAjjfo"
bitly.success?    #=> true

Expanding a Bitly URL

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

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

# result
bitly.short_url   #=> "http://bit.ly/2dAjjfo"
bitly.long_url    #=> "https://dx7.github.io/"
bitly.global_hash #=> "2dAkyet"
bitly.user_hash   #=> "2dAjjfo"
bitly.success?    #=> true

Getting Clicks from a Bitly URL

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

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

# result
bitly.short_url      #=> "http://bit.ly/2dAjjfo"
bitly.user_hash      #=> "2dAjjfo"
bitly.global_hash    #=> "2dAkyet"
bitly.user_clicks    #=> 0
bitly.global_clicks  #=> 1
bitly.success?       #=> true

Error handling

# if something goes wrong you can check
bitly.success? #=> false
bitly.error    #=> 'INVALID_LOGIN'

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
  -l, --login LOGIN                You need a free Bitly login and api key. Sign up here: http://bit.ly/a/sign_up.
  -k, --api-key KEY                You can find your api key here: http://bit.ly/a/your_api_key.

  -d, --domain DOMAIN              The short domain to use: either bit.ly, j.mp, bitly.com or a custom short domain.
                                   This option will override the default short domain selected in your Bitly account settings.

  -s, --shorten                    Given a long URL, returns a Bitlink.
  -e, --expand                     Given a Bitlink, hash or custom path, returns the target (long) URL.
  -u, --user-clicks                The total count of clicks to this user's Bitlink.
  -g, --global-clicks              The total count of the corresponding Bitly aggregate hash.

  -h, --help                       Print this help.
  -v, --version                    Print version.

Basic examples for command line:
  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

Authentication

bit.ly API requires authentication credentials. You can get it on bit.ly[https://app.bitly.com/Ba48fIHfOg5/bitlinks/2Q1GQcD?actions=accountMain&actions=settings&actions=advancedSettings&actions=apiSupport].

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

Or you can create the file ~/.bitly with that content:
  login: <login-here>
  api_key: <api-key-here>

Author

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

About

A simple bit.ly ruby client to shorten URLs, expand or get number of clicks on a bitlink.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages