Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / authorization_request.ex
Created April 5, 2018 12:27
A simplified example of storing an authorization request for an openeid connect valid code flow request
defmodule App.AuthorizationRequest do
use Ecto.Schema
schema "authorization_requests" do
field(:client_id, :string)
field(:code, :string)
field(:nonce, :string)
field(:access_token, :string)
field(:refresh_token, :string)
field(:scope, :string)
@myobie
myobie / example_to_download_file.ex
Created April 3, 2018 09:59
Send a file in phoenix (plug)
defmodule AppWeb.Controller do
use AppWeb, :controller
# sending bytes
def show(conn, %{"id" => id}) do
data = Storage.get_data_for_file_id(id)
conn
|> put_resp_header(
"content-disposition",
defmodule Someapp.FileStore do
@type key :: String.t()
@type data :: binary
@type options :: keyword
@callback put(key, data, options) :: :ok | {:error, atom}
@callback get(key) :: {:ok, data} | {:error, atom}
@callback download(key, Path.t()) :: :ok | {:error, atom}
@callback upload(key, Path.t(), options) :: :ok | {:error, atom}
@callback stream_down(Plug.Conn.t(), key, options) :: Plug.Conn.t() | no_return
@myobie
myobie / brunch-config.js
Last active February 8, 2018 17:11
My basic brunch.io config (for things like gohugo.io) I keep my stuff in ./assets/ so that's why public is ../public
exports.files = {
javascripts: {
joinTo: 'js/app.js'
},
stylesheets: {
joinTo: 'css/app.css'
}
}
exports.conventions = {
@myobie
myobie / example-template-usage.html
Last active August 30, 2020 15:00
Recursively list out sections and pages for a nice hugo navigation for a section or site
{{ define "main" }}
<div id="wrapper">
<div id="sidebar">
{{ partial "sidebar-sections-list.html" .Site.Sections }}
</div>
<div id="content">
<!-- ... -->
</div>
</div>
{{end}}
"abcdefghijklmnopqrstuvwxyzäüößABCDEFGHIJKLMNOPQRSTUVWXYZÄÜÖ" |> String.codepoints()
@myobie
myobie / hound-stage-example.ex
Last active December 1, 2017 15:52
This is an example of using GenStage to have a Hound producer/consumer, but the same thing can be done with just a GenServer
defmodule ExampleChecker do
use Hound.Helpers
use GenStage
def start_link(arg \\ []),
do: GenStage.start_link(__MODULE__, arg, [])
def init(_) do
try do
session_id = Hound.start_session()
@myobie
myobie / example-throws-in-callback-that-works-in-playgrounds.swift
Created November 29, 2017 02:36
An example of swift async callbacks that either throw or return a result
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// either throws an Error or returns a String result
typealias ThrowsCallback = () throws -> (String)
enum AsyncError: Error {
case kaboom
}
@myobie
myobie / example-throws-in-callback.swift
Last active November 29, 2017 02:36
An example of swift async callbacks that either throw or return a result
import Foundation
// either throws an Error or returns a String result
typealias ThrowsCallback = () throws -> (String)
enum AsyncError: Error {
case kaboom
}
func asyncActionWithCallback(_ someData: String, callback: @escaping (ThrowsCallback) -> Void) {
defmodule App do
defmacro protect(do: block) do
quote do
try
unquote(block)
rescue
error -> {:error, error}
end
end
end