Skip to content

Instantly share code, notes, and snippets.

@tzumby
Created January 31, 2019 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzumby/42d2740f08932cb3ae21a8f55bab14d2 to your computer and use it in GitHub Desktop.
Save tzumby/42d2740f08932cb3ae21a8f55bab14d2 to your computer and use it in GitHub Desktop.
Bittrex client
defmodule CryptoPrices.Aggregator.Bittrex do
@limit 10
@base "http://api.bittrex.com/api/v1.1/public/getorderbook"
def compute(pair, _opts) do
pair
|> fetch_json()
|> format_results()
end
defp format_results({:ok, %{ "result" => %{ "buy" => buy }}}) do
buy
|> Enum.map(&build_bid/1)
|> Enum.take(@limit)
end
defp build_bid(%{ "Quantity" => quantity, "Rate" => price }) do
%{price: price, quantity: quantity}
end
defp fetch_json(pair) do
case HTTPoison.get(url(pair)) do
{:ok, %HTTPoison.Response{body: body, status_code: 200}} ->
{:ok, Jason.decode!(body) }
{:ok, %HTTPoison.Response{status_code: 404}} ->
{:error, :not_found }
{:ok, %HTTPoison.Error{reason: reason}} ->
{:error, reason }
end
end
defp url(pair) do
"#{@base}?" <> URI.encode_query(market: pair, type: "both")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment