Class: LinkShortener::Rebrandly

Inherits:
Object
  • Object
show all
Defined in:
lib/link_shortener/rebrandly.rb

Constant Summary collapse

API_URL =
'https://api.rebrandly.com/v1/'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Rebrandly

Returns a new instance of Rebrandly.



5
6
7
# File 'lib/link_shortener/rebrandly.rb', line 5

def initialize(url)
  @url = url
end

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/link_shortener/rebrandly.rb', line 9

def call
  domain_id = fetch_domain_id

  body = { destination: @url }
  body[:domain] = { id: domain_id } if domain_id

  response = RestClient.post endpoint(:links), body.to_json, headers
  protocol = protocol_for(response)
  url = parse_field(response, 'shortUrl')
  return nil if url.blank?
  "#{protocol}://#{url}"
rescue RestClient::ExceptionWithResponse => e
  process_api_error(e)
rescue => e
  Rollbar.warning(e)
  nil
end