Class: Metalbird::UrlProcessor::Bitly

Inherits:
Object
  • Object
show all
Defined in:
lib/metalbird/url_processors/bitly.rb

Constant Summary collapse

RETRY_LIMIT =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Bitly

Returns a new instance of Bitly


12
13
14
15
16
17
18
19
# File 'lib/metalbird/url_processors/bitly.rb', line 12

def initialize(opts)
  ::Bitly.configure do |config|
    config.api_version = 3
    config.access_token = opts[:api_key]
  end

  @client = ::Bitly.client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


10
11
12
# File 'lib/metalbird/url_processors/bitly.rb', line 10

def client
  @client
end

Instance Method Details

#expand(shorten_link) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/metalbird/url_processors/bitly.rb', line 31

def expand(shorten_link)
  retry_counter ||= 0
  @client.expand(shorten_link).long_url
rescue => error
  Metalbird::Logger.error(error)
  retry_counter += 1
  retry if retry_counter <= RETRY_LIMIT
  raise URLExpandFailError
end

#generate(link) ⇒ Object


21
22
23
24
25
26
27
28
29
# File 'lib/metalbird/url_processors/bitly.rb', line 21

def generate(link)
  retry_counter ||= 0
  @client.shorten(link).short_url
rescue => error
  Metalbird::Logger.error(error)
  retry_counter += 1
  retry if retry_counter <= RETRY_LIMIT
  raise URLShortenFailError
end