lgp
It was a much simpler exercise, but I wanted to add a few things, and well, aside from the duplication, it’s ugly.
defmodule JsonAPI do
def query(cat,id,keys) do
categories = %{
"posts" => 100,
"comments" => 500,
"albums" => 100,
"photos" => 5000,
"todos" => 200,
"users" => 10
}
if cat not in Map.keys(categories) do
{:error, ~s('#{cat}' is not a valid category.) }
else
if id > categories[cat] do
{:error, ~s(The maximum id for '#{cat}' is #{categories[cat]}.) }
else
base = "https://jsonplaceholder.typicode.com/"
[base, cat, "/", to_string(id)]
|> :erlang.iolist_to_binary
|> HTTPoison.get
|> handle_response(keys)
end
end
end
def query(cat,id) do
categories = %{
"posts" => 100,
"comments" => 500,
"albums" => 100,
"photos" => 5000,
"todos" => 200,
"users" => 10
}
if cat not in Map.keys(categories) do
{:error, ~s('#{cat}' is not a valid category.) }
else
if id > categories[cat] do
{:error, ~s(The maximum id for '#{cat}' is #{categories[cat]}.) }
else
base = "https://jsonplaceholder.typicode.com/"
[base, cat, "/", to_string(id)]
|> :erlang.iolist_to_binary
|> HTTPoison.get
|> handle_response
end
end
end
def query(cat) do
categories = %{
"posts" => 100,
"comments" => 500,
"albums" => 100,
"photos" => 5000,
"todos" => 200,
"users" => 10
}
if cat not in Map.keys(categories) do
{:error, ~s('#{cat}' is not a valid category.) }
else
base = "https://jsonplaceholder.typicode.com/"
[base, cat]
|> :erlang.iolist_to_binary
|> HTTPoison.get
|> handle_response
end
end
def handle_response( {:ok, %{status_code: 200, body: body} = _response}, keys ) do
target = body
|> Poison.Parser.parse!(%{})
|> get_in(keys)
{:ok, target}
end
def handle_response( {:ok, %{status_code: status, body: body} = _response}, _keys) do
message = body
|> Poison.Parser.parse!(%{})
|> get_in(["message"])
{:error, status, message }
end
def handle_response( {:error, reason }, _ ) do
{:error, reason}
end
def handle_response( {:ok, %{status_code: 200, body: body} = _response}) do
target = body
|> Poison.Parser.parse!(%{})
{:ok, target}
end
def handle_response( {:ok, %{status_code: status, body: body} = _response}) do
message = body
|> Poison.Parser.parse!(%{})
|> get_in(["message"])
{:error, status, message }
end
def handle_response( {:error, reason }) do
{:error, reason}
end
end
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kartheek
Hi @lgp can you add new line with ``` at the beginning of the code block and end of code blocks. You can edit post and add them:
```
Your Code
```
Also your question is not clear as to what you want to cleanup ? Can you provide more details.
kodepett
You can convert the nested if/else to functions.
lgp
OK. How do I edit the post? I haven’t found a way.
al2o3cr
@lgp IIRC there’s an edit timeout for regular users or something, I went ahead and added backticks to your post.
Some general thoughts on cleaning up the code:
if/elsemay be more readable ascondquery/1andquery/2above) fill in the remaining arguments and call the “longer” version (query/3here)base = "https://jsonplaceholder.typicode.com/") to a private function instead of repeating themlgp
Thanks very much. I got rid of the nested ifs.
There really are no default arguments for the query/1 and query/2 versions. I would have to put so many conditionals inside the main function that I would be no better off. I’ll keep looking at that, though.
I’ll also look at using a private function for the URL building lines. Not sure how much that would save since handling the various options would introduce a lot of complications I think.
Again, thanks for the reply – and for adding the back ticks!
stevensonmt
which course is this from?
lgp
The Pragmatic Studio “Elixir/OTP” course. This exercise was from the notes, not one of the videos. And as I mentioned, I expanded on it a bit.
lgp
OK. Took care of the other suggestions.
larry@habu lib % ll json*
-rw-r–r–@ 1 larry staff 2787 Mar 5 17:57 json_api.ex
-rw-r–r–@ 1 larry staff 2003 Mar 5 17:57 json_api.new.ex
larry@habu lib % wc json*
112 301 2787 json_api.ex
90 213 2003 json_api.new.ex
202 514 4790 total
larry@habu lib %
I’ll post the new version for comparison.
lgp
Still need to work on the duplication in handle_response, but this is a great improvement. Thanks again!
lgp
And now cleaned up handle_response. Overall quite an improvement:
larry@habu lib % wc json*
113 304 2873 json_api.ex
73 185 1669 json_api.fin.ex
186 489 4542 total
larry@lil-habu lib %
And the (for now) final version:
Thanks once more…