Dev.to View Count Day 2 -- Collecting Data

Corey Alexander - Dec 19 '20 - - Dev Community

Took a break for a few days but back at it today for Day 2 of this Hackathon!

Last time I decided that I would use Rails, so today, let's start setting up a Rails app. We'll start on the backend for collecting the data from the Dev.to API, and we'll make our way to the front-end later.

To get started, I generated a Rails app using the rails new CLI.
Commit: 53c6c89c

From here, I installed DelayedJob and DelayedJobActiveRecord, which are my default tools for running background jobs in Rails. I do this in my database to avoid running a separate service and I also get co-transactionality between my business logic and my job enqueuing. I'm really just using it for this project since it's what I'm most familiar with, and I don't want to have to spin up something like Redis if I can avoid it.
Commit: 3ecd413

Now let's set up the models we need! For the moment, we are going to have a single user, so we aren't even going to model that, and just go right into Articles. Articles matches the terminology used in the dev.to API docs, so that is what I ended up naming the table. Here we record the name and a few other pieces of data about the articles.
Commit: 1fae1ad

Since that we have a model, it's time to start hitting the API and getting some data. So I created a simple job to hit the articles/me route and persist each article to the DB. Here I took the time to make sure pagination worked nicely to grab any number of articles. I might have been able to do this later, but I wanted to solve it while I was thinking about it and playing with the API.
Commit: 31b4a99

Next, I needed a model to actually hold the view and comment count data. This needs to be separate from Article cause we need to keep a historical record of the statistics. I created the ArticleStats table, such that a single Article will have many ArticleStats.
I also augmented the job I created before to generate a new Stat row on each run.
Commit: 99c8765

And with all that, I'm gonna call it a day! Looking forward to the next time I work on this, I think we'll be ready to try getting this hosted on the Digital Ocean App Platform.
Check back for more updates soon!

Photo by Gia Oris on Unsplash

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .