Skip to content

Instantly share code, notes, and snippets.

@jeregrine
jeregrine / comment_view.ex
Last active August 29, 2015 14:17
Encoding with Views
defmodule MyApp.CommentView do
use MyApp.Web, :view
@attributes ~W(id name inserted_at)
def render("show.json", data: data) when is_list(data) do
for c <- data do
render("show.json", data: data)
end
end
@jeregrine
jeregrine / controller.ex
Last active August 29, 2015 14:17
Encoding with Poison.
defmodule MyApp.MyModelController do
use MyApp.Web, :controller
plug :action
def show(conn, params) do
json conn, Repo.get!(MyModel, params[:id])
end
end
#!/usr/bin/env python
import random
obliqstrats = ['(Organic) machinery', '(This Line Intentionally Left Blank)', 'A line has two sides', 'A very small object -Its centre', 'Abandon desire', 'Abandon normal instructions', 'Accept advice', 'Accretion', 'Adding on', 'Allow an easement (an easement is the abandonment of a stricture)', 'Always first steps', 'Always give yourself credit for having more than personality', 'Always the first steps', 'Are there sections? Consider transitions', 'Ask people to work against their better judgement', 'Ask your body', 'Assemble some of the elements in a group and treat the group', 'Back up a few steps. What else could you have done?', 'Balance the consistency principle with the inconsistency principle', 'Be dirty', 'Be extravagant', 'Be less critical more often', 'Breathe more deeply', 'Bridges -build -burn', 'Call your mother and ask her what to do.', 'Cascades', 'Change ambiguities to specifics', 'Change instrument roles', 'Change nothing and continue with immaculate c
@jeregrine
jeregrine / test.rb
Created October 15, 2014 14:52
save and after_commit
def save_and_after_commit(&block)
Sequel::Model.db.transaction do
Sequel::Model.db.after_commit(&block)
self.save
end
end
def index(conn, %{"event" => "true"}) do
conn = conn
|> put_resp_content_type("text/event-stream")
|> send_chunked(200)
{:ok, conn} = chunk(conn, ["data: ", JSON.encode!(Thermostat.Data.get()), "\n\n"])
Phoenix.Topic.subscribe self, "data"
data_updated(conn)
end
defp data_updated(conn) do
# Simple Phoenix authentication plug
#
# - based on Plug's session store
# - redirects unauthenticated requests to login page "/login/<request url>"
# - /static/... requests are not authenticated
# - authentication is valid as long as session is valid (you can change this behaviour easily)
# Because we need session to be fetched BEFORE this plug, we must put this to router.ex:
#----------------------------
gem 'rest-client', '~> 1.6.7'
gem 'nokogiri'
@jeregrine
jeregrine / polyline_decode.ex
Last active August 29, 2015 13:59
Google Maps Polyline Decoder. Based almost entirely off of https://gist.github.com/signed0/2031157#file-gistfile1-py-L55
defmodule Roadcast.Google.Polyline do
use Bitwise
#TODO Work on this code
def decode(polyline) when is_binary(polyline) do
decode(lc <<x>> inbits polyline, do: x - 63)
end
def decode(coll) when is_list(coll) do
coll
'''Provides utility functions for encoding and decoding linestrings using the
Google encoded polyline algorithm.
'''
def encode_coords(coords):
'''Encodes a polyline using Google's polyline algorithm
See http://code.google.com/apis/maps/documentation/polylinealgorithm.html
for more information.

Keybase proof

I hereby claim:

  • I am jeregrine on github.
  • I am peregrine (https://keybase.io/peregrine) on keybase.
  • I have a public key whose fingerprint is 95F5 DBCD 84B3 A773 7308 9222 3C23 D809 EF15 3FD2

To claim this, I am signing this object: