Skip to content

Latest commit

 

History

History
153 lines (107 loc) · 3.87 KB

api-endpoints.md

File metadata and controls

153 lines (107 loc) · 3.87 KB

Cat Tinder API Endpoints

Overview

Endpoints are the location from which APIs can access the resources they need to perform CRUD actions. Endpoints can be tested through request specs and model specs.

Previous Lecture (37 min)

YouTube

Learning Objectives

  • can define API endpoints
  • can validate API endpoints
  • can create endpoints for appropriate RESTful actions

Vocabulary

  • API endpoints

Troubleshooting Tips

  • Did you create your database?
  • Did you migrate?
  • Errors? Always look at the first error in the list.

Cats Routes

routes

We need to create endpoints for the actions in our React application. API endpoints are the touch point of communication within the API when an external request is made. In this application, the endpoints will allow external requests for the CRUD actions to be performed on the Cat model.

For the time being we can stub these routes by defining controller methods with no internal logic.

app/controllers/cats_controller.rb

class CatsController < ApplicationController

  def index
  end

  def create
  end

  def update
  end

  def destroy
  end

end

Index Route

We start with the index route. In this endpoint, we want to return all of the cats that the application knows about.

Create a Spec
We're going to practice Test Driven Development, so let's start with a test. We'll add our test to the cats_request_spec.rb file:

/spec/requests/cats_request_spec.rb

require 'rails_helper'

RSpec.describe "Cats", type: :request do
  describe "GET /index" do
    it "gets a list of cats" do
      Cat.create(
        name: 'Felix',
        age: 2,
        enjoys: 'Walks in the park',
        image: 'https://images.unsplash.com/photo-1529778873920-4da4926a72c2?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1036&q=80'
      )

      # Make a request
      get '/cats'

      cat = JSON.parse(response.body)
      expect(response).to have_http_status(200)
      expect(cat.length).to eq 1
    end
  end
end

When we run that spec, it fails of course, because we don't have any code in the controller to respond to the request correctly. Yay failure!

Now we can write the controller code to make it pass:

def index
  cats = Cat.all
  render json: cats
end

Create

Next we'll tackle the create route. Let's start with adding a new test:

describe "POST /create" do
  it "creates a cat" do
    # The params we are going to send with the request
    cat_params = {
      cat: {
        name: 'Buster',
        age: 4,
        enjoys: 'Meow Mix, and plenty of sunshine.',
        image: 'https://images.unsplash.com/photo-1529778873920-4da4926a72c2?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1036&q=80'
      }
    }

    # Send the request to the server
    post '/cats', params: cat_params

    # Assure that we get a success back
    expect(response).to have_http_status(200)

    # Look up the cat we expect to be created in the db
    cat = Cat.first

    # Assure that the created cat has the correct attributes
    expect(cat.name).to eq 'Buster'
  end
end

And once again, this fails because we have no code in the controller to make it pass. Good! Adding the controller code for this spec is as follows:

  def create
    # Create a new cat
    cat = Cat.create(cat_params)
    render json: cat
  end

  # Handle strong parameters, so we are secure
  private
  def cat_params
    params.require(:cat).permit(:name, :age, :enjoys, :image)
  end

And we're green! This isn't quite production ready code, but its enough to get our first test of the endpoint to pass, which is what we're after, so we're happy.


Back to Syllabus