Building a Crypto Weed App: Part 2 - Barebones API
How to build a basic Ruby on Rails server that acts as an API for weed prices
In this installment, I will show you how to setup a basic API server with Ruby on Rails. Go back to my first post to understand the bigger picture. I'm skipping Part 1 for the moment. Given that will be my first paid post, I want to give it the attention it deserves.
All the code will be posted on Github. Here's the link to the last commit that is part of this post.
API Requirements
Accessible on my local machine only
Format /quotes => {date: YYYY-MM-DD UTC, currency: USD, price_per_oz: DECIMAL}
HTML and JSON output supported
Build in 4 hours
Why Ruby on Rails
For a project this basic, it does not matter what platform is used. I learned Rails a long time ago but have not touched it since. This is a learning project so RoR is being chosen mostly for selfish reasons. But it's a strong tool that has survived the test of time.
Start off by creating a new rails project:
~ rails new cluutch.io --webpack=react
Price Calculations
All weed is not created equal. Weed comes in so many sizes, potencies, flavors, packaging, purity, and so on. These are interesting dynamics to ponder, but with such little time, I will make very rough assumptions to make progress possible.
Just like Bitcoin prices are normally calculated as the weighted average across several different exchanges, Cluutch's pricing will be based on public market prices. The very first prices will be the average price of an ounce of mid level hybrid on each of these platforms.
Planet 13: World's largest dispensary in Vegas.
High Level Health: One of the highest rated dispensaries in Colorado.
CC101: Voted as one of the best in California.
Cannahome: Weed and Shroom only dark web marketplace.
Calculations will de done manually to start. Will work on consistency in reference product as time goes on.
Website
The first proof of concept will be a simple homepage that displays the price, as a function of the four input market prices. This command will generate all the bits we need to do the calculation and store the result.
~ rails g scaffold quote date:date currency:string mean_price_per_oz:decimal market1_price_per_oz:decimal market2_price_per_oz:decimal market3_price_per_oz:decimal market4_price_per_oz:decimal
Next, we need to calculate the average price every time we make changes to a quote:
We want `mean_price_per_oz` to always be computed, so we delete the input from the form. After changing our form view accordingly:
We can now create our first entry by spinning up the server with `rails s`. Now when we create a quote, the mean is automatically computed.
Finally, we fix our routing so that the homepage redirects to quotes index.
1. Create new pages controller: `rails g controller pages home`
2. Set the new route as the homepage by adding this to routes.rb: `root :to => "pages#home"`.
3. Redirect "pages#home" to "quotes#index"
At this point, we have basic CRUD for quotes. If we append `.json` to the url, we get a nice JSON output that we will use for the API in the next installment.
Next steps
In next week's post, I will walkthrough how to deploy and productionize this work. I will also start recording daily prices so that we'll have some real data to look at.