百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

bitly

> 开发工具
开源

一个用于 bit.ly API 的 Ruby 封装

449 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个用于 bit.ly API 的 Ruby 封装

Bitly

A Ruby gem for using the version 4 Bitly API to shorten links, expand short links and view metrics across users, links and organizations.

  • Bitly
    • Installation
    • Usage
      • Authentication
      • Creating an API client
      • Shorten a link
      • Expand a link
    • Available API Endpoints
      • Groups
      • Organizations
      • Users
      • Bitlinks
      • QR codes
      • Custom Bitlinks
      • Campaigns
      • BSDs (Branded Short Domains)
      • Webhooks
    • Customising HTTP requests
      • Build your own adapter
    • Development
    • Contributing
    • License
    • Code of Conduct

Installation

Add this line to your application's Gemfile:

ruby
gem 'bitly'

And then execute:

bash
$ bundle install

Or install it yourself as:

bash
$ gem install bitly

Usage

For a quick introduction, read this blog post on how to use the Bitly API in Ruby.

Authentication

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.

Creating an API client

All API methods are available through the Bitly::API::Client. Initialise the client with the access token like so:

ruby
client = Bitly::API::Client.new(token: token)

You can then use the client to perform actions with the API

Shorten a link

With an authenticated client you can shorten a link like so:

ruby
bitlink = client.shorten(long_url: "http://example.com")
bitlink.link
# => http://bit.ly/2OUJim0

Expand a link

With an authorised you can expand any Bitlink.

ruby
bitlink = client.expand(bitlink: "bit.ly/2OUJim0")
bitlink.long_url
# => http://example.com

Available API Endpoints

This gem supports the following active v4 API endpoints for theBitly API.

Groups

Groups documentation

  • Retrieve groups (GET /v4/groups)
  • Retrieve group (GET /v4/groups/{group_guid})
  • Update group (PATCH /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 QR codes for a group (GET /v4/groups/{group_guid}/qr-codes)
  • [premium] Retrieve click metrics for a group by city (GET /v4/groups/{group_guid}/cities)
  • [premium] Get group overrides (GET /v4/groups/{group_guid}/overrides)

Organizations

Organizations documentation

  • Retrieve organizations (GET /v4/organizations)
  • Retrieve organization (GET /v4/organizations/{organization_guid})
  • Retrieve organization shorten counts (GET /v4/organizations/{organization_guid}/shorten_counts)

Users

Users documentation

  • Retrieve user (GET /v4/user)
  • Update user (PATCH /v4/user)

Bitlinks

Bitlinks documentation

  • Shorten a link (POST /v4/shorten)
  • Expand a Bitlink (POST /v4/expand)
  • Retrieve a Bitlink (GET /v4/bitlinks/{bitlink})
  • Create a Bitlink (POST /v4/bitlinks)
  • Update a Bitlink (PATCH /v4/bitlinks/{bitlink})
  • Delete an unedited hash Bitlink (DELETE /v4/bitlinks/{bitlink})
  • Get clicks for a Bitlink (GET /v4/bitlinks/{bitlink}/clicks)
  • Get clicks summary for a Bitlink (GET /v4/bitlinks/{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 metrics for a Bitlink by city (GET /v4/bitlinks/{bitlink}/cities)
  • [premium] Get metrics for a Bitlink by device type (GET /v4/bitlinks/{bitlink}/devices)

QR codes

QR codes documentation

  • Retrieve a QR code (GET /v4/qr-codes/{qrcode_id})
  • Retrieve the scans summary for a QR code (GET /v4/qr-codes/{qrcode_id}/scans/summary)
  • Retrieve a QR code image (GET /v4/qr-codes/{qrcode_id}/image)

Custom Bitlinks

  • Add custom Bitlink (POST /v4/custom_bitlinks)
  • [premium] Retrieve custom Bitlink (GET /v4/custom_bitlinks/{custom_bitlink})
  • [premium] Update custom Bitlink (PATCH /v4/custom_bitlinks/{custom_bitlink})
  • [premium] Get metrics for a custom Bitlink by destination (GET /v4/custom_bitlinks/{custom_bitlink}/clicks_by_destination)
  • [premium] Get clicks for a custom Bitlin's entire history (GET /v4/custom_bitlinks/{custom_bitlink}/clicks)

Campaigns

  • [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})

BSDs (Branded Short Domains)

Branded Short Domains documentation

  • Retrieve BSDs (GET /v4/bsds)

Webhooks

  • [premium] Get webhooks (GET /v4/organizations/{organization_guid}/webhooks)
  • [premium] Create a webhook (POST /v4/webhooks)
  • [premium] Retrieve a webhook (GET /v4/webhooks/{webhook_guid})
  • [premium] Update a webhook (POST /v4/webhooks/{webhook_guid)
  • [premium] Delete a webhook (DELETE /v4/webhooks/{webhook_guid})
  • [premium] Verify a webhook (POST /v4/webhooks/{webhook_guid}/verify)

Customising HTTP requests

This gem comes with an HTTP client that can use different adapters. It ships with a Net::HTTP adapter that it uses by default.

If you want to control the connection, you can create your own instance of the Net::HTTP adapter and pass it options for an HTTP proxy or options that control the request. For example, to control the read_timeout you can do this:

ruby
adapter = Bitly::HTTP::Adapters::NetHTTP.new(request_options: { read_timeout: 1 })
http_client = Bitly::HTTP::Client.new(adapter)
api_client = Bitly::API::Client.new(http: http_client, token: token)

Similarly, you can use an HTTP proxy with the adapter by passing the proxy variables to the adapter's constructor.

ruby
adapter = Bitly::HTTP::Adapters::NetHTTP.new(proxy_addr: "example.com", proxy_port: 80, proxy_user: "username", proxy_pass: "password")
http_client = Bitly::HTTP::Client.new(adapter)
api_client = Bitly::API::Client.new(http: http_client, token: token)

Build your own adapter

If you want even more control over the request, you can build your own adapter. An HTTP adapter within this gem must have a request instance method that receives a Bitly::HTTP::Request object and ret

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyapi-clientapi-wrapperbitlybitly-api

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具