Class: LinkShrink::Shrinkers::Karmacracy

Inherits:
Base
  • Object
show all
Defined in:
lib/link_shrink/shrinkers/karmacracy.rb

Overview

Implements Karmacracy’s URL Shortener API

Author:

Instance Attribute Summary

Attributes inherited from Base

#url

Instance Method Summary collapse

Methods inherited from Base

#api_key, #api_key?, #body_parameters, collection, #content_type, error, #generate_chart_url, #http_method, inherited, response_options, short_url

Instance Method Details

#api_query_parameterString

Returns URL query parameters

Returns:

  • (String)

    query parameters to be used in request



20
21
22
# File 'lib/link_shrink/shrinkers/karmacracy.rb', line 20

def api_query_parameter
  "?u=#{api_username}&key=#{api_key}&format=json&url=#{url}"
end

#api_urlString

Returns full api url

Returns:

  • (String)

    full api url with query parameters



26
27
28
# File 'lib/link_shrink/shrinkers/karmacracy.rb', line 26

def api_url
  base_url.concat api_query_parameter
end

#api_usernameString

Returns API Key

Returns:

  • (String)

    API key or nil



38
39
40
# File 'lib/link_shrink/shrinkers/karmacracy.rb', line 38

def api_username
  api_username? ? ENV["#{sub_klass.upcase}_USERNAME"] : nil
end

#api_username?TrueClass, FalseClass

Predicate method for checking if the API key exists

Returns:

  • (TrueClass, FalseClass)


32
33
34
# File 'lib/link_shrink/shrinkers/karmacracy.rb', line 32

def api_username?
  ENV.has_key?("#{sub_klass.upcase}_USERNAME")
end

#base_urlString

Returns URL base for API

Returns:

  • (String)

    api base url



14
15
16
# File 'lib/link_shrink/shrinkers/karmacracy.rb', line 14

def base_url
  'http://kcy.me/api/'
end

#sanitize_url(new_url) ⇒ Object



42
43
44
# File 'lib/link_shrink/shrinkers/karmacracy.rb', line 42

def sanitize_url(new_url)
  URI::encode(new_url)
end