Skip to content

Commit

Permalink
Slight refactor and use new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Callum Barratt committed Jan 27, 2016
1 parent 7dec065 commit 054f532
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/mix/tasks/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Mix.Tasks.Api do
alias Callumapi.{Repo, Weight, Macro}

@myfitnesspal_endpoint "https://www.myfitnesspal.com/account/login"
@withings_endpoint "http://callumbarratt.herokuapp.com/api/v1/weighins"
@withings_endpoint "http://callum-rails-api.vdtapp.com/api/v1/weighins"
@macro_types ["calories", "carbs", "fat", "protein"]

@shortdoc "Import Macronutrient & Withings data from Rails API"
Expand Down Expand Up @@ -43,7 +43,7 @@ defmodule Mix.Tasks.Api do
"""

def retrieve_data(macro) do
HTTPoison.get!("http://www.myfitnesspal.com/reports/results/nutrition/#{macro}/400.json?report_name=#{macro}", %{"Cookie" => mfp_auth_session}).body
HTTPoison.get!("http://www.myfitnesspal.com/reports/results/nutrition/#{macro}/200.json?report_name=#{macro}", %{"Cookie" => mfp_auth_session}).body
|> Poison.decode!
end

Expand All @@ -58,17 +58,22 @@ defmodule Mix.Tasks.Api do
@macro_types
|> Enum.each(fn macronutrient ->
retrieve_data(macronutrient)["data"]
|> Enum.each(fn json ->
if json["total"] > 0.0 do
sanitized_value = json["total"] |> round |> to_string
formatted_date = format_date(json)
|> process_data(macronutrient)
end)
end

record = macro_exist?(formatted_date)
def process_data(data, macronutrient) do
data
|> Enum.each(fn json ->
if json["total"] > 0.0 do
sanitized_value = json["total"] |> round |> to_string
formatted_date = format_date(json)

new_macro = %{macronutrient: macronutrient, sanitized_value: sanitized_value, date: formatted_date}
|> save_macro(record)
end
end)
record = macro_exist?(formatted_date)

new_macro = %{macronutrient: macronutrient, sanitized_value: sanitized_value, date: formatted_date}
|> save_macro(record)
end
end)
end

Expand Down

0 comments on commit 054f532

Please sign in to comment.