From 5654e5f14dd42fb36268995323dc526d335d00a9 Mon Sep 17 00:00:00 2001 From: Michael Schaefermeyer Date: Fri, 2 Sep 2016 10:57:38 +0200 Subject: Refactor following credo --- lib/boltex.ex | 8 +++++++- lib/boltex/bolt.ex | 8 ++++---- lib/boltex/pack_stream.ex | 10 ++++------ lib/boltex/utils.ex | 6 ++++-- 4 files changed, 19 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/boltex.ex b/lib/boltex.ex index 992009f..a5b2bbf 100644 --- a/lib/boltex.ex +++ b/lib/boltex.ex @@ -1,4 +1,10 @@ defmodule Boltex do + @moduledoc """ + Elixir library for using the Neo4J Bolt Protocol. + + It supports de- and encoding of Boltex binaries and sending and receiving + of data using the Bolt protocol. + """ alias Boltex.Bolt def test(host, port, query, params \\ %{}, auth \\ nil) do @@ -7,7 +13,7 @@ defmodule Boltex do :ok = Bolt.handshake :gen_tcp, p :ok = Bolt.init :gen_tcp, p, auth - IO.inspect Bolt.run_statement(:gen_tcp, p, query) + Bolt.run_statement(:gen_tcp, p, query) end end diff --git a/lib/boltex/bolt.ex b/lib/boltex/bolt.ex index 984d0b6..d433ea6 100644 --- a/lib/boltex/bolt.ex +++ b/lib/boltex/bolt.ex @@ -89,7 +89,8 @@ defmodule Boltex.Bolt do Messages have to be in the form of {[messages], signature}. """ def send_messages(transport, port, messages) do - Enum.map(messages, &generate_binary_message/1) + messages + |> Enum.map(&generate_binary_message/1) |> generate_chunks |> Enum.each(&(transport.send(port, &1))) end @@ -151,7 +152,7 @@ defmodule Boltex.Bolt do ] with {:success, %{}} = data <- receive_data(transport, port), - do: [data | receive_data(transport, port) |> List.wrap] + do: [data | transport |> receive_data(port) |> List.wrap] end @doc """ @@ -178,7 +179,7 @@ defmodule Boltex.Bolt do * `:failure` """ def receive_data(transport, port, previous \\ []) do - case do_receive_data(transport, port) |> unpack do + case transport |> do_receive_data(transport) |> unpack do {:record, _} = data -> receive_data transport, port, [data | previous] @@ -207,7 +208,6 @@ defmodule Boltex.Bolt do {:error, :timeout} -> {:error, :no_more_data_received} other -> - IO.inspect Utils.hex_encode other raise "receive failed" end end diff --git a/lib/boltex/pack_stream.ex b/lib/boltex/pack_stream.ex index 736351f..aeca3a9 100644 --- a/lib/boltex/pack_stream.ex +++ b/lib/boltex/pack_stream.ex @@ -56,9 +56,8 @@ defmodule Boltex.PackStream do def decode(<< 0xD5, list_size :: 16 >> <> bin), do: list(bin, list_size) def decode(<< 0xD6, list_size :: 32 >> <> bin), do: list(bin, list_size) def decode(<< 0xD7 >> <> bin) do - position = - for(<< byte <- bin >>, do: byte) - |> Enum.find_index(&(&1 == 0xDF)) + bytes = for(<< byte <- bin >>, do: byte) + position = Enum.find_index bytes, &(&1 == 0xDF) << list :: binary-size(position), 0xDF, rest :: binary >> = bin @@ -71,9 +70,8 @@ defmodule Boltex.PackStream do def decode(<< 0xD9, entries :: 16 >> <> bin), do: map(bin, entries) def decode(<< 0xDA, entries :: 32 >> <> bin), do: map(bin, entries) def decode(<< 0xDB >> <> bin) do - position = - for(<< byte <- bin >>, do: byte) - |> Enum.find_index(&(&1 == 0xDF)) + bytes = for(<< byte <- bin >>, do: byte) + position = Enum.find_index bytes, &(&1 == 0xDF) << map:: binary-size(position), 0xDF, rest :: binary >> = bin diff --git a/lib/boltex/utils.ex b/lib/boltex/utils.ex index 333217e..fea5759 100644 --- a/lib/boltex/utils.ex +++ b/lib/boltex/utils.ex @@ -1,4 +1,6 @@ defmodule Boltex.Utils do + @moduledoc "Different utils used to debugging and helping." + def reduce_to_binary(enumerable, transform) do Enum.reduce enumerable, <<>>, fn(data, acc) -> acc <> transform.(data) end end @@ -8,7 +10,7 @@ defmodule Boltex.Utils do end def hex_decode(hex_list) do - for(hex <- hex_list, do: Integer.parse(hex, 16) |> elem(0)) - |> reduce_to_binary(&<<&1>>) + integers = for(hex <- hex_list, do: hex |> Integer.parse(16) |> elem(0)) + reduce_to_binary integer, &<<&1>> end end -- cgit v1.2.1