Class: OmbuLabs::Shortener::RebrandlyClient
- Inherits:
-
Object
- Object
- OmbuLabs::Shortener::RebrandlyClient
- Defined in:
- lib/ombu_labs/shortener.rb
Instance Method Summary collapse
-
#initialize(url) ⇒ RebrandlyClient
constructor
You can initialize the client like this:.
-
#shorten ⇒ String
Shortens the URL and returns a short URL string.
-
#shorten! ⇒ String
Shortens the URL and returns a short URL string.
Constructor Details
#initialize(url) ⇒ RebrandlyClient
You can initialize the client like this:
> OmbuLabs::Shortener::RebrandlyClient.new(“example.com/ombu”)
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
#shorten ⇒ String
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.
28 29 30 31 32 33 |
# File 'lib/ombu_labs/shortener.rb', line 28 def shorten shorten! rescue Rebrandly::RebrandlyError => err puts "Error: #{err.}" @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.
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 |