Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fun with Ruby and APIs (Github Commit Messages Are Forever)

Fun with Ruby and APIs (Github Commit Messages Are Forever)

A 20 minutes tutorial for the Ruby Beginner's Track (http://rubynewby.com/) on using Ruby and the Github API to create madlibs out of your commit messages

Romeeka Gayhart

October 21, 2014
Tweet

More Decks by Romeeka Gayhart

Other Decks in Programming

Transcript

  1. Fun with APIs & Ruby Romeeka Gayhart - Oct 2014

    - Boulder Ruby Beginner Track
  2. And today… I’ll be showing you how to make Mad

    Libs with Ruby and the Github API
  3. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  4. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  5. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  6. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  7. What is Github? In English… Github is where you put

    your code. It allows us to share code, using ‘Git’ And it helps make Open Source work possible
  8. A Github Profile (mine) This is where my work goes.

    ! ! Both my personal (or ‘Open Source’ work) ! ! And my day job - which you don’t see much of here since it is done in ‘private’ repos
  9. What is a Commit Message? Every time you put your

    code into Git, you add a message.
  10. What is a Commit Message? Every time you put your

    code into Git, you add a message.
  11. What is a Commit Message? Every time you put your

    code into Git, you add a message.
  12. What is a Commit Message? Every time you put your

    code into Git, you add a message.
  13. What is a Commit Message? Every time you put your

    code into Git, you add a message.
  14. What is a Commit Message? Commit Messages are really important….

    ! They live in ‘Source Control’ forever… ! And believe me. Your future employers will judge you for them…
  15. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  16. What is an API? The API (application programming interface) is

    basically the computer’s GUI Let’s programs use the data easily
  17. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  18. Fetching Data How do we get to it? Ruby Faraday

    JSON Faraday is an HTTP client gem Our Favorite Language (JavaScript Object Notation) ! is a lightweight data-interchange format.
  19. Hitting the Link Using Faraday to hit the link And

    using JSON to parse the response
  20. Hitting the Link Woah… That’s the first page of every

    user in Github…! ! We need to be a little more specific….
  21. Using Pry We just hacked into the mainframe…! ! Pry

    lets you pause the process and play with the results
  22. Using Pry If it’s a Hash.. then we can use

    the concept of ‘Keys’ and ‘Values’ to dig in…
  23. Grabbing the Push Events Just grabbing the ‘unique’ events will

    let us get a clearer picture… We need to isolate the right events in the code…
  24. Grabbing the Push Events And it calls this guy… which

    goes through the data and selects only the ‘Push Events’
  25. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  26. Making the Program Let’s not cram all the code into

    one file though… ! Think about it… What is the purpose of User Commits? ! To get commits… ! And what is the purpose of our program?…
  27. Making the Program Let’s not cram all the code into

    one file though… ! Think about it… What is the purpose of User Commits? ! To get commits… ! And what is the purpose of our program?…
  28. Make the Class Notice that we don’t need to require

    ‘JSON’ or ‘Faraday’ But we do need to require our User Commits…
  29. Accessing the Commits And since this happens here - we

    can remove it from User Commits as well…
  30. Accessing the Commits We want to keep our code clean

    - so let’s move this idea to its own method - down in private
  31. Accessing the Commits We also want to keep our username

    accessible. But let’s keep it private too
  32. Swap Words Right now our plain messages are sentences. But

    what we want to split apart the words
  33. Swap Words downcase - converts all messages to lower case

    split - splits a string into an array. The default character that it splits on is a space
  34. Did it work? Not so fast. Don’t want to ruin

    the surprise yet… Let’s first clean up how the data is displayed…
  35. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  36. The Plan • What is Github? • What is an

    API? • Fetching Data • Making the Program • Showing the Data • How Could We Do It Better?
  37. Test It! Use MiniTest Or use RSPEC It doesn’t matter…

    just test Thing about a month from now.. if I come back to this project
  38. Test First! Use MiniTest Or use RSPEC It doesn’t matter…

    just test Thing about a month from now.. if I come back to this project How the heck can I change anything? I won’t know what will break everything.
  39. Test First! Use MiniTest Or use RSPEC It doesn’t matter…

    just test Thing about a month from now.. if I come back to this project How the heck can I change anything? I won’t know what will break everything. And I won’t understand why I wrote the code the way I did…
  40. Test First! Use MiniTest Or use RSPEC It doesn’t matter…

    just test Thing about a month from now.. if I come back to this project How the heck can I change anything? I won’t know what will break everything. And I won’t understand why I wrote the code the way I did… I’ll spend more time trying to figure it out than I spent writing the code…
  41. Test First! Use MiniTest Or use RSPEC It doesn’t matter…

    just test That’s EXACTLY what happened to me when I tried to get the code ready for this presentation.
  42. Test First! Use MiniTest Or use RSPEC It doesn’t matter…

    just test And if you want someone to help you with your code…! ! You better have tests…
  43. The Dangers of Hardcoding The first few times you play

    the github madlibs it’ll be funny…
  44. The Dangers of Hardcoding The first few times you play

    the github madlibs it’ll be funny… But ‘replaces butt’ will lose its charm eventually…
  45. The Dangers of Hardcoding The first few times you play

    the github madlibs it’ll be funny… But ‘replaces butt’ will lose its charm eventually…
  46. The Dangers of Hardcoding You’ll want to change the words

    around. ! And it’s not great to have those words be hardcoded into your class.
  47. The Dangers of Hardcoding Think about using things like: A

    simple database A config file Heck even reading and writing to a CSV…
  48. Pretty It Up Even if you aren’t ready to make

    an entire website for your commits…. The terminal has lots of styling tools.
  49. Pretty It Up There are also general Coding Standards around

    what a Command Line Interface Tool should present to the user. IEEE Std 1003.1, 2004 Edition Checkout this document if you’re interested…
  50. There is a world of data out there that we

    can access with simple code! ! Let’s go make the world a better (and more fun) place!