Class: ClnkApi::Link

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/clnk_api/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Link

Returns a new instance of Link.



7
8
9
# File 'lib/clnk_api/link.rb', line 7

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/clnk_api/link.rb', line 6

def api_key
  @api_key
end

#long_urlObject

Returns the value of attribute long_url.



6
7
8
# File 'lib/clnk_api/link.rb', line 6

def long_url
  @long_url
end

#short_codeObject

Returns the value of attribute short_code.



6
7
8
# File 'lib/clnk_api/link.rb', line 6

def short_code
  @short_code
end

#short_urlObject

Returns the value of attribute short_url.



6
7
8
# File 'lib/clnk_api/link.rb', line 6

def short_url
  @short_url
end

Instance Method Details

#build_response(response) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/clnk_api/link.rb', line 65

def build_response(response)
  unless response.body.strip.empty?
    url_data = MultiJson.load(response.body)["data"] || {} rescue {}
    self.long_url ||= url_data["long_url"]
    self.short_url ||= url_data["short_url"]
    self.short_code ||= url_data["short_code"]
  end
end

#expand(short_code) ⇒ Object



23
24
25
26
27
28
# File 'lib/clnk_api/link.rb', line 23

def expand(short_code)
  self.short_code = short_code
  options = {:query=>{ :short_code=> short_code,:access_token=> @api_key} }
  response = self.class.get("/api/v1/links/expand", options)
  handle_response(response)
end

#handle_response(response) ⇒ Object



44
45
46
47
# File 'lib/clnk_api/link.rb', line 44

def handle_response(response)
  raise_errors(response)
  build_response(response)
end

#info(url) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/clnk_api/link.rb', line 15

def info(url)
  validate_url(url)
  self.short_url = url
  options = {:query=>{ :short_url=> url,:access_token=> @api_key} }
  response = self.class.get("/api/v1/links/info", options)
  handle_response(response)
end

#raise_errors(response) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/clnk_api/link.rb', line 49

def raise_errors(response)
  code = response.code.to_i
  case code
  when 400
    raise ClnkApi::General.new("Parameter check failed. This error indicates that a required parameter is missing or a parameter has a value that is out of bounds.")
  when 401
    raise ClnkApi::Unauthorized.new
  when 404
    raise ClnkApi::NotFound.new
  when 500
    raise ClnkApi::InformClnk.new
  when 503
    raise ClnkApi::Unavailable.new
  end
end

#shorten(url) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/clnk_api/link.rb', line 32

def shorten(url)
  validate_url(url)
  long_url = url
  options = {:body=>{ :long_url=> long_url,:access_token=> @api_key} }
  response = self.class.post("/api/v1/links/shorten", options)
  handle_response(response)

  
  
end

#validate_url(url) ⇒ Object

Raises:



11
12
13
# File 'lib/clnk_api/link.rb', line 11

def validate_url(url)
  raise ClnkApi::General.new("Url is invalid.") unless  url =~ /\A#{URI::regexp(['http', 'https'])}\z/
end