Skip to content

Commit e9de64c

Browse files
committed
Docs for cost
* Adding docs about cost * Adding cost to json response * Updating api docs * Including doc about suggestion
1 parent 6d94c5c commit e9de64c

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

app/models/short.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Short < ApplicationRecord
4141
exclusion: { in: RESERVED_SHORTS, message: :reserved }
4242

4343
def marshall
44-
{ created_at: created_at.iso8601, full_url: full_url, short_url: short_url, token: token }
44+
{ cost: cost, created_at: created_at.iso8601, full_url: full_url, short_url: short_url, token: token }
4545
end
4646

4747
private

docs/api-spec.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ info:
77
license:
88
name: MIT
99
url: https://github.com/truggeri/rails-url-shortener/blob/main/LICENSE
10-
version: 1.0.0
10+
version: 1.0.1
1111
servers:
1212
- url: https://short.truggeri.com/
1313
externalDocs:
@@ -50,12 +50,17 @@ paths:
5050
application/json:
5151
schema:
5252
required:
53+
- cost
5354
- created_at
5455
- full_url
5556
- short_url
5657
- token
5758
type: object
5859
properties:
60+
cost:
61+
type: number
62+
description: The cost of the short in USD
63+
example: 9
5964
created_at:
6065
type: string
6166
description: Date in iso8601 format

docs/design.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ From the original requirements specification,
1818
> 1. The project should include an automated test suite.
1919
> 2. The project should include a README file with instructions for running the web service and its tests. You should also use the README to provide context on choices made during development.
2020
> 3. The project should be packaged as a zip file or submitted via a hosted git platform (Github, Gitlab, etc).
21+
>
22+
> **Additional Requirements**:
23+
>
24+
> 1. Calculate a cost for each slug, where consonants cost $1, vowels $2 and any repeated character costs $1 extra.
25+
> a. ex: goly -> $5, oely -> $6, gole -> $6
26+
> 2. Add a suggestion service that given a valid hostname (characters only) suggests a slug that has minimal cost.
27+
> a. ex: goldbelly -> gldb ($4), google -> golg ($6), hi -> hihh ($7)
2128
2229
## Product Design
2330

@@ -174,6 +181,31 @@ This project includes a [root README file](../README.md) which references this d
174181

175182
This project is hosted using [GitHub](https://github.com/) at [https://github.com/truggeri/rails-url-shortener](https://github.com/truggeri/rails-url-shortener).
176183

184+
## Additional Requirements
185+
186+
A later pairing session brought about the following additions.
187+
188+
### Cost Calculation
189+
190+
The cost calculation is done as a [before-validation action on the `Short` model](https://github.com/truggeri/rails-url-shortener/blob/6d94c5c421e59b95427749c15f62b4de91d0f8b8/app/models/short.rb#L34).
191+
It iterates over each character and tallies a cost per character.
192+
A slight modification was made to allow for digits and `-_` characters at a cost of $3 each. Reoccurrences are
193+
handled by a hash for quick access. This cost is then saved to the model to avoid recalculation.
194+
195+
### Suggestion Service
196+
197+
There is a [suggestion service](https://github.com/truggeri/rails-url-shortener/blob/main/app/lib/suggestion.rb)
198+
that takes in a hostname (such as `google`) and outputs a suggestion at minimal cost.
199+
This works by breaking the hostname into possible characters (consonants and vowels), then iterating in order of
200+
201+
* Unused consonants ($1 each)
202+
* Unused vowels ($2 each)
203+
* Used consonants ($2 each)
204+
* Used vowels ($3 each)
205+
206+
A random selection is done for each used character to provide unique suggestions. A randomization _could_ be done
207+
on all characters after the generation too.
208+
177209
## Future Improvements
178210

179211
To see areas for improvement, [read our documentation](./future_improvements.md).

public/api-spec.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ info:
77
license:
88
name: MIT
99
url: https://github.com/truggeri/rails-url-shortener/blob/main/LICENSE
10-
version: 1.0.0
10+
version: 1.0.1
1111
servers:
1212
- url: https://short.truggeri.com/
1313
externalDocs:
@@ -50,12 +50,17 @@ paths:
5050
application/json:
5151
schema:
5252
required:
53+
- cost
5354
- created_at
5455
- full_url
5556
- short_url
5657
- token
5758
type: object
5859
properties:
60+
cost:
61+
type: number
62+
description: The cost of the short in USD
63+
example: 9
5964
created_at:
6065
type: string
6166
description: Date in iso8601 format

spec/models/short_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@
209209
Timecop.freeze(time) do
210210
allow(Token).to receive(:encode).with({ iat: object.created_at.to_i, uuid: object.uuid })
211211
.and_return('fake-token')
212-
expect(subject).to include(created_at: time.iso8601,
212+
expect(subject).to include(cost: 7,
213+
created_at: time.iso8601,
213214
full_url: 'full-full',
214215
short_url: 'shorty',
215216
token: 'fake-token')

0 commit comments

Comments
 (0)