Created
June 30, 2013 06:08
-
-
Save troyk/5894065 to your computer and use it in GitHub Desktop.
quick hack at a doman specific url shortner, decided to go a different route but going to gist it incase I want to come back to it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A Roberto Url is a tiny url. It depends on the cross-language hashids lib http://www.hashids.org/ | |
| class RobertoUrlController < ApplicationController | |
| layout false | |
| # cursory glance at the code, this appears to be threadsafe | |
| HASHER = ::Hashids.new("no offense rob, this is a tribute to your carb intake not your manhood") #change the salt and all urls are invalid! | |
| PREFIX = "#{Rails.configuration.base_url}/l" | |
| # note, extremely important that you do not change the keys of the object map as the | |
| # hashing is tied to the sequence key: So add class maps to the end of the list! | |
| # also, because classes can be models, use the string representation of the class | |
| # otherwise the AR connection pool is going to have a fit with threads! | |
| OBJ_MAP = { | |
| 1 => 'User::Invitation' | |
| } | |
| # setup 2 way map | |
| OBJ_MAP.merge!(OBJ_MAP.invert) | |
| class << self | |
| def model_to_url(model) | |
| index = OBJ_MAP[model.class.to_s] | |
| raise "#{model.class.to_s} not mapped to a RobertoUrl" if index.nil? | |
| "#{PREFIX}#{HASHER.encrypt(index,model.id)}" | |
| end | |
| def model_from_id(id) | |
| index, id = HASHER.decrypt(id) | |
| OBJ_MAP[index].constantize.find(id) | |
| end | |
| end | |
| def show | |
| @model = self.class.model_from_id(params[:id]) | |
| if @model.is_a?(User::Invitation) | |
| return show_user_invitation | |
| end | |
| end | |
| def show_user_invitation | |
| @invitation = @model | |
| @invitation_data = { | |
| id: @invitation.id | |
| } | |
| #render :json => @model | |
| render :action => :user_invitation | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment