Skip to content

Instantly share code, notes, and snippets.

Steve Domin stevedomin

Block or report user

Report or block stevedomin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View plug_attachment.ex
import Swoosh.Email
upload_invoice = %Plug.Upload{
path: "/tmp/plug/DEFfF014AaA01F",
content_type: "",
filename: "invoice-peter-may.pdf"
}
new()
|> to("peter@example.com")
View simple_attachment.ex
import Swoosh.Email
new()
|> to("peter@example.com")
|> from({"Jarvis", "jarvis@example.com"})
|> subject("Invoice May")
|> text_body("Here is the invoice for your superhero services in May.")
|> attachment("/Users/jarvis/invoice-peter-may.pdf")
@stevedomin
stevedomin / empty.ex
Last active Feb 21, 2016
empty changeset with Ecto 2.0
View empty.ex
#
# pre ecto 2.0
#
# web/models/user.ex
defmodule User do
schema "users" do
field :name
field :email
end
@stevedomin
stevedomin / mybank.ex
Created Jan 15, 2016
How to test an API client in Elixir?
View mybank.ex
defmodule MyBank.Client do
defstruct client_id: nil, access_token: nil
def request(client, method, path, params \\ %{}, body \\ nil, headers \\ []) do
# prepare url, headers, etc.
case HTTPoison.request(method, url, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, body}
# handle other cases ...
end
end
@stevedomin
stevedomin / iex
Created Sep 7, 2015
Process.info/2 weirdness
View iex
iex(1)> pid = spawn fn -> end
#PID<0.61.0>
iex(2)> Process.info(pid, :status)
nil
iex(3)> Process.info(pid, :status)
nil
iex(4)>
@stevedomin
stevedomin / form.html.eex
Last active Mar 17, 2016
Phoenix example form
View form.html.eex
<%= form_for @changeset, @action, fn f -> %>
<div>
<%= label f, :name %>
<%= text_input f, :name %>
</div>
<div>
<%= submit "Submit" %>
</div>
<% end %>
@stevedomin
stevedomin / ex_playground.conf
Created Aug 12, 2015
Elixir Playground upstart conf
View ex_playground.conf
description "ex_playground"
setuid ex_playground
setgid ex_playground
start on runlevel [2345]
stop on shutdown
respawn
respawn limit 10 20
@stevedomin
stevedomin / create_post.exs
Last active Sep 24, 2019
Using UUIDs as primary key with Ecto
View create_post.exs
defmodule MyBlog.Repo.Migrations.CreatePost do
use Ecto.Migration
def change do
create table(:posts, primary_key: false) do
add :id, :uuid, primary_key: true
add :body, :string
add :word_count, :integer
timestamps
@stevedomin
stevedomin / porcelain_spawn_receive.exs
Last active Aug 29, 2015
Porcelain spawn/receive
View porcelain_spawn_receive.exs
alias Porcelain.Process
alias Porcelain.Result
defmodule Cmd do
def run() do
args = ["b"]
opts = [
in: "a\nb\nc\nb\nb\nd\nb\ne",
out: {:send, self()},
err: {:send, self()} # eksperimental -> This is the only thing that changed
View latency.markdown

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns                     on recent CPU
L2 cache reference ........................... 7 ns                     14x L1 cache
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns                     20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs
Read 1 MB sequentially from memory ..... 250,000 ns  = 250 µs           4X memory
You can’t perform that action at this time.