Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

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

To claim this, I am signing this object:

@adanselm
adanselm / plug_router.ex
Created June 20, 2019 07:19
Plug hook to do something before HTTP response is sent (like logging)
defmodule MyApp.PlugRouter do
use Plug.Router
require Logger
plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
plug :match
@adanselm
adanselm / compare_dirs.ex
Created October 16, 2018 10:01
Find files from ref dir that are not present in target dir
defmodule CompareDirs do
def compare(ref, target) do
ref_list = md5_dir(ref)
target_list = md5_dir(target)
Enum.reduce(ref_list, [], fn({path, hash}, acc) ->
case Enum.find(target_list, fn({_t_path, t_hash}) -> t_hash == hash end) do
true -> acc
_ -> acc ++ [path]
@adanselm
adanselm / tasks.ex
Created June 25, 2015 08:27
Retrieving timeslots and publishing tweets
defmodule ObesebirdApi.Tasks do
import Ecto.Query
alias ObesebirdApi.Slot
alias ObesebirdApi.Post
alias ObesebirdApi.Repo
require Logger
def get(day_of_week, {hour, min, _sec}) do
query = from s in Slot,
@adanselm
adanselm / scheduler.ex
Last active August 29, 2015 14:23
Scheduling recurring tasks with a GenServer
defmodule ObesebirdApi.Scheduler do
use GenServer
alias ObesebirdApi.Slot
alias ObesebirdApi.Tasks
require Logger
# Server
def init(args) do
case Process.send(self, :check, []) do
@adanselm
adanselm / .travis.yml
Last active August 29, 2015 14:23
Obesebird_api travis configuration
language: elixir
elixir:
- 1.0.4
otp_release:
- 17.4
addons:
postgresql: '9.3'
before_script:
- MIX_ENV=test mix do deps.get, deps.compile, ecto.create, ecto.migrate
deploy:
@adanselm
adanselm / prod.secret.exs
Last active August 29, 2015 14:23
Production configuration reading from heroku env
use Mix.Config
# In this file, we keep production configuration that
# you likely want to automate and keep it away from
# your version control system.
config :obesebird_api, ObesebirdApi.Endpoint,
secret_key_base: System.get_env("SECRET_KEY_BASE")
# Configure your database
config :obesebird_api, ObesebirdApi.Repo,
@adanselm
adanselm / router.ex
Last active August 29, 2015 14:23
Obesebird_api router
defmodule ObesebirdApi.Router do
use ObesebirdApi.Web, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", ObesebirdApi do
pipe_through :api
@adanselm
adanselm / generated_routes.txt
Created June 17, 2015 09:36
The routes created by our router
api_user_path GET /api/users SbSso.Api.UserController.index/2
api_user_path GET /api/users/:id SbSso.Api.UserController.show/2
api_user_path POST /api/users SbSso.Api.UserController.create/2
api_user_path PATCH /api/users/:id SbSso.Api.UserController.update/2
PUT /api/users/:id SbSso.Api.UserController.update/2
api_user_path DELETE /api/users/:id SbSso.Api.UserController.destroy/2
api_user_subscription_path GET /api/users/:user_id/subs SbSso.Api.SubscriptionController.index/2
api_user_subscription_path GET /api/users/:user_id/subs/:id SbSso.Api.SubscriptionController.show/2
api_user_subscription_path POST /api/users/:user_id/subs SbSso.Api.SubscriptionController.create/2
api_product_path GET /api/products SbSso.Api.ProductController.index/2
@adanselm
adanselm / router.ex
Created June 17, 2015 09:20
A typical Phoenix router
defmodule SbSso.Router do
use Phoenix.Router
pipeline :api do
plug :accepts, ~w(json)
end
# /API section
scope "/api", alias: SbSso.Api, as: :api do
pipe_through :api