Skip to content

Instantly share code, notes, and snippets.

View harrisonmalone's full-sized avatar

Harrison Malone harrisonmalone

View GitHub Profile

Workflow

GitHub Gist will now be the hub of my coding archive. The intention is to have a central point of reference for all of the code that I'm proud of but doesn't exactly belong anywhere.

I wanted my code to be in the cloud, to be searchable and to have nice syntax highlighting. Gist ticks all these boxes.

My workflow was inspired by gister which allows me to post gists from the command line and receive the url.

To install gister with ruby run:

require 'pry'
def seconds_conversion(minutes, seconds)
minutes = minutes * 60
total = minutes + seconds
end
puts "------------------------
What would you like to do
------------------------"
require 'pry'
require_relative 'stuff'
# this is a nice example of how to structure a terminal app functionally, using the while running loop in the menu class, and then passing an instance of a class into the route_action method so we have access to those methods
def menu
running = true
while running
display_menu
action = gets.chomp.to_i
# we create a new stuff instance which allows us to access the methods with the stuff class
stuff = Stuff.new
require 'stripe'
require 'colorize'
require 'dotenv/load'
require 'csv'
Stripe.api_key = ENV.fetch('STRIPE_API_KEY')
food = [
{name: "burger", price: 500},
{name: "sandwich", price: 300}

Stuff to remember

this is a hash

    city = { 
        name: "Melbourne",
        population: 6000000
    }
* {
margin: 0;
}
.container {
margin: 0 auto;
padding: 40px;
}
.row {

Takeaways from models day one

Things to think about from today: we’ve been learning about databases and how they connect with Ruby. Basically, the way we connect them is with a Model: the M from MVC (Model, View, Controller).

But, what is a database?

It's something that stores data, but it's also more than this. Something that stores data could be: an array, a hash, a linked list, or any data structure - so just saying a database stores data is not hugely helpful.

A database needs to solve two problems:

  1. Store data and get it anytime we want.
def find_short(s)
s = s.split(" ")
l = []
s.each do |word|
l << word.length
end
l = l.min
return l # l: length of the shortest word
end

Strava API

This challenge is similar to what we did the other day with Stripe - using an API to extract data that we can then store in some way.

Today we'll be using the Strava API. They have decent documentation if you're interested.

What we want to use in this case is the List Athlete Activities GET request. This will return the activities of one specific Strava user. It needs an access_token to actually work which in this case you can use my own.

If you enter in this url into your web browser it will return a big JSON file while contains all of my running data. Notice the the id number and the access_token in the url. I recommend you open JSON files in Firefox as it has a built in JSON reader.

# 1. Make a ruby method that takes a number in base 2 (binary, as a string) and converts it to a decimal (not using the inbuilt ruby methods).
def binary_to_decimal(binary)
binary = binary.split("").map(&:to_i)
# this splits binary numbers into an array and turns them into integers
exp = binary.length - 1
# => 5
# this determines the starting point of the exponential, we have to minus 1 to get the correct exp, we need the last exp to be 2 ** 0
base_array = []
while exp >= 0