Skip to content

Commit

Permalink
Add resource loading plug
Browse files Browse the repository at this point in the history
  • Loading branch information
cpjk committed Apr 22, 2015
0 parents commit f00dc99
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/_build
/deps
erl_crash.dump
*.ez
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Canary
======

** TODO: Add description **
24 changes: 24 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for third-
# party users, it should be done in your mix.exs file.

# Sample configuration:
#
# config :logger, :console,
# level: :info,
# format: "$date $time [$level] $metadata$message\n",
# metadata: [:user_id]

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
30 changes: 30 additions & 0 deletions lib/canary.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Canary.LoadResource do

def init(options) do
options
end

def call(conn, opts) do
conn
|> load_resource(opts[:resource])
end

@doc """
Load the resource given by conn.params["id"] and
add it to conn.assigns.
"""
def load_resource(conn, resource) do
fetched_resource = resource
|> fetch_resource(conn.params["id"])

%{ conn | assigns: Map.put(conn.assigns, :fetched_resource, fetched_resource) }
end

@doc """
Fetch the resource from the database. Initially only supports ecto.
Need a place to define which repo to use
"""
def fetch_resource(resource, resource_id) do
Cooking.Repo.get(resource, resource_id)
end
end
27 changes: 27 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Asd.Mixfile do
use Mix.Project

def project do
[app: :canary,
version: "0.0.1",
elixir: "~> 1.0",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end

# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[applications: [:logger]]
end

defp deps do
[
{ :ecto, "~> 0.10.0" },
{ :canada, "~> 1.0.0" },
{ :plug, "~> 0.12.0" }
]
end
end
5 changes: 5 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%{"canada": {:hex, :canada, "1.0.0"},
"decimal": {:hex, :decimal, "1.1.0"},
"ecto": {:hex, :ecto, "0.10.2"},
"plug": {:hex, :plug, "0.12.1"},
"poolboy": {:hex, :poolboy, "1.4.2"}}
7 changes: 7 additions & 0 deletions test/canary_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule CanaryTest do
use ExUnit.Case

test "the truth" do
assert 1 + 1 == 2
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit f00dc99

Please sign in to comment.