Skip to content

Commit 4bce952

Browse files
committed
Add show method to return local authority json
- This is much simpler, at this point we're just doing slug lookups and returning a 404 or the local authority hash (including the parent hash if there is one).
1 parent edb89ba commit 4bce952

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

app/controllers/api/local_authority_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def index
1717
render json: { errors: { "postcode": [e.message] } }, status:
1818
end
1919

20+
def show
21+
@local_authority = LocalAuthority.from_slug(params[:authority_slug])
22+
23+
render json: { local_authority: @local_authority.to_h }
24+
rescue GdsApi::HTTPNotFound
25+
render json: {}, status: :not_found
26+
end
27+
2028
private
2129

2230
def check_postcode_param

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
scope "/api" do
2626
scope "/local-authority" do
2727
get "/" => "api/local_authority#index"
28+
get "/:authority_slug" => "api/local_authority#show"
2829
end
2930
end
3031

spec/requests/local_authority_api_spec.rb

+53
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,57 @@
7272
end
7373
end
7474
end
75+
76+
describe "local authority slugs" do
77+
context "when the slug is not found"
78+
before { stub_local_links_manager_does_not_have_an_authority("foo") }
79+
80+
it "returns a 404 if the slug is not found" do
81+
get "/api/local-authority/foo"
82+
83+
expect(response).to have_http_status(:not_found)
84+
end
85+
86+
context "when the slug points to a unitary authority" do
87+
before { stub_local_links_manager_has_a_local_authority("westminster") }
88+
89+
it "returns the authority with no parent and tier set to unitary" do
90+
get "/api/local-authority/westminster"
91+
92+
expect(response).to have_http_status(:ok)
93+
expect(response.parsed_body).to eq({
94+
"local_authority" => {
95+
"name" => "Westminster",
96+
"homepage_url" => "http://westminster.example.com",
97+
"tier" => "unitary",
98+
"slug" => "westminster",
99+
},
100+
})
101+
end
102+
end
103+
104+
context "when the slug points to a district authority" do
105+
before { stub_local_links_manager_has_a_district_and_county_local_authority("aylesbury", "buckinghamshire") }
106+
107+
it "returns the authority with the parent and tier set to district" do
108+
get "/api/local-authority/aylesbury"
109+
110+
expect(response).to have_http_status(:ok)
111+
expect(response.parsed_body).to eq({
112+
"local_authority" => {
113+
"name" => "Aylesbury",
114+
"homepage_url" => "http://aylesbury.example.com",
115+
"tier" => "district",
116+
"slug" => "aylesbury",
117+
"parent" => {
118+
"name" => "Buckinghamshire",
119+
"homepage_url" => "http://buckinghamshire.example.com",
120+
"tier" => "county",
121+
"slug" => "buckinghamshire",
122+
},
123+
},
124+
})
125+
end
126+
end
127+
end
75128
end

0 commit comments

Comments
 (0)