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 --- README.md | 2 ++ lib/boltex.ex | 8 +++++++- lib/boltex/bolt.ex | 8 ++++---- lib/boltex/pack_stream.ex | 10 ++++------ lib/boltex/utils.ex | 6 ++++-- mix.exs | 4 +++- mix.lock | 8 ++++++-- 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1d10461..1aec124 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Boltex [![Build Status](https://travis-ci.org/mschae/boltex.svg?branch=master)](https://travis-ci.org/mschae/boltex) +[![Inline docs](http://inch-ci.org/github/mschae/boltex.svg?branch=master)](http://inch-ci.org/github/mschae/boltex) + Elixir implementation of the Bolt protocol and corresponding PackStream protocol. Both is being used by Neo4J. 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 diff --git a/mix.exs b/mix.exs index 3c6421f..078f6cd 100644 --- a/mix.exs +++ b/mix.exs @@ -33,7 +33,9 @@ defmodule Boltex.Mixfile do # Type "mix help deps" for more examples and options defp deps do [ - {:ex_doc, "~> 0.13.0", only: [:dev]}, + {:credo, "~> 0.3", only: [:dev, :test]}, + {:ex_doc, "~> 0.13.0", only: [:docs]}, + {:inch_ex, "~> 0.5.2", only: [:docs]}, {:mix_test_watch, "~> 0.2.6", only: [:dev, :test]}, ] end diff --git a/mix.lock b/mix.lock index 76a8d11..553111e 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,10 @@ -%{"connection": {:hex, :connection, "1.0.3", "3145f7416be3df248a4935f24e3221dc467c1e3a158d62015b35bd54da365786", [:mix], []}, +%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []}, + "connection": {:hex, :connection, "1.0.3", "3145f7416be3df248a4935f24e3221dc467c1e3a158d62015b35bd54da365786", [:mix], []}, + "credo": {:hex, :credo, "0.4.11", "03a64e9d53309b7132556284dda0be57ba1013885725124cfea7748d740c6170", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]}, "db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]}, "earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], []}, "ex_doc": {:hex, :ex_doc, "0.13.0", "aa2f8fe4c6136a2f7cfc0a7e06805f82530e91df00e2bff4b4362002b43ada65", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, - "mix_test_watch": {:hex, :mix_test_watch, "0.2.6", "9fcc2b1b89d1594c4a8300959c19d50da2f0ff13642c8f681692a6e507f92cab", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}]}} + "inch_ex": {:hex, :inch_ex, "0.5.3", "39f11e96181ab7edc9c508a836b33b5d9a8ec0859f56886852db3d5708889ae7", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]}, + "mix_test_watch": {:hex, :mix_test_watch, "0.2.6", "9fcc2b1b89d1594c4a8300959c19d50da2f0ff13642c8f681692a6e507f92cab", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}]}, + "poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []}} -- cgit v1.2.1