Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/api/brands_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Api::BrandsController < ApplicationController
def show
expires_in 5.minutes, public: true
render json: brand.as_json(only: %i[name slug])
end

private

def brand
Brand.find_by!(id: params[:brand_id])
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@
get "/:tag", to: "api/form_documents#show", as: :form_document, constraints: { tag: /draft|live|archived/ }
get "/group", to: "api/form_documents#group", as: :form_group
end

get "brands/:brand_id", to: "api/brands#show", as: :brand
end

get "/maintenance" => "errors#maintenance", as: :maintenance_page
Expand Down
41 changes: 41 additions & 0 deletions spec/requests/api/brands_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "rails_helper"

RSpec.describe Api::BrandsController, type: :request do
let(:headers) { { "ACCEPT": "application/json" } }

describe "#show" do
context "when the brand exists" do
let(:brand) { create :brand, name: "Golden Zephyr", slug: "golden-zephyr" }

before do
get "/api/v2/brands/#{brand.id}", headers:
end

it "returns http success" do
expect(response).to have_http_status(:success)
end

it "returns the brand configuration" do
expect(response.parsed_body).to eq({
"name" => "Golden Zephyr",
"slug" => "golden-zephyr",
})
end

it "sets the response to be cached for 5 minutes" do
expect(response.headers["Cache-Control"]).to eq("max-age=300, public")
end
end

context "when the brand does not exist" do
before do
get "/api/v2/brands/0", headers:
end

it "returns http not found" do
expect(response).to have_http_status(:not_found)
expect(response.headers["Content-Type"]).to eq("application/json; charset=utf-8")
end
end
end
end