Skip to content

Instantly share code, notes, and snippets.

Avatar

Stephen Bussey sb8244

View GitHub Profile
View gist:351d6561b9617546fbb5
DeviceNotifier = Struct.new(:message, :count) do
def call
notifications.each do |notif|
pusher.push(notif)
end
end
private
def pusher
@sb8244
sb8244 / cluster_loader_balancer.ex
Created Jul 30, 2019
ClusterLoadBalancer for balancing anything (WebSocket) across a cluster
View cluster_loader_balancer.ex
# We use this ClusterLoadBalancer to prevent hot nodes in our load balanced setup.
# The WebSockets are provided by Phoenix through the PushEx application https://github.com/pushex-project/pushex/
# The load balancer's `Worker` module is where the bulk of the cluster orchestration happens, using pg2 for cross node communication
defmodule ClusterLoadBalancer.Behavior do
@moduledoc """
Behavior for implementing a ClusterLoadBalancer compatible tool.
"""
@callback count() :: number
View PushExClient.js
import { Pushex } from 'pushex'
import { getToken } from './tokenService'
const pushexOptions = {
getParams: () => getToken().then(({ token }) => Promise.resolve({ token })),
onConnectionError: pushex => {
pushex.resetParams()
},
}
View troll.ex
defmodule SteveTrollsElixir.Private do
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
@on_definition {unquote(__MODULE__), :on_priv_def}
end
end
defmacro private() do
quote do
View fun_with_router.ex
defmodule TestRouter do
def build(routes) do
contents =
quote do
use Plug.Router
plug :match
plug :dispatch
Enum.map(unquote(Macro.escape(routes)), fn %{match: match, assign: assign} ->
@sb8244
sb8244 / chartkick.ex
Created May 16, 2021
This allows chartkick to be embedded in LiveViews
View chartkick.ex
defmodule Chartkick do
@moduledoc """
Adapted from https://github.com/buren/chartkick-ex/blob/master/lib/chartkick.ex to work with LiveView.
Works in conjunction with Phoenix LiveView hooks to render charts using Chartkick.js library.
"""
require EEx
gen_chart_fn = fn chart_type ->
View contentful-import.json
{
"contentTypes": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "hq8sy8wk8n0y"
}
@sb8244
sb8244 / notion.ex
Created Dec 22, 2021
Example of fetching pages and blocks from Notion's public API in Elixir
View notion.ex
defmodule Clove.Connections.Client.Notion do
# There is not a way to filter last_edited_time, so consumer should check if it needs to fetch the blocks or not
# This will help for deletion detection, though
def retrieve_pages(connection) do
request = %{
method: :post,
url: "#{base_url()}/v1/search",
params: %{
"query" => "",
"sort" => %{
View hook-example.js
/**
* This example is just copy/pasted from my code base. The gist of it is that `this.pushEventTo` on the hook
* will send an event over the LiveView channel that is processed by the component/LiveView that's mounted at
* that element.
*
* I recommend using pushEventTo instead of pushEvent because I've run into situations where the event gets sent
* to the incorrect component or LiveView.
*/
const GeneratePDFButton = {
mounted() {
View header.html
<script type="text/javascript">
if (window.top !== window.self) {
document.body.classList.add('in-iframe')
}
</script>