Fine grain control on Sinatra Routes

At times, you might require fine grain control on the your Sinatra routes.
For example, for the controller path /users/<user_id> , the following would be valid calls –
/users/123 or /users/abc or /users/d6e881d33949470f9df4aa39606d03ea

Now, if you have the user_id as a uuid, you have to validate the user_id every time. Instead of validating the uuid yourself, you could ask Sinatra to reject the request if the provided user_id does not match the uuid pattern.
So, assuming your user_id to be UUID (length 32), instead of defining your controllers as –
put :create, map: ‘/users/:user_id’
you can define them as follows –
put :create, map: %r{/users/(?<user_id>[a-z0-9]{32})}

Notice the <user_id> in the above regular expression. This is match group in a regex and you can get the user_id using the regular params object –
user_id = params[:user_id]

Details here – Sinatra Routes


Home Printer buying – why it is better to NOT buy a Canon Printer!

I bought a Canon Printer (Print, Copy, Scan – INR 3500) some 3 years back from Nehru Place. The guys there advised me not to buy it because of no resale value. They suggested to buy the HP one instead. But they were a bit late as I had already purchased one. And well, impressed with what I had bought, I ignored their comments. Who wants to sell a printer anyways! And if I decide to sell, what price would I get from the INR 3500 printer.

Looking back now, I feel that they were right not only because of the resale point but also the following points –
1. The running cost is exorbitantly high for Canon printers. This is simply because the Cartridge cost is very high.
2. The availability of Cartridges. It is difficult to get Canon cartridges from local shops. So, my dad who is the primary user of the printer had to either go to Nehru Place to buy them or I had to order them through Flipkart.

Now, both the inks are empty and hence no printouts. I am given the task of fixing the printer. The only point I am deliberating now is whether to invest in Canon cartridges or buy a new printer itself. I zeroed-in on an HP wireless printer for the comfort of printing wirelessly (~INR 6000) but still have to make the decision of buying the Canon cartridges vs the new HP printer.

Here is a quick comparison –
Approximate prints requirement per year

Mono Color
800 150

Per page cost

Cartridge Type Cost Yield (pages) Cost/page
Canon PG 810XL Black 1260 401 3.14
Canon CL 811 Tri-color 1335 244 5.47
HP 678 Black 500 480 1.04
HP 678 Tri-color 500 150 3.33

Running cost per year – Existing printer (Canon)

Cartridge type Pages to print Total Cartridge Cost per Cartridge Total Cost
Black 800 2 1260 2520
Color 150 1 1335 1335
Total/Year 3855

Total Running cost for two years = 2 x 3855 = INR 7710

Running cost per year – New Printer (HP)
First Year: New Printer Cost – INR 6000 with 1 black and 1 color cartridge.
Additional Cartridges –

Cartridge type Pages to print left Total Cartridges Cost per Cartridge Total Cost
Black ~400 1 500 500
Total 6500

 Second Year

Cartridge type Pages to print Total Cartridges Cost per Cartridge Total Cost
Black 800 2 500 1000
Color 150 1 500 500
Total 1500

Total Running cost for two years = 6500 + 1500 = INR 8000

As you see here that the cost is almost same after two years and you get –
1. The benefits and Warranty of latest new printer
2. Convenience of a wireless printer
3. Low running cost
4. Convenience of ordering the Cartridges online through HP directly. HP provides this facility. So, no more searching around Canon Cartridges.

Note: This is just an approximate calculation. After I did this calculation, I checked with my dad and he says that currently he buys more than 3 black and 1 color Canon Cartridges per year. That’s more savings if I move to the HP Printer.
Sources: Prices: Flipkart, Yield: the respective websites of Canon and HP

PUT vs POST while creating a resource

The most asked question and one where I have seen the most inconsistency and interchangeable use in REST APIs is whether to use the HTTP method PUT or POST while designing a REST API to create a User entity.
Usually, I have seen suggestions such as use POST to create and PUT to update, or use POST to create and update.
Though, it depends on how you want to model your APIs and many more factors, the most simplest approach that can get you started and help you to distinguish and decide which HTTP method to use is to know whether –
The Entity’s id is provided by the client, with the API path similar to – /v1/users/{user_id}
The Entity’s id is created on the server, with the API path similar to – /v1/users

If the id is provided by client, you can go for PUT, that is, PUT /v1/users/{user_id}
Else, go for POST, that is POST /v1/users

Note: For a more detailed explanation, refer to this stackoverflow thread