From 89be7567acb9595fd349c55ed6f0157132909b1c Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Tue, 5 Mar 2024 10:38:16 -0600 Subject: [PATCH] Add grape endpoint to list artworks --- app/api/using_grape/artworks_endpoint.rb | 11 +++++++ config/routes.rb | 4 +++ .../using_grape/list_artworks_spec.rb | 33 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 app/api/using_grape/artworks_endpoint.rb create mode 100644 spec/requests/using_grape/list_artworks_spec.rb diff --git a/app/api/using_grape/artworks_endpoint.rb b/app/api/using_grape/artworks_endpoint.rb new file mode 100644 index 0000000..a8febf1 --- /dev/null +++ b/app/api/using_grape/artworks_endpoint.rb @@ -0,0 +1,11 @@ +module UsingGrape + class ArtworksEndpoint < Grape::API + format :json + + namespace :artworks do + get do + Artwork.all + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 593a034..6ed305c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,9 @@ Rails.application.routes.draw do namespace :api do + namespace :using_grape do + mount UsingGrape::ArtworksEndpoint, at: "/" + end + namespace :using_nothing do resources :artworks end diff --git a/spec/requests/using_grape/list_artworks_spec.rb b/spec/requests/using_grape/list_artworks_spec.rb new file mode 100644 index 0000000..167d84a --- /dev/null +++ b/spec/requests/using_grape/list_artworks_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +describe "GET /api/using_grape/artworks" do + context "without any artworks" do + it "returns an empty array" do + get "/api/using_grape/artworks" + expect(response.status).to eq 200 + expect(response.parsed_body).to eq [] + end + end + + context "with an artwork" do + let!(:artwork) { FactoryBot.create(:artwork) } + + it "returns that artwork" do + get "/api/using_grape/artworks" + expect(response.status).to eq 200 + expect(response.parsed_body).to eq [artwork].as_json + end + end + + context "with a few artworks" do + before do + FactoryBot.create_list(:artwork, 3) + end + + it "returns those artworks" do + get "/api/using_grape/artworks" + expect(response.status).to eq 200 + expect(response.parsed_body.count).to eq 3 + end + end +end