Basic Bash

In this post I wanted to go over some basic Bash Scripting. Once you know the basics of bash for navigating file systems and executing commands learning how to write basic scripts can be helpful as well. For instance with bash scripting you can regularly move files from one system to another, rename a large number of files and folders, or perform a certain set of operations every time a system boots up. I’ll cover Variables, If statements, and Loops.

Getting started:

Using your Bash terminal find a nice directory to work in and enter the command touch myBashScript.sh

This will create a file with the conventional .sh file extension. Open this file up in your favorite editor and add #!/bin/bash on the very first line. This is called a shebang and it describes the interpreter that we’re going to use (in this case bash). Keep in mind that octothorps(#) can be used to write comments in your code.

Lets add a simple command to test this out. I’m gonna try this echo Greetings Planet.

Now save your file,  go back to the command line and type ./myBashScript.sh. You will probably get a permissions related error. By default this file is not executable for security reasons. You can change this by using the chmod command which can modify permissions. Try this chmod 755 myBashScript.sh. Now you should be able to run ./myBashScript.sh and get the expected results.

Read More

DIY Doom Index – How it’s made!

DIY Doom index is a project I made while at The Flatiron School’s web development immersive. DIY Doom Index allows users to sift through daily political, economic and environmental datasets to build a personalized Doom index. Similar to how the S&P 500 or Dow Jones stock indices aggregate multiple companies’ stock performance into one number, DIY doom index aggregates a number of different “pro-doom” and “anti-doom” metrics into one over-all doom number. Users build a single index that tells them how much better or worse the world is everyday based on their political, economic, and environmental sensitivities. You can use the app here (It’s hosted for free on Heroku and may take a few mins to load, try refreshing if you get an error). The code is on my GitHub and I made a video demo you can checkout. In this series of posts I’m going to go over how I made DIY doom index and suggest some areas for improvement. In this post I’m going to cover how I calculated the index to give each user a personalized experience.

Calculating the index

All of the API calls and index calculations happen in the Ruby on Rails backend. This helps avoid CORS related errors and allows me to quickly load updates as the user adjust their doom preferences.

Model

My ActiveRecord / Postgres model was comprised of Users, User-datasets (a join class), and Datasets.

Users (diy_doomsday_backend/app/models/user.rb):

class User < ApplicationRecord

  # Adds methods to set and authenticate against a Bcrypt password
  # requires "password_digest" in user schema/migration
  has_secure_password

  # sets up relationships
  has_many :user_datasets
  has_many :datasets, through: :user_datasets

  #!!!  add additonal validation

end

This class is fairly simple, it sets up the relationships the other database tables and invokes has_secure_password which utilizes the bcrypt gem and allows my backend to store user authentication details without saving plain text passwords to my database (more on authentication later).  The other classes are even simpler. I just set up the relationships and didn’t do any real validation in the backend. This is certainly an area where the app could be improved. When building the front end, I structured the various requests and forms to validate most of the data before sending it to the backend. These classes could be reworked to make the API more resilient, secure and usable with other front end apps.

User-Datasets (diy_doomsday_backend/app/models/user_dataset.rb):

class UserDataset < ApplicationRecord

  # sets up relationships
  has_many :users
  has_many :datasets
end

Datasets (diy_doomsday_backend/app/models/dataset.rb):

class Dataset < ApplicationRecord

  # sets up relationships
  has_many :user_datasets
  has_many :users, through: :user_datasets

end

Read More