Class: Googl::Shorten

Inherits:
Base show all
Includes:
Utils
Defined in:
lib/googl/shorten.rb

Constant Summary

Constants included from Utils

Utils::API_CLIENT_LOGIN_URL, Utils::API_HISTORY_URL, Utils::API_URL, Utils::SCOPE_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#info, #qr_code

Constructor Details

#initialize(long_url, user_ip = nil, api_key = nil) ⇒ Shorten

Creates a new short URL, see Googl.shorten



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/googl/shorten.rb', line 11

def initialize(long_url, user_ip = nil, api_key = nil)
  modify_headers('Content-Type' => 'application/json')
  options = {"longUrl" => long_url}
  shorten_url = API_URL

  if (user_ip != nil && !user_ip.empty?)
    options["userIp"] = user_ip
  end
  if (api_key != nil && !api_key.empty?)
    shorten_url += "?key=#{api_key}"
  end

  options_json = options.to_json
  resp = post(shorten_url, :body => options_json)
  if resp.code == 200
    self.short_url  = resp['id']
    self.long_url   = resp['longUrl']
  else
    raise exception(resp.parsed_response)
  end
end

Instance Attribute Details

#long_urlObject

Returns the value of attribute long_url



7
8
9
# File 'lib/googl/shorten.rb', line 7

def long_url
  @long_url
end

#short_urlObject

Returns the value of attribute short_url



7
8
9
# File 'lib/googl/shorten.rb', line 7

def short_url
  @short_url
end