Class: ClnkApi::Link
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#long_url ⇒ Object
Returns the value of attribute long_url.
-
#short_code ⇒ Object
Returns the value of attribute short_code.
-
#short_url ⇒ Object
Returns the value of attribute short_url.
Instance Method Summary collapse
- #build_response(response) ⇒ Object
- #expand(short_code) ⇒ Object
- #handle_response(response) ⇒ Object
- #info(url) ⇒ Object
-
#initialize(api_key) ⇒ Link
constructor
A new instance of Link.
- #raise_errors(response) ⇒ Object
- #shorten(url) ⇒ Object
- #validate_url(url) ⇒ Object
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_key ⇒ Object
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_url ⇒ Object
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_code ⇒ Object
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_url ⇒ Object
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 (short_code) self.short_code = short_code = {:query=>{ :short_code=> short_code,:access_token=> @api_key} } response = self.class.get("/api/v1/links/expand", ) 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 = {:query=>{ :short_url=> url,:access_token=> @api_key} } response = self.class.get("/api/v1/links/info", ) 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 = {:body=>{ :long_url=> long_url,:access_token=> @api_key} } response = self.class.post("/api/v1/links/shorten", ) handle_response(response) end |