Introduction
Learn how to integrate Dub with Ruby.
1. Prerequisites
To follow this guide, you will need to:
2. Install and initialize the Dub Ruby SDK
Install
To install the Dub Ruby SDK, run the following command:
gem install dub
Initialize
Initialize the Dub Ruby SDK by creating a new instance of the Dub
struct.
require 'dub'
dub = ::OpenApiSDK::Dub.new
dub.config_security(
::OpenApiSDK::Shared::Security.new(
token: ENV['DUB_API_KEY'],
)
)
3. Create link
Let’s create a short link using the Dub Ruby SDK.
req = ::OpenApiSDK::Operations::CreateLinkRequest.new(
request_body: ::OpenApiSDK::Operations::CreateLinkRequestBody.new(
url: "https://google.com"
)
)
res = dub.links.create(req)
puts res.raw_response.body
Optionally, you can also pass an externalId
field which is a unique identifier for the link in your own database to associate it with the link in Dub’s system.
req = ::OpenApiSDK::Operations::CreateLinkRequest.new(
request_body: ::OpenApiSDK::Operations::CreateLinkRequestBody.new(
url: "https://google.com",
external_id: "12345"
)
)
res = dub.links.create(req)
puts res.raw_response.body
This will let you easily update the link or retrieve analytics for it later on using the externalId
instead of the Dub linkId
.
4. Upsert link
Dub Ruby SDK provides a method to upsert a link – where an existing link is updated if it exists, or a new link is created if it doesn’t. so you don’t have to worry about checking if the link already exists.
req = ::OpenApiSDK::Operations::UpsertLinkRequest.new(
request_body: ::OpenApiSDK::Operations::UpsertLinkRequestBody.new(
url: "https://google.com",
),
)
res = dub.links.upsert(req)
puts res.raw_response.body
This way, you won’t have to worry about checking if the link already exists when you’re creating it.
5. Update link
Let’s update an existing link using the Dub Ruby SDK.
You can do that in two ways:
- Using the link’s
linkId
in Dub’s system. - Using the link’s
externalId
in your own database (prefixed withext_
).
req = ::OpenApiSDK::Operations::UpdateLinkRequest.new(
link_id: "cly2p8onm000cym8200nfay7l",
request_body: ::OpenApiSDK::Operations::UpdateLinkRequestBody.new(
url: "https://google.us",
),
)
res = dub.links.update(req)
puts res.raw_response.body
6. Retrieve analytics for link
Let’s retrieve analytics for a link using the Dub Ruby SDK.
req = ::OpenApiSDK::Operations::RetrieveAnalyticsRequest.new(
link_id: "clmnr6jcc0005l308q9v56uz1",
interval: ::OpenApiSDK::Operations::Interval::SEVEND,
group_by: ::OpenApiSDK::Operations::GroupBy::TIMESERIES
)
res = dub.analytics.retrieve(req)
puts res.raw_response.body
7. Examples
Was this page helpful?