Skip to content

Commit

Permalink
Format with mix formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtaylor committed May 8, 2019
1 parent f999805 commit 6f377bb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/lowflyingrocks/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ defmodule LowFlyingRocks.Application do
import Supervisor.Spec, warn: false

children = [
worker(LowFlyingRocks.Tweeter, [LowFlyingRocks.Tweeter]),
worker(LowFlyingRocks.Importer, [LowFlyingRocks.Importer]),
worker(LowFlyingRocks.Tweeter, [LowFlyingRocks.Tweeter]),
worker(LowFlyingRocks.Importer, [LowFlyingRocks.Importer])
]

opts = [strategy: :rest_for_one, name: LowFlyingRocks.Supervisor]
Expand Down
10 changes: 5 additions & 5 deletions lib/lowflyingrocks/formatter.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule LowFlyingRocks.Formatter do

def format(neo) do
t = neo.timestamp
body = generate_body(neo)
{t, body}
end

defp generate_body(neo) do
"#{neo.name}, #{format_diameter_range(neo)} in diameter, just passed the Earth at #{format_speed(neo)}, missing by #{format_distance(neo)}. #{neo.url}"
"#{neo.name}, #{format_diameter_range(neo)} in diameter, just passed the Earth at #{
format_speed(neo)
}, missing by #{format_distance(neo)}. #{neo.url}"
end

defp format_diameter_range(neo) do
Expand All @@ -23,12 +24,11 @@ defmodule LowFlyingRocks.Formatter do

defp format_distance(neo) do
au = neo.distance
km = au * 149598000
count = km |> round |> Integer.digits |> Enum.count
km = au * 149_598_000
count = km |> round |> Integer.digits() |> Enum.count()
non_sig_count = count - 3
divider = :math.pow(10, non_sig_count)
s = round(km / divider) * divider
"#{Number.Delimit.number_to_delimited(s, precision: 0)}km"
end

end
10 changes: 5 additions & 5 deletions lib/lowflyingrocks/importer.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule LowFlyingRocks.Importer do
require Logger
use GenServer
alias LowFlyingRocks.{Parser,Formatter,Tweeter}
alias LowFlyingRocks.{Parser, Formatter, Tweeter}

def start_link(name \\ nil) do
GenServer.start_link(__MODULE__, :ok, [name: name])
GenServer.start_link(__MODULE__, :ok, name: name)
end

def init(:ok) do
Expand All @@ -21,14 +21,16 @@ defmodule LowFlyingRocks.Importer do
case fetch() do
{:ok, body} ->
body |> parse |> format |> schedule

:error ->
Logger.error("Failed to download NEOs")
end
end

defp fetch do
Logger.info("Downloading NEOs from JPL API")
response = HTTPotion.get("https://ssd-api.jpl.nasa.gov/cad.api?dist-max=0.2", [timeout: 30_000])
response = HTTPotion.get("https://ssd-api.jpl.nasa.gov/cad.api?dist-max=0.2", timeout: 30_000)

case HTTPotion.Response.success?(response) do
true -> {:ok, response.body}
false -> :error
Expand All @@ -50,6 +52,4 @@ defmodule LowFlyingRocks.Importer do
defp interval do
Application.fetch_env!(:lowflyingrocks, :import_interval)
end

end

23 changes: 11 additions & 12 deletions lib/lowflyingrocks/parser.ex
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
defmodule LowFlyingRocks.Parser do

alias LowFlyingRocks.NEO

def parse(json) do
Poison.Parser.parse!(json)
|> Map.fetch!("data")
|> Enum.reject(fn(d) ->
|> Enum.reject(fn d ->
d |> Enum.at(10) == nil
end)
|> Enum.map(fn(d) ->
|> Enum.map(fn d ->
name = d |> Enum.at(0)
distance = d |> Enum.at(4) |> String.to_float
speed = d |> Enum.at(7) |> String.to_float
h = d |> Enum.at(10) |> String.to_float
distance = d |> Enum.at(4) |> String.to_float()
speed = d |> Enum.at(7) |> String.to_float()
h = d |> Enum.at(10) |> String.to_float()
timestamp = d |> Enum.at(3) |> parse_timestamp

url = "https://ssd.jpl.nasa.gov/sbdb.cgi?sstr=#{name}" |> URI.encode
url = "https://ssd.jpl.nasa.gov/sbdb.cgi?sstr=#{name}" |> URI.encode()

%NEO{
name: name,
name: name,
timestamp: timestamp,
distance: distance,
distance: distance,
speed: speed,
diameter_min: diameter_min(h),
diameter_max: diameter_max(h),
url: url,
url: url
}
end)
end
Expand All @@ -39,13 +38,13 @@ defmodule LowFlyingRocks.Parser do

defp h_to_diameter(h, p) do
ee = -0.2 * h
(1329.0 / :math.sqrt(p)) * :math.pow(10, ee) * 1000
1329.0 / :math.sqrt(p) * :math.pow(10, ee) * 1000
end

defp parse_timestamp(ts) do
s = "{YYYY}-{Mshort}-{0D} {0h24}:{m}"

Timex.Parse.DateTime.Parser.parse!(ts, s)
|> DateTime.from_naive!("Etc/UTC")
end

end
11 changes: 6 additions & 5 deletions lib/lowflyingrocks/tweeter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule LowFlyingRocks.Tweeter do
alias Timex.Duration

def start_link(name \\ nil) do
GenServer.start_link(__MODULE__, :ok, [name: name])
GenServer.start_link(__MODULE__, :ok, name: name)
end

def set_tweets(server, tweets) do
Expand Down Expand Up @@ -36,7 +36,7 @@ defmodule LowFlyingRocks.Tweeter do

defp timeout_for_tweet({timestamp, _body}) do
timestamp
|> Timex.diff(Timex.now, :duration)
|> Timex.diff(Timex.now(), :duration)
|> Duration.to_milliseconds(truncate: true)
|> max(0)
end
Expand All @@ -56,13 +56,15 @@ defmodule LowFlyingRocks.Tweeter do
defp filter_old_tweets(tweets) do
now = DateTime.utc_now()

tweets |> Enum.reject(fn({t, _}) ->
tweets
|> Enum.reject(fn {t, _} ->
compare_timestamps(t, now)
end)
end

defp sort_tweets_by_timestamp(tweets) do
tweets |> Enum.sort(fn({a, _}, {b, _}) ->
tweets
|> Enum.sort(fn {a, _}, {b, _} ->
compare_timestamps(a, b)
end)
end
Expand All @@ -74,5 +76,4 @@ defmodule LowFlyingRocks.Tweeter do
:gt -> false
end
end

end
11 changes: 7 additions & 4 deletions test/lowflyingrocks_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule LowFlyingRocksTest do
use ExUnit.Case
alias LowFlyingRocks.{NEO,Formatter,Parser}
alias LowFlyingRocks.{NEO, Formatter, Parser}

test "parsing json" do
json = File.read!("test/api.json")
Expand All @@ -17,7 +17,7 @@ defmodule LowFlyingRocksTest do
assert first.url == "https://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2017%20AN4"

timestamp = DateTime.from_naive!(~N[2017-01-16 10:44:00.000], "Etc/UTC")
assert (DateTime.compare(timestamp, first.timestamp) == :eq)
assert DateTime.compare(timestamp, first.timestamp) == :eq
end

test "formatting NEO" do
Expand All @@ -35,7 +35,10 @@ defmodule LowFlyingRocksTest do

{t, s} = Formatter.format(neo)
assert DateTime.compare(t, timestamp) == :eq
assert s == "2017 AN4, 60m-133m in diameter, just passed the Earth at 26km/s, missing by 9,440,000km. #{neo.url}"
end

assert s ==
"2017 AN4, 60m-133m in diameter, just passed the Earth at 26km/s, missing by 9,440,000km. #{
neo.url
}"
end
end

0 comments on commit 6f377bb

Please sign in to comment.