To setup the application in your local machine you need to have ruby 2.6.3
and rails 5.2.3
installed. Then follow the next steps:
- First clone the repository:
git clone [email protected]:alexventuraio/urls-shortener.git
- Then, enter into the main directory:
cd urls-shortener/
- Now run
bundle install
- The database is handled by PostgreSQL, we recommend you to install it first. Then copy the sample DB configuration file:
cp config/database.yml.sample config/database.yml
- Now you are ready to create the database and run the migrations by executing:
rake db:create && rake db:migrate
- Now you are ready to run the Rails server with:
rails s
- Finally, open your browser and go to:
[http://localhost:3000](http://localhost:3000/)
We are using Rspec for the entire tests suite, so you can run them by executing:
bundle exec rspec
To run a specific file just add the name file like this:
bundle exec rspec spec/models/url_spec.rb
Also, make sure to set up the ENV variable for the Redis local server. Copy the sample configuration file:
cp .env.sample .env
And set the corresponding value in there:
REDIS_PROVIDER=redis://localhost:6379/0
We are handling background processing with Sidekiq and Redis but for development purpose in local environment we are are running jobs inline as you can see in the following config:
# config/application.rb
config.active_job.queue_adapter = Rails.env.production? ? :sidekiq : :async
So, if you want to run jobs in Sidekiq, then you only need to change the queue_adapter
to :async
and make sure you have Redis running and you should be ready to go.
https://myshorten.herokuapp.com/
We are taking advantage of the already existing Digest Class in Ruby language.
This class allow us to generate a hash from a given string as described in the documentation and accepts a second param to indicate the number of caracters we want to get back. So, we are taking only the first 10 characters to make it kind of short. The most commonly used class method is hexdigest
and this is the one we are using, but there are also digest
and base64digest
.
Basically, this is the way we are generating the shorten URLs.