Class: SignalApi::ShortUrl

Inherits:
SignalHttpApi show all
Includes:
ApiMock
Defined in:
lib/signal_api/short_url.rb,
lib/signal_api/mocks/short_url.rb

Overview

Manage short URLs via Signal’s URL shortening service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiMock

included

Constructor Details

#initialize(id, target_url, short_url, domain) ⇒ ShortUrl

Returns a new instance of ShortUrl.



18
19
20
21
22
23
# File 'lib/signal_api/short_url.rb', line 18

def initialize(id, target_url, short_url, domain)
  @id = id
  @target_url = target_url
  @short_url = short_url
  @domain = domain
end

Instance Attribute Details

#domainObject (readonly)

The domain of the short URL



16
17
18
# File 'lib/signal_api/short_url.rb', line 16

def domain
  @domain
end

#idObject (readonly)

The ID of the shortend URL on the Signal platform



13
14
15
# File 'lib/signal_api/short_url.rb', line 13

def id
  @id
end

#short_urlObject (readonly)

The shortened URL



7
8
9
# File 'lib/signal_api/short_url.rb', line 7

def short_url
  @short_url
end

#target_urlObject (readonly)

The target URL that was shortened



10
11
12
# File 'lib/signal_api/short_url.rb', line 10

def target_url
  @target_url
end

Class Method Details

.create(target, domain) ⇒ ShortUrl

Create a short URL for the provided target URL

Parameters:

  • target (String)

    The target URL that is to be shortened

  • domain (String)

    The short URL domain to use

Returns:

  • (ShortUrl)

    A ShortUrl object representing the short URL on the Signal platform



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/signal_api/short_url.rb', line 31

def self.create(target, domain)
  body = <<-END
<short_url>
  <target_url><![CDATA[#{target}]]></target_url>
  <domain_id>1</domain_id>
</short_url>
  END

  SignalApi.logger.info "Attempting to create a short URL for #{target}"
  with_retries do
    response = post('/api/short_urls.xml',
                    :body => body,
                    :format => :xml,
                    :headers => common_headers)

    if response.code == 201
      data = response.parsed_response['short_url']
      new(data['id'], data['target_url'], "http://#{domain}/#{data['slug']}", domain)
    else
      handle_api_failure(response)
    end
  end
end