Skip to content

Instantly share code, notes, and snippets.

View imranismail's full-sized avatar

Imran Ismail imranismail

View GitHub Profile
@imranismail
imranismail / formatDistance.js
Created February 6, 2018 15:23
formatDistance for luxon extracted from date-fns
const FORMAT_DISTANCE_LOCALE = {
lessThanXSeconds: {
one: "'less than a second'",
other: "'less than 's' seconds'"
},
xSeconds: {
one: "s' second'",
other: "s' seconds'"
},
@imranismail
imranismail / params.ex
Last active April 14, 2022 11:08
Ecto as Parameter Validator
defmodule Web.InvalidParamsError do
defexception [:changeset, plug_status: 422]
def message(value) do
"""
Invalid parameters
#{inspect(error_messages(value.changeset))}
"""
end
@imranismail
imranismail / how-to-set-up-stress-free-ssl-on-os-x.md
Created September 30, 2017 10:40 — forked from jed/how-to-set-up-stress-free-ssl-on-os-x.md
How to set up stress-free SSL on an OS X development machine

How to set up stress-free SSL on an OS X development machine

One of the best ways to reduce complexity (read: stress) in web development is to minimize the differences between your development and production environments. After being frustrated by attempts to unify the approach to SSL on my local machine and in production, I searched for a workflow that would make the protocol invisible to me between all environments.

Most workflows make the following compromises:

  • Use HTTPS in production but HTTP locally. This is annoying because it makes the environments inconsistent, and the protocol choices leak up into the stack. For example, your web application needs to understand the underlying protocol when using the secure flag for cookies. If you don't get this right, your HTTP development server won't be able to read the cookies it writes, or worse, your HTTPS production server could pass sensitive cookies over an insecure connection.

  • Use production SSL certificates locally. This is annoying

defmodule App.ChangesetView do
use App, :view
@doc """
Traverses and translates changeset errors.
See `Ecto.Changeset.traverse_errors/2`
"""
def translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end
@imranismail
imranismail / binary_gap.ex
Created July 31, 2017 02:25
BinaryGap in Elixir
defmodule BinaryGap do
def max(number) when is_number(number) do
number
|> Integer.to_string(2)
|> calculate()
end
defp calculate(binary, count \\ -1, max \\ 0)
defp calculate("1" <> remaining, count, max) do
calculate(remaining, 0, Enum.max([max, count]))

Keybase proof

I hereby claim:

  • I am imranismail on github.
  • I am imranismail (https://keybase.io/imranismail) on keybase.
  • I have a public key ASDxoGttt5zk2ozJKng_1DUiRYjeAxoakXYHWFE_bsoObwo

To claim this, I am signing this object:

@imranismail
imranismail / example.ex
Last active June 18, 2017 19:48
Trying to figure out function head bindings for Elixir Macro
defmodule Aggregate do
defmacro replay(struct, aggregate, do: block) do
quote bind_quoted: [struct: struct] do
@events [struct|@events]
def __replay__(struct, unquote(aggregate)), do: unquote(block)
end
end
end
defmodule User do
@imranismail
imranismail / README.md
Last active May 8, 2017 04:55
React with Phoenix

Using React with Phoenix

Create a component renderer helper and pass props as [data-props=""]

defmodule Web.ComponentHelpers do
  import Phoenix.HTML.Tag, only: [content_tag: 3]

  def component(name, props \\ %{})
  def component(name, props) when is_list(props),
    do: component(name, Enum.into(props, %{}))
@imranismail
imranismail / add-block-storage.md
Last active September 20, 2017 06:45
Kubernetes 1.6.1 on Digital Ocean Ubuntu 16.04

Attach Block Storage

Attach block storage to droplet.

50G for starters

Configure Volume

sudo mkfs.ext4 -F /dev/disk/by-id/ sudo mkdir -p /mnt/; sudo mount -o discard,defaults /dev/disk/by-id/ /mnt/; echo /dev/disk/by-id/ /mnt/ ext4 defaults,nofail,discard 0 0 | sudo tee -a /etc/fstab

@imranismail
imranismail / queue.ex
Last active April 3, 2021 21:12
Process native queue with backpressure using Erlang's :queue and Elixir's GenStage spawning a Process for each job in queue
# Elixir :queue wrapper with modified behavior for empty queues to mimic Enumerable module in Elixir
defmodule Queue do
def insert(queue, item), do: :queue.in(item, queue)
def insert_last(queue, item), do: :queue.in_r(item, queue)
def member?(queue, item), do: :queue.member(item, queue)
def filter(queue, fun), do: :queue.filter(fun, queue)