Module: ISGD

Defined in:
lib/isgd.rb

Class Method Summary collapse

Class Method Details

.lookup(url) ⇒ String

Looks up a short URL’s original long URL.

Parameters:

  • url (String)

    The shortened URL to look up.

Returns:

  • (String)

    The error message.

  • (String)

    The long URL.

See Also:

Since:

  • 1.0.0



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/isgd.rb', line 36

def lookup(url)
  params = {
    format: 'json',
    shorturl: url
  }
  @client = HTTPClient.new if @client.nil?

  response = JSON.parse(@client.get(URI.parse('https://is.gd/forward.php'), params).body)

  return response['errormessage'] if response.key?('errormessage')

  response['url']
end

.shorten(url, short = nil) ⇒ String

Shortens a URL using ISGD.

Parameters:

  • url (String)

    The URL to shorten.

  • short (String) (defaults to: nil)

    The custom short URL to use as the shortened link. This cannot be shorter than 5, or more than 30.

Returns:

  • (String)

    The error message.

  • (String)

    The shortened URL.

See Also:

Since:

  • 1.0.0



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/isgd.rb', line 15

def shorten(url, short = nil)
  params = {
    format: 'json',
    url: url
  }
  params[:shorturl] = short if !short.nil? && 30 < short.length < 5
  @client = HTTPClient.new if @client.nil?

  response = JSON.parse(@client.get(URI.parse('https://is.gd/create.php'), params).body)

  return response['errormessage'] if response.key?('errormessage')

  response['shorturl']
end