Skip to content

Instantly share code, notes, and snippets.

View spiegela's full-sized avatar

Aaron Spiegel spiegela

View GitHub Profile
@spiegela
spiegela / json_to_csv.rb
Created August 4, 2013 20:45
Sometimes, I get JSON arrays, and I want them to be CSV files.
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
raise Exception, 'you must provide a json file' unless ARGV[0]
json = JSON.parse(File.open(ARGV[0]).read)
puts json.first.collect {|k,v| k}.join(',')
puts json.collect {|node| "#{node.collect{|k,v| v}.join(',')}\n"}.join
// Select box utilizing Select2 functionality that overrides Ember.Select;
// Define view in the same way that you would an Ember.Select view.
// Additional attributes supported are: width, allowClear, and closeOnSelect;
// Example view:
// {{ view App.Select2
// viewName="fieldValueSelect2"
// prompt="Please select a value list"
// contentBinding="controller.fieldValuesLists"
// optionLabelPath="content.name"
// optionValuePath="content.id"
// Select box utilizing Select2 functionality that overrides Ember.Select;
// Define view in the same way that you would an Ember.Select view.
// Additional attributes supported are: width, allowClear, and closeOnSelect;
// Example view:
// {{ view App.Select2
// viewName="fieldValueSelect2"
// prompt="Please select a value list"
// contentBinding="controller.fieldValuesLists"
// optionLabelPath="content.name"
// optionValuePath="content.id"
@spiegela
spiegela / fizzbuzz.erl
Created October 21, 2013 02:21
Fizzbuzz implemented with tail-recursion in erlang.
-module(fizzbuzz).
-compile(export_all).
run() ->
run(lists:seq(1,100), []).
run([], Acc) ->
lists:reverse(Acc);
run([H|T], Acc) ->
run(T, [print_string(H)|Acc]).
@spiegela
spiegela / rest.exs
Created November 1, 2013 15:32
A RESTful router example with Dynamo
# Run this app from Dynamo root with:
# mix run -r examples/rest.exs --no-halt
#
# Open a browser at URL http://localhost:3030 see the wonderment of rest.
#
defmodule Rest do
# Cowboy Rest Handler resources
#
# * User guide: http://ninenines.eu/docs/en/cowboy/HEAD/guide/rest_handlers
@spiegela
spiegela / fruit_handler.ex
Last active December 27, 2015 13:09
Dynamo Restful Resource Example
# Run this app from Dynamo root with:
# mix run -r examples/fruit_handler.exs --no-halt
#
# Open a browser at URL http://localhost:3030 see the wonderment of rest.
#
defmodule Fruit do
# Cowboy REST handler resources
#
# * User guide: http://ninenines.eu/docs/en/cowboy/HEAD/guide/rest_handlers
@spiegela
spiegela / fruit_collection_handler.ex
Last active December 27, 2015 15:28
Possible API for developing RESTful routes & handlers in Dynamo
defmodule FruitCollectionHandler do
use Dynamo.RESTHandler
allowed_methods ["GET", "POST", "OPTIONS"]
provide "application/json", :get_json
provide "text/html", :get_html
accept "application/json", :post_json
prepare do
@spiegela
spiegela / fruit_collection_handler.ex
Last active December 27, 2015 17:39
More experimental RESTful router implementation
defmodule RESTRouter.FruitCollectionHandler do
provides "application/json"
provides "text/html"
provides "text/plain"
accepts "application/json"
defp fetch
Fruit.to_sequence.reverse
end
defmodule Dynamo.Ecto.Resource do
def object
conn.fetch :params
id = conn.params[:id]
query = # Ecto DB Query creation
object = Ecto.execute query
end
end
defmodule Fruit do
# In some dynamo project
defmodule MyProj.Mixfile do
def deps(:prod) do
[ :ecto ]
end
end
# Generate source, tests, dir structure, etc.
# mix ecto.gen.repo -> Generate a database respository in Ecto,