Skip to content

Instantly share code, notes, and snippets.

View sorentwo's full-sized avatar
🏡

Parker Selbert sorentwo

🏡
View GitHub Profile
@sorentwo
sorentwo / socket_loop.ex
Created September 23, 2016 15:25
Listen and loop with elixir socket
def loop(socket) do
case socket |> Socket.Web.recv! do
{:text, data} ->
# process data
loop(socket)
{:ping, _ } ->
socket |> Socket.Web.send!({:pong, ""})
end
end
@sorentwo
sorentwo / refresh_bench.rb
Created June 8, 2016 23:56
Benchmarking and tuning the performance of Cache#read_multi with refreshing
require 'benchmark/ips'
require 'readthis'
cache = Readthis::Cache.new(namespace: 'rd', expires_in: 60)
range = ('a'..'z').to_a
range.each { |key| cache.write(key, key) }
Benchmark.ips do |x|
x.report 'read_multi:standard' do
@sorentwo
sorentwo / flippant_test.exs
Created May 17, 2016 20:57
The same tests with different adapters
for adapter <- [Memory, Redis] do
@adapter adapter
setup_all do
Application.put_env(:flippant, :adapter, @adapter)
Application.ensure_started(:flippant)
on_exit fn ->
Application.stop(:flippant)
end
@sorentwo
sorentwo / skylight.rb
Created April 6, 2016 03:13
Knuckles Skylight Integration
module Skylight
module Normalizers
class Knuckles < Normalizer
register 'knuckles.stage'
CAT = 'app.knuckles.pipeline'.freeze
def normalize(trace, name, payload)
stage = payload[:stage]
title = stage.sub(/^Knuckles::(Stages::)?/, '')
@sorentwo
sorentwo / changeset.diff
Created March 9, 2016 17:46
git diff v1.1.3..master -- lib/ecto/changeset.ex
* `valid?` - Stores if the changeset is valid
- * `model` - The changeset root model
+ * `data` - The changeset source data, for example, a struct
* `params` - The parameters as given on changeset creation
* `changes` - The `changes` from parameters that were approved in casting
* `errors` - All errors from validations
* `validations` - All validations performed in the changeset
* `constraints` - All constraints defined in the changeset
* `required` - All required fields as a list of atoms
- * `optional` - All optional fields as a list of atoms
@sorentwo
sorentwo / email_view.ex
Created March 7, 2016 16:09
Elixir/Phoenix markdown email templates
defmodule MyApp.EmailView do
use MyApp.Web, :view
def render(template, format, %{assigns: assigns}) do
rendered = render_to_string(__MODULE__, template, assigns)
case format do
:text -> rendered
:html -> Earmark.to_html(rendered)
end
@sorentwo
sorentwo / email.ex
Last active February 11, 2016 20:16
Normalize and Validate
def normalize(email) do
%{email | from: normalize(email.from, :from),
to: normalize(List.wrap(email.to), :to),
cc: normalize(List.wrap(email.cc), :cc),
bcc: normalize(List.wrap(email.bcc), :bcc)}
end
defp normalize(record, type) do
Formatter.format_email_address(record, %{type: type})
end
@sorentwo
sorentwo / mailer.ex
Created February 11, 2016 19:17
Bamboo.Mailer as a genserver
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
%{adapter: adapter, config: config} = Bamboo.Mailer.parse_opts(__MODULE__, opts)
@adapter adapter
@config config
end
end
def start_link(opts \\ []) do
@sorentwo
sorentwo / query.ex
Created January 7, 2016 22:11
over_query.ex
# SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;
from e in MyApp.EmpSalaray,
select: [e.depname, e.empno, e.salary, avg(e.salary)],
over: partition_by(e.depname)
@sorentwo
sorentwo / postgres.ex
Created December 31, 2015 16:41
Ecto - Run query with trapped exit
defp run_query(opts, sql) do
opts = Keyword.put(opts, :database, "template1")
parent = self()
{:ok, conn} = Connection.connect(opts)
spawn fn ->
value = try do
Connection.query(conn, sql, [], [])