Class: Sendly::LinksResource

Inherits:
Object
  • Object
show all
Defined in:
lib/sendly/links_resource.rb

Overview

Links resource — branded URL shortening.

Mint branded short links for a destination URL, list the links your workspace has created (with click analytics), and enable or disable an individual link (a per-link kill switch).

Branded, owned-domain short links improve deliverability — carriers filter public shorteners — and give you click data.

NOTE: URL shortening is not yet generally available. It is gated behind the url_shortener rollout flag (currently founder-only); for API-key clients a flag-off account reads as a 404 NotFoundError (the feature is absent) until the flag is on for your account.

Examples:

Shorten a URL

link = client.links.create(url: "https://example.com/spring-sale?utm_source=sms")
puts link.short_url # => "https://sendly.live/l/Ab3xY7"

List your links with click counts

result = client.links.list(limit: 20)
result.each { |l| puts "#{l.short_url} -> #{l.destination_url} (#{l.click_count} clicks)" }

Kill a link

client.links.disable(link.code)

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ LinksResource

Returns a new instance of LinksResource.



162
163
164
# File 'lib/sendly/links_resource.rb', line 162

def initialize(client)
  @client = client
end

Instance Method Details

#create(url:) ⇒ Sendly::ShortLink

Mint a branded short link for a destination URL. Uses your workspace's brand slug when one is configured.

Parameters:

  • url (String)

    The destination URL to shorten (must be http/https)

Returns:

Raises:



173
174
175
176
177
178
# File 'lib/sendly/links_resource.rb', line 173

def create(url:)
  validate_url!(url)

  response = @client.post("/links", { url: url })
  ShortLink.new(response)
end

#disable(code) ⇒ Sendly::ShortLinkStatus

Disable a short link (its redirect returns 404 until re-enabled). Convenience wrapper over #update.

Parameters:

  • code (String)

    The short link code

Returns:



216
217
218
# File 'lib/sendly/links_resource.rb', line 216

def disable(code)
  update(code, disabled: true)
end

#enable(code) ⇒ Sendly::ShortLinkStatus

Re-enable a previously disabled short link. Convenience wrapper over #update.

Parameters:

  • code (String)

    The short link code

Returns:



225
226
227
# File 'lib/sendly/links_resource.rb', line 225

def enable(code)
  update(code, disabled: false)
end

#list(limit: nil, offset: nil) ⇒ Sendly::ShortLinkList

List the short links your workspace has created, newest first, with click counts and a 14-day daily click histogram.

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Maximum links to return (default 50, max 200)

  • offset (Integer, nil) (defaults to: nil)

    Number of links to skip (default 0)

Returns:



186
187
188
189
190
191
192
193
# File 'lib/sendly/links_resource.rb', line 186

def list(limit: nil, offset: nil)
  params = {}
  params[:limit] = limit if limit
  params[:offset] = offset if offset

  response = @client.get("/links", params)
  ShortLinkList.new(response)
end

#update(code, disabled:) ⇒ Sendly::ShortLinkStatus

Enable or disable a short link. A disabled link's redirect returns 404 until it is re-enabled.

Parameters:

  • code (String)

    The short link code

  • disabled (Boolean)

    true to disable, false to re-enable

Returns:

Raises:



203
204
205
206
207
208
209
# File 'lib/sendly/links_resource.rb', line 203

def update(code, disabled:)
  raise ValidationError, "Link code is required" if code.nil? || code.to_s.empty?

  encoded_code = URI.encode_www_form_component(code)
  response = @client.patch("/links/#{encoded_code}", { disabled: disabled })
  ShortLinkStatus.new(response)
end