Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Commit

Permalink
Add method to convert DarkSkyResponse to Dict (#9)
Browse files Browse the repository at this point in the history
* Add method to convert DarkSkyResponse to Dict
  • Loading branch information
ellisvalentiner committed May 16, 2018
1 parent 052eda8 commit 7b9b6d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/DarkSky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct DarkSkyResponse
latitude::Float64
longitude::Float64
timezone::String
offset::Optional{Int}
currently::Optional{Dict}
minutely::Optional{Dict}
hourly::Optional{Dict}
Expand All @@ -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))
Expand All @@ -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

Expand All @@ -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"
Expand All @@ -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

"""
Expand All @@ -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
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ 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 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...")
Expand Down

0 comments on commit 7b9b6d8

Please sign in to comment.