1 of 51

Text and Strings

JavaScript objects, APIs, and data formats

1.31.2018 // DataViz for Arch, Urbanism, Humanities

2 of 51

Some inspiration

3 of 51

4 of 51

5 of 51

6 of 51

7 of 51

8 of 51

9 of 51

Colored by topic: satellite / moon / R&D / rockets / space shuttle / russia

10 of 51

Representing the same data two-ways

11 of 51

12 of 51

13 of 51

Dataset:�

    • victim of a traffic accident in France in 1958

      • Pedestrians 28,951
      • Bicycles 17,247
      • Motorcycles 74,887
      • 4-Wheeled 63,071

14 of 51

15 of 51

16 of 51

String manipulation

17 of 51

String Operators

  • Attach two strings together...

var fname = "Harry ";

var lname = "Potter";

console.log(fname + lname);

>> "Harry Potter"

  • Get the length, in number of characters...

var name = "Snuffleupagus";

console.log(name.length);

>> 13

18 of 51

  • Get the string between the nth characters...

var str = "Apple, Banana, Kiwi";

var res = str.substring(7, 13);

>> "Banana"

  • Find the position of the first occurrence of a text...

var str = "Please locate where Waldo is!";

console.log(str.indexOf("Waldo"));

>> 20

  • Replace one sequence of letters with another...

var str = "Let’s go to LA!";

console.log(str.replace("LA", "Chicago"));

>> "Let’s go to Chicago!"

19 of 51

  • Split a string by a specific character in to an array...

var str = "Hello! How are you?";

console.log(split(str, "!");

>> ["Hello", " how are you?"]

var str = "Once upon a time";

console.log(split(str, " ");

>> ["Once", "upon", "a", "time"]

  • Join an array into a single string...

var arr = ["Once", "upon", "a", "time"];

console.log(join(arr, "! "));

>> "Once! upon! a! time"

20 of 51

if-statements continued:

string comparison and logical operators

21 of 51

Relational operators, for strings

str

Less than

str == "pizza rocks!"

== also tests equality of strings

str != "tacos"

!= also tests inequality on strings

str <= "pizza rocks!"

Strings are compared in dictionary order (alphabetical)

str > "24 apples"

Numbers in strings are also treated in dictionary order

str.length == 12

`length` returns number of characters in string

str.indexOf("pie") == -1

`indexOf` returns -1 if string isn’t found

str.charAt(1) == "i"

`charAt` returns character at (zero-based) index

22 of 51

Logical operators allow you to combine boolean values.

Sometimes you need more complicated logic that combines multiple requirements, e.g.

If today is a weekday, and the student is Sally, send a PB&J sandwich for lunch.

23 of 51

! operator

  • ! (pronounced "not") is a one-argument operator (applies to one thing) that negates a boolean value: (!true) is false and (!false) is true.

var hot = true;

var sunny = true;

if( !sunny ) {

console.log("bring an umbrella");

}

24 of 51

&& operator

  • && (pronounced "and") is a two-argument operator that returns true only if both arguments are true; otherwise it returns false.

var hot = true;

var sunny = true;

if( sunny && hot ) {

console.log("wear a hat");

}

25 of 51

|| operator

  • || (pronounced "or") is a two-argument operator that returns true if and only if one of the arguments is true; otherwise it returns false.

var cold = true;

var windy = false;

if( cold || windy ) {

console.log("wear a coat!");

}

26 of 51

Truth tables:

First input in orange

Second input in blue

Output in white

true

false

true

true

false

false

false

false

&& operator

true

false

true

true

true

false

true

false

|| operator

27 of 51

Examples of logical operators in action

(5 <= x) && (x <= 20)

X is between 5 and 20 (inclusive)

(x < 10) || (x > 25)

X is not between 10 and 25 (inclusive)

!((15 <= x) && (x <= 30))

X is not between 15 and 30 (inclusive)

28 of 51

One more data type: Objects

29 of 51

Objects are...

  • Simply, grouping of different types of information.
  • More complex, a representation of an “actor” with state and behavior: for example, you can program a real-world being with complex behaviors like so:

var bird = {

species: "bluejay",

color: "blue",

location: [34.23, 498.23],

fly: function() {...},

cry: function() {...}, ...

}

30 of 51

An object has two characteristics...

  • State: this is the collection of information in the object, aka JavaScript properties. Different kinds of objects have different kinds of information in them: a Shape might have width, height, margins, color; a Date object might have year, month, day, hour, etc.
  • Behavior, are described through functions. Methods allow you to operate on the object, extracting information and even modifying the information.

31 of 51

Object Syntax

var emptyObject = {};

var person = {

name: "Harry Potter",

age: 11,

gender: "Male",

currentLocation: "Surrey, England"

}

32 of 51

  • The object is enclosed by squiggly brackets: {}.
  • Each property is listed as key-value pairs: one pair looks like

{ name: "Harry" }, where key = name and value = Harry

  • Reading a value:

person.name;

// returns "Harry"

  • Changing a value:

person.name = "Agnes";

// person now stores "Agnes" as the name

  • This syntax will be how we usually work with data in JavaScript. More on this later.

33 of 51

What’s an API?

34 of 51

API = Application Programming Interface

  • APIs are basically an agreement between different software packages, or different computers, on how to communicate.
  • Real-world analogy: electricity plugs in your house: “plug-and-play” when both sides have the same way of “connecting”, a certain shape with a certain voltage. Sometimes though, you’ll need converters.

35 of 51

How It Works

  • You (the client) makes a request with specific parameters about what data you want, to an API endpoint.
  • The server processes your request (usually pulls data from a database.)
  • Server returns a response containing data according to the parameters you specified.
  • You use the data to update your drawing.

36 of 51

Basic Components of working with an API

  • Endpoints
    • For the web, these are a bunch of URLs. You write code to talk to these, and different endpoints will give you different data back.
  • Parameters
    • You can customize what data you get back by specifying “settings” in your request, e.g. which user’s data, how many results to return, etc.
  • Response
    • Data, usually in the form of JSON (JavaScript Object Notation, a grammar for storing and exchanging data.)

37 of 51

APIs are everywhere

  • Browser API
    • Infrequently used, but you can write JS to open tabs, etc.
  • Your own server
    • Most commercial websites are built by engineering teams who write their own APIs to serve their own data to their pages.
  • 3rd Party APIs
    • Most large websites and services such as Google Maps, Twitter, Facebook, PayPal, etc. provide APIs allowing developers to make use of their data or services (e.g. displaying custom Google Maps on your site, or using Facebook login to log in your users).

38 of 51

Security and Authentication with APIs

  • Dangers
    • Things you could theoretically do using an API (e.g. Facebook’s API): take down a server, misrepresent personal data or the site’s brand, cause 3rd party to lose traffic (and advertisement revenue), etc. etc.
  • Control Mechanisms
    • Sign-in
    • Rate limiting
    • App approval

39 of 51

Example Projects that Use 3rd Party APIs

  • Every Noise at Once
    • uses Echo Nest API to extract genre information and the Spotify Web API to play track previews.
  • 10 Years of Tweets
    • uses Twitter API to visualize one person’s activity on Twitter.

40 of 51

How to talk to APIs: HTTP Requests

41 of 51

HTTP(S) = Hypertext Transfer Protocol (Secure)

  • Defines a format for how any two machines, or servers and clients, communicate.
  • Specifies a set of:
    • Request types
      • GET “just reading data, not changing anything”
      • POST “sending you data, plz update (do something with it!)”
    • Response codes
      • A series of numbers that tell you what’s happening with your request and the server...

42 of 51

Common HTTP Status Codes

  • 200 OK� Server responded with what you wanted, Everything is great.
  • 301 Moved Permanently

Usually invisible: URL automatically redirects to another URL.

  • 400 Bad Request

Your request was invalid: typo, or bad format.

  • 403 Forbidden

You’re not allowed to access this (not owner, or else sign in?)

  • 404 Not Found

You’re asking for something that doesn’t exist.

  • 500 Internal Server Error

Something broke in the server.

  • 503 Service Unavailable

Can’t reach the server.

43 of 51

AJAX = Asynchronous Javascript And XML*

  • “Asynchronous” is the key part: Ajax enables HTTP requests to happen at any time, without being tied to the page (re)load.
  • *XML is actually a misnomer—these days JSON is the preferred format in which to exchange data.

  • For example, Facebook: when you comment on a post, you’re filling out a small form and submitting it to FB’s servers using AJAX; once your submission is processed, only the relevant small section of the page updates, instead of refreshing the whole page.

44 of 51

Ajax for asynchronous client-server communication. From "Head First jQuery" book.

45 of 51

Helpful Reading (via Mozilla)

46 of 51

Using NYTimes API

47 of 51

NYTimes API

  • Get headlines, photos, links to articles
  • Authentication is relatively easy
  • Process:
  • Go to https://developer.nytimes.com
  • Sign up for API key (get email)
  • Explore APIs via their tool
  • Test out code (class demo)

48 of 51

49 of 51

50 of 51

51 of 51

Demo: JSON and Working with API Responses