Class: OmbuLabs::Shortener::RebrandlyClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ombu_labs/shortener.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RebrandlyClient

You can initialize the client like this:

> OmbuLabs::Shortener::RebrandlyClient.new(“example.com/ombu”)

Parameters:

  • args (String)

    A valid long URL



15
16
17
18
19
20
21
# File 'lib/ombu_labs/shortener.rb', line 15

def initialize(url)
  @url = url
  Rebrandly.configure do |config|
    config.api_key = ENV['REBRANDLY_API_KEY']
  end
  @api = Rebrandly::Api.new
end

Instance Method Details

#shortenString

Shortens the URL and returns a short URL string. It will NOT raise an exception if there is a communication error with the Rebrandly API.

Returns:

  • (String)


28
29
30
31
32
33
# File 'lib/ombu_labs/shortener.rb', line 28

def shorten
  shorten!
rescue Rebrandly::RebrandlyError => err
  puts "Error: #{err.message}"
  @url
end

#shorten!String

Shortens the URL and returns a short URL string. It will raise an exception if there is a communication error with the Rebrandly API.

Returns:

  • (String)


40
41
42
43
44
45
46
# File 'lib/ombu_labs/shortener.rb', line 40

def shorten!
  puts "Shortening: #{@url}"
  domain = find_domain
  link = @api.shorten(@url, domain: domain.to_h)
  puts "Shortened: SHORT: https://#{link.short_url}"
  link.short_url
end