Class: LinkShrink::Shrinkers::Base Abstract
- Inherits:
-
Object
- Object
- LinkShrink::Shrinkers::Base
- Defined in:
- lib/link_shrink/shrinkers/base.rb
Overview
Subclass and override methods as needed
end
Direct Known Subclasses
Google, IsGd, Karmacracy, Owly, TinyUrl
Class Attribute Summary collapse
-
.collection_key ⇒ Object
Returns the value of attribute collection_key.
-
.error_key ⇒ Object
Returns the value of attribute error_key.
-
.url_key ⇒ Object
Returns the value of attribute url_key.
Instance Attribute Summary collapse
-
#url ⇒ String
Long url to shrink.
Class Method Summary collapse
-
.collection(new_collection) ⇒ Object
Defines collection_key in response.
-
.error(new_error_key = 'error') ⇒ Object
Defines error_key in response.
-
.inherited(sub_klass) ⇒ String
Callback method to define a sub_klass method for reference.
-
.response_options(&block) ⇒ Object
Helper method that yields into the other response structure methods.
-
.short_url(short_url_key) ⇒ Object
Defines url_key in response.
Instance Method Summary collapse
-
#api_key ⇒ String
Returns API Key.
-
#api_key? ⇒ TrueClass, FalseClass
Predicate method for checking if the API key exists.
-
#api_query_parameter ⇒ String
URL query parameters.
-
#api_url ⇒ Object
Complete URL with query parameters.
-
#base_url ⇒ String
URL base for API requests.
-
#body_parameters(params = {}) ⇒ NilClass
Parameters to be used in API request.
-
#content_type ⇒ String
Returns Content-Type to be used in Request headers.
-
#generate_chart_url(new_url, image_size = {}) ⇒ String
Method for generating QR codes or charts.
-
#http_method ⇒ Symbol
Returns HTTP method to be used in request override
:get
with:post
. -
#sanitize_url(new_url) ⇒ String
Encodes URL.
Class Attribute Details
.collection_key ⇒ Object
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_key ⇒ Object
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_key ⇒ Object
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
#url ⇒ String
Returns 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
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
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
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 (&block) yield end |
.short_url(short_url_key) ⇒ Object
Defines url_key in response
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_key ⇒ String
Returns API Key
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
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_parameter ⇒ String
Returns query parameters to be used in request.
52 53 54 |
# File 'lib/link_shrink/shrinkers/base.rb', line 52 def api_query_parameter fail "#{__method__} not implemented" end |
#api_url ⇒ Object
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_url ⇒ String
Returns api base URL.
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
59 60 61 |
# File 'lib/link_shrink/shrinkers/base.rb', line 59 def body_parameters(params = {}) nil if params.empty? end |
#content_type ⇒ String
Returns 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
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_method ⇒ Symbol
Returns 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
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 |