Skip to content

Commit

Permalink
adds String example to readme: URL Shortener example
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Dec 3, 2018
1 parent 246ca31 commit 3e8eb82
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ and increase gradually as required
# How?



## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
Expand All @@ -163,6 +162,47 @@ end

## Usage

Right now there are only two use cases for `cid` generation
which are inline with what we need for our _current_ App/project:

### `cid` from a `String`

Given that under-the-hood `cid` is "_just_" using SHA512 hash,
the _output_ will _always_ be the same. It's simple, that's the point.

```
require Cid
Cid.make("hello world") # > "MJ7MSJwS1utMxA9QyQLytNDtd5RGnx6m" (always!)
```

A real world use case for
wanting a `cid` based on a `String`
is a URL shortener.
For example you have a long URL:
https://github.com/dwyl/phoenix-ecto-append-only-log-example
the `cid` of this url is:

```
require Cid
Cid.make("https://github.com/dwyl/phoenix-ecto-append-only-log-example") # > "gVSTedHFGBetxyYib9mBQsjtZj4dJjQe"
```

We can then create a URLs table in our URL shortening app/service such that:

| `inserted_at ` | **`URL`** (PK) | `cid` | `short` |
| ----------- | ----------- | ----------- | ----------- |
| 1541609554 | https://github.com/dwyl/phoenix-ecto-append-only-log-example | gVSTedHFGBetxyYib9mBQsjtZj4dJjQe | gVS |

So the "short" url would be
[dwyl.co/gVS](https://github.com/dwyl/phoenix-ecto-append-only-log-example)


### `cid` from a `Map`




<!--
Expand Down
4 changes: 4 additions & 0 deletions test/cid_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ defmodule CidTest do
assert Cid.make(map) == "GdrVnsLSdxRphXgQgNsmq1FDyRXAySXT"
end

test "Cid.make(\"hello world\")" do
assert Cid.make("hello world") == "MJ7MSJwS1utMxA9QyQLytNDtd5RGnx6m"
end

end

0 comments on commit 3e8eb82

Please sign in to comment.