Skip to content

Commit

Permalink
Add grape endpoint to list artworks
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 5, 2024
1 parent 11b4326 commit 89be756
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/api/using_grape/artworks_endpoint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module UsingGrape
class ArtworksEndpoint < Grape::API
format :json

namespace :artworks do
get do
Artwork.all
end
end
end
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 33 additions & 0 deletions spec/requests/using_grape/list_artworks_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 89be756

Please sign in to comment.