class ClnkApi::Link
Attributes
api_key[RW]
clicks[RW]
long_url[RW]
short_code[RW]
short_url[RW]
Public Class Methods
new(api_key)
click to toggle source
# File lib/clnk_api/link.rb, line 6 def initialize(api_key) @api_key = api_key end
Public Instance Methods
build_response(response)
click to toggle source
# File lib/clnk_api/link.rb, line 64 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"] self.clicks ||= url_data["clicks"] end end
expand(short_code)
click to toggle source
# File lib/clnk_api/link.rb, line 22 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)
click to toggle source
# File lib/clnk_api/link.rb, line 43 def handle_response(response) raise_errors(response) build_response(response) end
info(url)
click to toggle source
# File lib/clnk_api/link.rb, line 14 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)
click to toggle source
# File lib/clnk_api/link.rb, line 48 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)
click to toggle source
# File lib/clnk_api/link.rb, line 31 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)
click to toggle source
# File lib/clnk_api/link.rb, line 10 def validate_url(url) raise ClnkApi::General.new("Url is invalid.") unless url =~ /\A#{URI::regexp(['http', 'https'])}\z/ end