class Shortnr
Constants
- VERSION
Public Class Methods
new(store:)
click to toggle source
# File lib/shortnr.rb, line 6 def initialize(store:) validate_store!(store) @store = store end
Public Instance Methods
find(shortened)
click to toggle source
Finds params for a shortened URL
@param shortend [String] Shortened param @returns [Hash] Returns a Hash or nil
# File lib/shortnr.rb, line 28 def find(shortened) raise ArgumentError.new("Expected shortened to be a string") unless shortened.is_a?(String) maybe_result = @store.get(key_for(shortened)) if maybe_result && maybe_result.length > 0 JSON.parse(maybe_result, symbolize_names: true) else nil end end
shorten(params)
click to toggle source
Shortens a set of params to a md5 hash and stores it in the store
@param params [Hash] Params to be shortened @returns [String] Shortened string
# File lib/shortnr.rb, line 16 def shorten(params) raise ArgumentError.new("Expected params to be a hash or array") unless params.is_a?(Hash) or params.is_a?(Array) shortened = Digest::MD5.hexdigest(params.to_json) @store.set(key_for(shortened), params.to_json) shortened end
Private Instance Methods
key_for(shortened)
click to toggle source
# File lib/shortnr.rb, line 46 def key_for(shortened) "shortnr:#{shortened}" end
validate_store!(store)
click to toggle source
# File lib/shortnr.rb, line 41 def validate_store!(store) raise ArgumentError.new("Store should respond to #set") unless store.respond_to?(:set) raise ArgumentError.new("Store should respond to #get") unless store.respond_to?(:get) end