Skip to content

Commit

Permalink
Adding count route
Browse files Browse the repository at this point in the history
* /count gives number of shorts
* Specs
* Updated api docs
  • Loading branch information
truggeri committed Aug 17, 2021
1 parent 984797c commit 817e858
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/controllers/shorts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def suggest
render(json: { hostname: hostname, short: slug }, status: 200)
end

def count
render(json: { count: Short.count }, status: 200)
end

private

def load_short
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

resources(:shorts, path: '/', only: %i[create destroy show]) do
collection do
get('/count', to: 'shorts#count')
post('/suggestion', to: 'shorts#suggest')
end
end
Expand Down
22 changes: 21 additions & 1 deletion docs/api-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ info:
license:
name: MIT
url: https://github.com/truggeri/rails-url-shortener/blob/main/LICENSE
version: 1.1.0
version: 1.2.0
servers:
- url: https://short.truggeri.com/
externalDocs:
Expand Down Expand Up @@ -146,6 +146,26 @@ paths:
description: The provided bearer is not authorized to remove this short URL
404:
description: Short URL could not be found
/count:
get:
tags:
- ops
summary: Gives a count of the number of shorts in the system
description: Gives the total number of shorts in the system
responses:
200:
description: The count of shorts
content:
application/json:
schema:
required:
- count
type: object
properties:
count:
type: number
description: The number of shorts
example: 5
/suggestion:
post:
tags:
Expand Down
22 changes: 21 additions & 1 deletion public/api-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ info:
license:
name: MIT
url: https://github.com/truggeri/rails-url-shortener/blob/main/LICENSE
version: 1.1.0
version: 1.2.0
servers:
- url: https://short.truggeri.com/
externalDocs:
Expand Down Expand Up @@ -146,6 +146,26 @@ paths:
description: The provided bearer is not authorized to remove this short URL
404:
description: Short URL could not be found
/count:
get:
tags:
- ops
summary: Gives a count of the number of shorts in the system
description: Gives the total number of shorts in the system
responses:
200:
description: The count of shorts
content:
application/json:
schema:
required:
- count
type: object
properties:
count:
type: number
description: The number of shorts
example: 5
/suggestion:
post:
tags:
Expand Down
34 changes: 34 additions & 0 deletions spec/requests/shorts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,38 @@
end
end
end

describe '#count' do
subject { get('/count') }

context 'when no shorts' do
it do
subject
expect(response).to have_http_status(:ok)
expect(response.body).to eq({ count: 0 }.to_json)
end
end

context 'when one short' do
it do
Short.create(full_url: 'something1')
subject
expect(response).to have_http_status(:ok)
expect(response.body).to eq({ count: 1 }.to_json)
end
end

context 'when multipe shorts' do
let(:count) { Random.rand(2..5) }

it do
(1..count).each do |i|
Short.create(full_url: "something#{i}")
end
subject
expect(response).to have_http_status(:ok)
expect(response.body).to eq({ count: count }.to_json)
end
end
end
end

0 comments on commit 817e858

Please sign in to comment.