I am going to explain one of the common approaches used to create URL shortner.
- Think of an alphabet we want to use. In your case that’s
[a-zA-Z0-9]. It contains 62 letters.
- Take an auto-generated, unique numerical key (the auto-incremented
id of a MySQL table for example).
For this example I will use 12510 (125 with a base of 10).
- Now you have to convert 12510 to X62 (base 62).
12510 = 2×621 + 1×620 =
Now map the indices 2 and 1 to your alphabet. This is how your mapping (with an array for example) could look like:
0 → a
1 → b
25 → z
52 → 0
61 → 9
With 2 → c and 1 → b you will receive cb62 as the shortened URL.
How to resolve a shortened URL to the initial ID
The reverse is even easier. You just do a reverse lookup in your alphabet.
- e9a62 will be resolved to “4th, 61st, and 0th letter in alphabet”.
[4,61,0] = 4×622 + 61×621 + 0×620 = 1915810
- Now find your database-record with
WHERE id = 19158 and do the redirect.
Sometimes you may need to have multiple github accounts (one for office, one for work , one for girl friend, whatever) .
Lets say you have ssh keys already setup and github is set for one account. Now for new account:
-keygen -t rsa -C
Careful while name the file , dont overwrite the existing one .I named it as ~/.ssh/id_rsa_xxx
- Add the new key to github account
This is simple enough. Copy the id_rsa_xxx.pub in github > Settings > SSH Keys
- Create a new config file in vi ~.ssh/config
Add following :
You are done . Try it out ..
Vola !! Happy coding !!
Devise gem is one of the most popular gem for creating user login and signup.
The /logout action is by default set to :DELETE inside devise.rb . Most of the browsers only support :GET and :POST.
To change the method of /logout to :GET make following changes in devise.rb
config.sign_out_via = :get
Maven helps to manage the dependency of JARS . Most of the time it may happen that same JAR file with different versions is being loaded because some other jar files have it as dependency and thus causing the conflicts .
Typical example is have log4j or slf4j jar with different version in spring-xx.jar and junit-xxx.jar . So when you load you classes an conflict may arise and because of incompatible version of same jar file . For e.g.
The above jar uses log4j 1.2.17 (you can check the dependency using mvn dependency:tree).
Consider this log4j jar file is conflicting with some other higher version of log4j which is used by other jar. To resolve this issue we would have to remove the conflicting log4j jar from org.springframework.
it can be done as follows :