Like A Girl

Pushing the conversation on gender equality.

Code Like A Girl

Don’t Be Scared of API’s! A Beginner’s Guide to API’s Using Meetup and Rails (Part 1)

Does the phrase “check out the API documentation” strike fear into your heart? Are you trying to break into open source? New to freelancing? Understanding the basics of API’s and how to get data from an API is an invaluable tool to success. This tutorial is going to break you in gently and show you how to get public event data from Meetup.com

What is an API (and why do you care)?

API stands for Application Programming Interface, and essentially it is a way to share data with third party applications. What good is the internet without data sharing? Meetup is a great platform with great documentation, and I’ve found all organizations want their meetings published on their website. Knowing the Meetup API should help you out!

Let’s get started!

Create a new project in Rails

Open a terminal window and type:

rails new understanding_meetup_api
cd understanding_meetup_api

Next we need to create a meetup controller. This command will generate the controller with an action of index.

rails generate controller meetup index

Create and add a git repository (optional, but recommended)

git init
git add -A

Open understanding_meetup_api in your text editor of choice. Navigate to config/routes.rb and add the route to your root

root ‘meetup#index’

Great! We’re ready to dive into the API. Go read the API documentation and you’ll be all set: https://www.meetup.com/meetup_api/

Haha! Just kidding.

I’ll walk you through the relevant documentation as we go, trying my best to explain it piece by piece.

Create the meetup class

In the app/models directory, create a new file and save it as meetup.rb. This will be our meetup class. Technically we should store it in the /lib directory because this class is not a database backed model, but for familiarity we will keep it in /models.

This class will allow us to interact with Meetup’s API. In order to do that, this class will have the following functionality:

  1. Get event data from Meetup’s API
  2. Check for a good connection to Meetup’s API and error handle as necessary
  3. Use query parameters to refine the data

Get events data from Meetup’s API

The first step in requesting data from Meetup’s API is understanding how to ask for it — or how to compose it. The API documentation tells us how Meetup wants us to ask. The documentation is the key that helps us get from what we want in English:
“Dear Meetup. I’d like to see 10 events from Women Who Code San Francisco.”
To this: http://api.meetup.com/Women-Who-Code-SF/events?&page=10

From here: https://www.meetup.com/meetup_api/ scroll down to “Composing a request”. I’ve included a screenshot with notes in red to help explain each piece of the puzzle.

Meetup API Docs — Composing a Request
  1. Base URI: The main URI we will use to communicate with Meetup.
  2. Method: What you are trying to do — in our case it will be getting events by group.
  3. Arguments: We will cover query parameters in Part 2.

We are going to use HTTParty to communicate with the API. HTTParty lets us easily make HTTP requests.

To install add gem httparty to your Gemfile and run bundle from the command line.

In your meetup.rb class add:

class Meetup
include HTTParty

Add the following base URI to your meetup.rb file:

base_uri `api.meetup.com`

Find the method you are calling. If you scroll to the top of https://www.meetup.com/meetup_api/, on the right sidebar it says “API methods”. We are going to get events, so type “events” into the sidebar. I’ve circled the method we want below in red.

API methods available from Meetup.com

Great! Let’s GET this data! I’m going to get data from operation-code-hampton-roads. Feel free to use the group of your choice!

An important note — you need to call the method using the group’s URL (not the group name). To find your group URL navigate to the group page on Meetup.com. In the url search bar you should see www.meetup.com/your-group-url.

In your meetup.rb class add the following method:

def get_data
self.class.get(‘/operation-code-hampton-roads/events’)
end

HTTParty provides the get method we are using in get_data. However, it returns a lot of information we don’t want. In order to get only the event data we want we need to call .parsed_response on the get method.

Add another method to meetup.rb

def events
get_data.parsed_response
end

What’s happening here? The events method is calling the get_data method and the get_data method is actually doing the heavy lifting of getting data from the API. We’re then adding .parsed_response to the data returned from get_data and making that available via the events method.

To make this data accessible to the front end we need to do a little work in the controller. Navigate to app/controllers/meetup_controller.rb For now we’re going to serve the data in JSON format. This enables any front end to consume our data. Update meetup_controller.rb as shown below:

class MeetupController < ApplicationController
def index
@events = Meetup.new.events
render json: @events
end
end

In your console start the server rails server

In your browser navigate to http://localhost:3000

You should see the event data on your screen! WOOHOO! You just got data from an API!

Hopefully this tutorial helped you understand a little more about API’s and how to sort through API documentation to get the information you need. Hopefully you can use your new documentation reading skills to get data from any API!. Please check out Part 2: Using query parameters and error handling with Meetup’s API.