A Ruby gem for using the version 4 Bitly API to shorten links, expand short links and view metrics across users, links and organizations.
Add this line to your application's Gemfile:
gem 'bitly'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install bitly
All API endpoints require authentication with an OAuth token. You can get your own OAuth token from the Bitly console. Click on the account drop down menu, then Profile Settings then Generic Access Token. Fill in your password and you can generate an OAuth access token.
For other methods to generate access tokens for users via OAuth flows, see the Authentication documentation.
Once you have an access token you can use all the API methods.
All API methods are available through the Bitly::API::Client
. Initialise the client with the access token like so:
client = Bitly::API::Client.new(token: token)
You can then use the client to perform actions with the API
With an authenticated client you can shorten a link like so:
bitlink = client.shorten(long_url: "http://example.com")
bitlink.link
# => http://bit.ly/2OUJim0
With an authorised you can expand any Bitlink.
bitlink = client.expand(bitlink: "bit.ly/2OUJim0")
bitlink.long_url
# => http://example.com
This gem supports the following active v4 API endpoints for theBitly API.
- Retrieve groups (
GET /v4/groups
) - Retrieve group (
GET /v4/groups/{group_guid}
) - Update group (
PATCH /v4/groups/{group_guid}
) - Delete group (
DELETE /v4/groups/{group_guid}
) - Retrieve tags by group (
GET /v4/groups/{group_guid}/tags
) - Retrieve group preferences (
GET /v4/groups/{group_guid}/preferences
) - Update group preferences (
PATCH /v4/groups/{group_guid}/preferences
) - Retrieve Bitlinks by group (
GET /v4/groups/{group_guid}/bitlinks
) - Retrieve sorted Bitlinks by group (
GET /v4/groups/{group_guid}/bitlinks/{sort}
) - Retrieve group shorten counts (
GET /v4/groups/{group_guid}/shorten_counts
) - Retrieve click metrics for a group by referring networks (
GET /v4/groups/{group_guid}/referring_networks
) - Retrieve click metrics for a group by countries (
GET /v4/groups/{group_guid}/countries
)
- Retrieve organizations (
GET /v4/organizations
) - Retrieve organization (
GET /v4/organizations/{organization_guid}
) - Retrieve organization shorten counts (
GET /v4/organizations/{organization_guid}/shorten_counts
)
- Shorten a link (
POST /v4/shorten
) - Expand a Bitlink (
POST /v4/expand
) - Retrieve a Bitlink (
GET /v4/bitlink/{bitlink}
) - Create a Bitlink (
POST /v4/bitlinks
) - Update a Bitlink (
PATCH /v4/bitlink/{bitlink}
) - Get clicks for a Bitlink (
GET /v4/bitlink/{bitlink}/clicks
) - Get clicks summary for a Bitlink (
GET /v4/bitlink/{bitlink}/clicks/summary
) - Get metrics for a Bitlink by countries (
GET /v4/bitlinks/{bitlink}/countries
) - Get metrics for a Bitlink by referrers (
GET /v4/bitlinks/{bitlink}/referrers
) - Get metrics for a Bitlink by referring domains (
GET /v4/bitlinks/{bitlink}/referring_domains
) - Get metrics for a Bitlink by referrers by domain (
GET /v4/bitlinks/{bitlink}/referrers_by_domains
) - [premium] Get a QR code for a Bitlink (
GET /v4/{bitlink}/qr
)
- Add custom Bitlink (
POST /v4/custom_bitlinks
) - [premium] Retrieve custom Bitlink (
GET /v4/custom_bitlinks/{custom_bitlink}
) - [premium] Update custom Bitlink (
PATCH /v4/custom_bitlink/{custom_bitlink}
) - [premium] Get metrics for a custom Bitlink by destination (
GET /v4/custom_bitlinks/{custom_bitlink}/clicks_by_destination
)
- [premium] Retrieve campaigns (
GET /v4/campaigns
) - [premium] Create campaign (
POST /v4/campaigns
) - [premium] Retrieve campaign (
GET /v4/campaigns/{campaign_guid}
) - [premium] Update campaign (
PATCH /v4/campaigns/{campaign_guid}
) - [premium] Retrieve channels (
GET /v4/channels
) - [premium] Create channel (
POST /v4/channels
) - [premium] Retrieve channel (
GET /v4/channels/{channel_guid}
) - [premium] Update channel (
PATCH /v4/channels/{channel_guid}
)
Branded Short Domains documentation
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/philnash/bitly. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Bitly project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.