From 091686e7a58ee22b7048843a6b84a21defdea7c8 Mon Sep 17 00:00:00 2001 From: Ellis Valentiner Date: Sun, 6 May 2018 15:07:54 -0400 Subject: [PATCH 1/2] Add method to convert DarkSkyResponse to Dict --- src/DarkSky.jl | 23 ++++++++++++++++------- test/runtests.jl | 2 ++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/DarkSky.jl b/src/DarkSky.jl index 9329df1..6a9b1b5 100644 --- a/src/DarkSky.jl +++ b/src/DarkSky.jl @@ -18,6 +18,7 @@ struct DarkSkyResponse latitude::Float64 longitude::Float64 timezone::String + offset::Optional{Int} currently::Optional{Dict} minutely::Optional{Dict} hourly::Optional{Dict} @@ -26,6 +27,8 @@ struct DarkSkyResponse flags::Optional{Dict} end DarkSkyResponse(x::Dict) = DarkSkyResponse((get.(x, String.(fieldnames(DarkSkyResponse)), nothing))...) +Dict(x::DarkSkyResponse) = Dict(String(f) => getfield(x, f) for f in fieldnames(x) if getfield(x, f) != nothing) +Base.convert(Dict, x::DarkSkyResponse) = Dict(x) function Base.show(io::IO, x::DarkSkyResponse) print(io, (x.latitude, x.longitude)) @@ -39,11 +42,16 @@ for fieldname in fieldnames(DarkSkyResponse) end end -function _get_json(url::String, verbose::Bool) +function _get_json(url::String, out_type::String, verbose::Bool) response = HTTP.get(url) verbose ? info(response) : nothing if response.status == 200 - return DarkSkyResponse(JSON.Parser.parse(String(response.body))) + out = JSON.Parser.parse(String(response.body)) + if out_type == "DarkSkyResponse" + return DarkSkyResponse(out) + else + return out + end end end @@ -63,7 +71,7 @@ Make a "Forecast Request", returns the current weather forecast for the next wee """ function forecast(latitude::Float64, longitude::Float64; verbose::Bool=true, exclude::Optional{Array{String}}=nothing, extend::Optional{String}=nothing, - lang::String="en", units::String="us") + lang::String="en", units::String="us", out_type::String="DarkSkyResponse") @argcheck in(lang, SUPPORTED_LANGS) @argcheck in(units, SUPPORTED_UNITS) url = "https://api.darksky.net/forecast/$(ENV["DARKSKY_API_KEY"])/$latitude,$longitude?lang=$lang&units=$units" @@ -73,7 +81,7 @@ function forecast(latitude::Float64, longitude::Float64; verbose::Bool=true, if !(extend === nothing) url = "$url&extend=$extend" end - _get_json(url, verbose) + _get_json(url, out_type, verbose) end """ @@ -92,16 +100,17 @@ the past or future. - `units`: return weather conditions in the requested units (optional). """ function forecast(latitude::Float64, longitude::Float64, time::DateTime; verbose::Bool=true, - exclude::Optional{Array{String}}=nothing, lang::String="en", units::String="us") + exclude::Optional{Array{String}}=nothing, lang::String="en", units::String="us", + out_type::String="DarkSkyResponse") @argcheck in(lang, SUPPORTED_LANGS) @argcheck in(units, SUPPORTED_UNITS) url = "https://api.darksky.net/forecast/$(ENV["DARKSKY_API_KEY"])/$latitude,$longitude,$time?lang=$lang&units=$units" if !(exclude === nothing) url = "$url&exclude=$(join(exclude, ","))" end - _get_json(url, verbose) + _get_json(url, out_type, verbose) end -export forecast +export forecast, DarkSkyResponse end # module diff --git a/test/runtests.jl b/test/runtests.jl index 6235e37..4b32d8c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,8 @@ if haskey(ENV, "DARKSKY_API_KEY") @test isa(alerts(response), Array{Any, 1}) | isa(alerts(response), Void) @test isa(flags(response), Dict{String, Any}) @test isa(print(response), Void) + dict_response = forecast(42.3601, -71.0589, DateTime(2018, 3, 7, 0, 0, 0), out_type=""); + @test Dict(response) == dict_response @test isa(forecast(42.3601, -71.0589, DateTime(2018, 1, 1, 0, 0, 0), lang="es", units="si", exclude=["minutely"]), DarkSky.DarkSkyResponse) else warn("Dark Sky API key was not found in your system environment variables, skipping tests...") From 025e151a1367437dec84334b89fa498a30541af2 Mon Sep 17 00:00:00 2001 From: Ellis Valentiner Date: Mon, 7 May 2018 15:58:34 -0400 Subject: [PATCH 2/2] Increase test coverage --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 4b32d8c..9997970 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,6 +18,7 @@ if haskey(ENV, "DARKSKY_API_KEY") @test isa(print(response), Void) dict_response = forecast(42.3601, -71.0589, DateTime(2018, 3, 7, 0, 0, 0), out_type=""); @test Dict(response) == dict_response + @test Dict(response) == convert(Dict, response) @test isa(forecast(42.3601, -71.0589, DateTime(2018, 1, 1, 0, 0, 0), lang="es", units="si", exclude=["minutely"]), DarkSky.DarkSkyResponse) else warn("Dark Sky API key was not found in your system environment variables, skipping tests...")