Skip to content

Commit

Permalink
refactor: 💡 UrlHelper validate_url
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusJiang committed Oct 29, 2024
1 parent d268df3 commit ac0f402
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ _dev*
_stag*
_prod*
nohup.out

.env
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ docker compose up
```

```sh
# Create .env file
cp .env.template .env
```

Run app
# Run
export TELEGRAM_BOT_TOKEN=<YOUR_TELEGRAM_BOT_TOKEN>

```sh
sh start.sh
iex -S mix run --no-halt
```
5 changes: 4 additions & 1 deletion lib/migration/typesense.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Migration.Typesense do
alias SmallSdk.Typesense

import Tj.UrlHelper, only: [validate_url!: 1]

def create_collection!(schema) do
req = build_request("/collections")
res = Req.post!(req, json: schema)
Expand All @@ -16,7 +18,8 @@ defmodule Migration.Typesense do
end

defp get_env() do
url = Application.fetch_env!(:save_it, :typesense_url)
url = Application.fetch_env!(:save_it, :typesense_url) |> validate_url!()

api_key = Application.fetch_env!(:save_it, :typesense_api_key)

{url, api_key}
Expand Down
4 changes: 3 additions & 1 deletion lib/save_it/photo_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule SaveIt.PhotoService do
require Logger
alias SmallSdk.Typesense

import Tj.UrlHelper, only: [validate_url!: 1]

def create_photo!(
%{
belongs_to_id: belongs_to_id
Expand Down Expand Up @@ -115,7 +117,7 @@ defmodule SaveIt.PhotoService do
end

defp get_env() do
url = Application.fetch_env!(:save_it, :typesense_url)
url = Application.fetch_env!(:save_it, :typesense_url) |> validate_url!()
api_key = Application.fetch_env!(:save_it, :typesense_api_key)

{url, api_key}
Expand Down
4 changes: 3 additions & 1 deletion lib/small_sdk/typesense.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule SmallSdk.Typesense do
require Logger

import Tj.UrlHelper, only: [validate_url!: 1]

def create_document!(collection_name, document) do
req = build_request("/collections/#{collection_name}/documents")
res = Req.post(req, json: document)
Expand Down Expand Up @@ -70,7 +72,7 @@ defmodule SmallSdk.Typesense do
end

defp get_env() do
url = Application.fetch_env!(:save_it, :typesense_url)
url = Application.fetch_env!(:save_it, :typesense_url) |> validate_url!()
api_key = Application.fetch_env!(:save_it, :typesense_api_key)

{url, api_key}
Expand Down
11 changes: 11 additions & 0 deletions lib/tj/url_helper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Tj.UrlHelper do
def validate_url!(url) do
uri = URI.parse(url)

if uri.scheme in ["http", "https"] and uri.host do
url
else
raise ArgumentError, "Invalid URL: #{url}"
end
end
end

0 comments on commit ac0f402

Please sign in to comment.