Repository: jeregrine/jsonapi (elixir)

Issue:
Replace underscores for dashs in multi word names
Seems that according to jsonapi spec and ember data jsonapi,

some_thing , should become some-thing.

In the jsonapi.org page, it has the example:
....

```
attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
```

....

If i manage to learn how to do this, i can do it on the weekend and write some tests.


Pull request description:
Allow for underscores to be converted to dashes.
@jeregrine would love to get your thoughts on this first pass.

This closes #12 

Patch:
diff --git a/lib/jsonapi/view.ex b/lib/jsonapi/view.ex
index 10c56b7..da78516 100644
--- a/lib/jsonapi/view.ex
+++ b/lib/jsonapi/view.ex
@@ -82,7 +82,15 @@ defmodule JSONAPI.View do
 
       #TODO Figure out the nesting of fields
       def attributes(data, conn) do
-        Map.take(data, fields())
+        underscore_to_dash = Application.get_env(:jsonapi, :underscore_to_dash, false)
+
+        data = Map.take(data, fields())
+
+        if underscore?() do
+          underscore(data)
+        else
+          data
+        end
       end
 
       def meta(_data, _conn), do: nil
@@ -120,7 +128,6 @@ defmodule JSONAPI.View do
         "#{url_for(data, conn)}/relationships/#{rel_type}"
       end
 
-
       if Code.ensure_loaded?(Phoenix) do
         def render("show.json", %{data: data, conn: conn}),
           do: show(data, conn, conn.params)
@@ -133,6 +140,21 @@ defmodule JSONAPI.View do
           do: show(data, conn, params)
       end
 
+      defp underscore?, do: Application.get_env(:jsonapi, :underscore_to_dash, false)
+
+      defp underscore(data) do
+        data
+        |> Enum.map(fn {key, value} -> {underscore_key(key), value} end)
+        |> Enum.into(%{})
+      end
+
+      defp underscore_key(key) do
+        key
+        |> to_string
+        |> String.replace("_", "-")
+        |> String.to_atom
+      end
+
       defoverridable attributes: 2,
                      fields: 0,
                      id: 1,


Test patch:
diff --git a/test/jsonapi/view_test.exs b/test/jsonapi/view_test.exs
index a2675b1..5357f8d 100644
--- a/test/jsonapi/view_test.exs
+++ b/test/jsonapi/view_test.exs
@@ -17,6 +17,16 @@ defmodule JSONAPI.ViewTest do
     use JSONAPI.View, type: "posts", namespace: "/api"
   end
 
+  defmodule UserView do
+    use JSONAPI.View,
+      type: "users",
+      namespace: "/api"
+
+    def fields do
+      [:age, :first_name, :last_name]
+    end
+  end
+
   alias JSONAPI.ViewTest.CommentView
 
   test "type/0 when specified via using macro" do
@@ -41,4 +51,10 @@ defmodule JSONAPI.ViewTest do
   test "render/2 is not defined when 'Phoenix' is not loaded" do
     refute {:render, 2} in PostView.__info__(:functions)
   end
+
+  test "attributes/2 with `:underscore_to_dash` option" do
+    Application.put_env(:jsonapi, :underscore_to_dash, true)
+    expected_map = %{age: 100, "first-name": "Jason", "last-name": "S"}
+    assert expected_map == UserView.attributes(%{age: 100, first_name: "Jason", last_name: "S"}, nil)
+  end
 end
diff --git a/test/serializer_test.exs b/test/serializer_test.exs
index 47f37f0..e7fee72 100644
--- a/test/serializer_test.exs
+++ b/test/serializer_test.exs
@@ -69,7 +69,7 @@ defmodule JSONAPISerializerTest do
     assert Enum.count(encoded[:included]) == 4
   end
 
-  test "serialize handles a list " do
+  test "serialize handles a list" do
     data = %{
       id: 1,
       text: "Hello",
@@ -99,7 +99,7 @@ defmodule JSONAPISerializerTest do
     assert Enum.count(encoded[:included]) == 4
   end
 
-  test "serialize handles an empty relationship " do
+  test "serialize handles an empty relationship" do
     data = %{
       id: 1,
       text: "Hello",
@@ -124,7 +124,7 @@ defmodule JSONAPISerializerTest do
     assert Enum.count(encoded[:included]) == 1
   end
 
-  test "serialize handles a nil relationship " do
+  test "serialize handles a nil relationship" do
     data = %{
       id: 1,
       text: "Hello",