Skip to content

Instantly share code, notes, and snippets.

@bethsebian
Last active June 15, 2018 19:52
Show Gist options
  • Save bethsebian/c8d2d75d89639d78cc694993b687c02e to your computer and use it in GitHub Desktop.
Save bethsebian/c8d2d75d89639d78cc694993b687c02e to your computer and use it in GitHub Desktop.

Class 3: Basic Algorithms (cont) and a Taste of APIs

Class Overview

  • Project Work Time and Check-Ins
  • Additional Concepts in Ruby
    • Defining and calling methods
    • Passing parameters
  • Introduction to APIs
  • API Work Time

Review of principles

  • Gradual release of responsibility
  • Teaching to fish (use your resources, ask a peer, ask an instructor)
  • Emotional intelligence: managing the discomfort of not knowing
  • Technical strategy and communication

Project Work Time and Check-Ins

When you're not doing your check-in, continue to work on your project and fill out the self-reflection questions (below). Challenges from last week are described here.

Reflection Questions

  • What have been the most difficult parts of your challenge? How did/are you moving through them?
  • What have your "wins" been?
  • What's the next thing you're gonna tackle?

Additional Concepts in Ruby

One way to bring some order to a simple application is to organize your code into methods you define and call.

def my_method
  prints "Hello World"
end

my_method

What happens if you remove the second my_method here? Why?

We can also write methods that take parameter that change the output of a method.

def my_method(greeting)
  prints greeting
end

my_method("Hello, friend!")

Passing arguments to methods is a very powerful tool that allows us to simply our code and create code that is dynamic and flexible (sometimes a very good thing!).

Introduction to APIs

Let's take a look at this cultivated list of APIs. These APIs are special because they are "open" and can be accessed without us needing a secret key to gain access to them.

When you hit one of the described endpoints, what do you see in your browser?

JSON

JSON is the standard format with which APIs return data to the requester. Create a file with the following JSON content, and we'll experiment with working with JSON to extract meaningful data.

// weather_data.json
{
  "query":{
    "count":1,
    "created":"2018-06-15T00:58:27Z",
    "lang":"en-US",
    "results":{
      "channel":{
        "item":{
          "condition":{
            "text":"Mostly Sunny"
          }
        }
      }
    }
  }
}
# json_playground.rb
require 'json'

json = File.read('weather_data.json')
obj = JSON.parse(json)

puts obj["query"]["results"]["channel"]["item"]["condition"]["text"]

What does line puts obj["query"]["results"]["channel"]["item"]["condition"]["text"] do for us?

Try It

Make a new json file with the JSON response from this Yahoo Weather endpoint and make changes to json_playground.rb to read from that new file and puts the high for the coming Saturday in Nome, AK.

Extensions

Pick another open API from toddmotto's list of openapis, save the response in a new JSON file, and write a program that will puts a meaningful sentence containing 3 independent pieces of information from the response. For example, using the AK weather conditions, you could prepare the sentence, The forecast for this Fri is a high of 51, a low of 44, and it will be partially cloudy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment