benoittgt

benoittgt

Looking for a code review on code that use Plug

Hello

I wrote a very simple tool on Elixir to get notified on Slack when someone mention you in and issue on Github.

The project use Plug and I recently added a fix for a specific behavior of the github’s api but I’m not sure about what I did.

The change is small (don’t be scared about the 187 lines it’s the fixture) and simple but I would to get some feedbacks.

https://github.com/benoittgt/PhubMe/pull/16

Feel free to make any comments. I have already shared some doubts on the pull request.

Thanks

Most Liked

thomasbrus

thomasbrus

Sure! You’re welcome. It’s not perfect but a good step in the right direction I’d say

thomasbrus

thomasbrus

Hey!

I have a few comments on web.ex as a whole:

  • web.ex:27 cond can simply be an if/else expression

  • web.ex:50-55 should be possible to split this method using pattern matching (e.g. def valid_github_payload?(%{"issue" => _issue} = body_params, [{"x-github-event", "issue_comment", ...}])),

    You can then have one function definition per case (last one matches on any body_params and simply returns false). This way the function is easier to digest and it’s more obvious what the different scenarios are.

    The function definition might get a bit long if you’re gonna pattern match on the complete request headers, so perhaps an idea is to leave out the {"content-type", "application/json"} part.

I agree about perhaps refactoring the valid_github_payload? function cause it indeed does two things now. Just looking at that function name alone I think it makes sense it either returns true or false (not a tuple).

The request handler could then (when true) invoke handle_github_payload, which sends the private message, or does nothing. Again pattern matching can be used.

Finally, a few subjective remarks:

  • web.ex:15 remove space after _ :yum: . Also I like this style better:
adapter_port = port(System.get_env("PORT")))
{:ok, _} = Plug.Adapters.Cowboy.http(PhubMe.Web, [], adapter_port)
  • web.ex:20 and web.ex:22 would both have them in the same style instead of only one via the , do: shorthand
def port(nil), do: 8080
def port(value), do: String.to_integer(value)

… Actually, I don’t think you need the port method at all, this would do the job as well:

port = (System.get_env("PORT") || "8080") |> String.to_integer
{:ok, _} = Plug.Adapters.Cowboy.http(PhubMe.Web, [], port)
benoittgt

benoittgt

Awesome. Thanks a lot Thomas ! Will make the changes now.

Thanks a lot

Where Next?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement