Class: LinkShrink::Shrinkers::Base Abstract

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

Overview

This class is abstract.

Subclass and override methods as needed

end

Examples:

Implement a Shrinker class

class Shorty < Base

 def base_url
   'http://shorty.com/api/2.0/shorten'
 end

 def api_query_parameter
   "?url=#{url}"
 end

 def api_url
   base_url.concat api_query_parameter
 end

Author:

Direct Known Subclasses

Google, IsGd, Karmacracy, Owly, TinyUrl

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.collection_keyObject

Returns the value of attribute collection_key.



123
124
125
# File 'lib/link_shrink/shrinkers/base.rb', line 123

def collection_key
  @collection_key
end

.error_keyObject

Returns the value of attribute error_key.



123
124
125
# File 'lib/link_shrink/shrinkers/base.rb', line 123

def error_key
  @error_key
end

.url_keyObject

Returns the value of attribute url_key.



123
124
125
# File 'lib/link_shrink/shrinkers/base.rb', line 123

def url_key
  @url_key
end

Instance Attribute Details

#urlString

Returns long url to shrink.

Returns:

  • (String)

    long url to shrink



28
29
30
# File 'lib/link_shrink/shrinkers/base.rb', line 28

def url
  @url
end

Class Method Details

.collection(new_collection) ⇒ Object

Defines collection_key in response

Parameters:

  • new_collection (String)

    collection_key



132
133
134
# File 'lib/link_shrink/shrinkers/base.rb', line 132

def collection(new_collection)
  self.collection_key = new_collection
end

.error(new_error_key = 'error') ⇒ Object

Defines error_key in response

Parameters:

  • new_error_key (String) (defaults to: 'error')

    error_key



144
145
146
# File 'lib/link_shrink/shrinkers/base.rb', line 144

def error(new_error_key = 'error')
  self.error_key = new_error_key
end

.inherited(sub_klass) ⇒ String

Callback method to define a sub_klass method for reference

Returns:

  • (String)

    inherited class name



32
33
34
35
36
37
38
# File 'lib/link_shrink/shrinkers/base.rb', line 32

def self.inherited(sub_klass)
  sub_klass.class_eval do
    define_method 'sub_klass' do
      @sub_klass = "#{sub_klass.name}"[/::(\w+)::(\w+)/, 2]
    end
  end
end

.response_options(&block) ⇒ Object

Helper method that yields into the other response structure methods



126
127
128
# File 'lib/link_shrink/shrinkers/base.rb', line 126

def response_options(&block)
  yield
end

.short_url(short_url_key) ⇒ Object

Defines url_key in response

Parameters:

  • short_url_key (String)

    url_key



138
139
140
# File 'lib/link_shrink/shrinkers/base.rb', line 138

def short_url(short_url_key)
  self.url_key = short_url_key
end

Instance Method Details

#api_keyString

Returns API Key

Returns:

  • (String)

    API key or nil



76
77
78
# File 'lib/link_shrink/shrinkers/base.rb', line 76

def api_key
  api_key? ? ENV["#{sub_klass.upcase}_URL_KEY"] : nil
end

#api_key?TrueClass, FalseClass

Predicate method for checking if the API key exists

Returns:

  • (TrueClass, FalseClass)


70
71
72
# File 'lib/link_shrink/shrinkers/base.rb', line 70

def api_key?
  ENV.has_key?("#{sub_klass.upcase}_URL_KEY")
end

#api_query_parameterString

Returns query parameters to be used in request.

URL query parameters

Returns:

  • (String)

    query parameters to be used in request

Raises:

  • (String)

    unless implemented on inheriting class



52
53
54
# File 'lib/link_shrink/shrinkers/base.rb', line 52

def api_query_parameter
  fail "#{__method__} not implemented"
end

#api_urlObject

Complete URL with query parameters



64
65
66
# File 'lib/link_shrink/shrinkers/base.rb', line 64

def api_url
  api_key? ? base_url.concat(api_query_parameter) : base_url
end

#base_urlString

Returns api base URL.

URL base for API requests

Returns:

  • (String)

    api base URL

Raises:

  • (String)

    unless implemented on inheriting class



44
45
46
# File 'lib/link_shrink/shrinkers/base.rb', line 44

def base_url
  fail "#{__method__} not implemented"
end

#body_parameters(params = {}) ⇒ NilClass

Parameters to be used in API request

Parameters:

  • params (Hash) (defaults to: {})

    parameters to be used

Returns:

  • (NilClass)

    nil if parameters are empty



59
60
61
# File 'lib/link_shrink/shrinkers/base.rb', line 59

def body_parameters(params = {})
  nil if params.empty?
end

#content_typeString

Returns content-type.

Returns Content-Type to be used in Request headers

Returns:

  • (String)

    content-type



100
101
102
# File 'lib/link_shrink/shrinkers/base.rb', line 100

def content_type
  'application/json'
end

#generate_chart_url(new_url, image_size = {}) ⇒ String

Method for generating QR codes or charts

Parameters:

  • new_url (String)

    url to be processed

  • image_size (Hash<symbol>) (defaults to: {})

    image size target

Returns:

  • (String)

    chart or qr code url



116
117
118
# File 'lib/link_shrink/shrinkers/base.rb', line 116

def generate_chart_url(new_url, image_size = {})
  fail "#{__method__} not implemented"
end

#http_methodSymbol

Returns http method.

Returns HTTP method to be used in request

override +:get+ with +:post+

Returns:

  • (Symbol)

    http method



93
94
95
# File 'lib/link_shrink/shrinkers/base.rb', line 93

def http_method
  :get
end

#sanitize_url(new_url) ⇒ String

Encodes URL

Parameters:

  • new_url (String)

    url to be parsed

Returns:

  • (String)

    parsed URL



83
84
85
86
87
# File 'lib/link_shrink/shrinkers/base.rb', line 83

def sanitize_url(new_url)
  URI.encode(
    !(new_url =~ /^(http?:\/\/)?/) ? "http://#{new_url}" : new_url
  )
end