From 5fa844811dbac574a49fdf0f32135eb2250b89ce Mon Sep 17 00:00:00 2001 From: joshday Date: Sun, 5 Jan 2020 08:59:53 -0500 Subject: [PATCH] major simplification --- README.md | 26 ++++---------------------- src/XKCD.jl | 23 ++++------------------- test/runtests.jl | 4 ++-- 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 73b5756..cb89562 100644 --- a/README.md +++ b/README.md @@ -2,30 +2,16 @@ # XKCD -A Julia package for retrieving the image (and importantly, the hovertext) for the XKCD webcomic: - -[xkcd.com](https://xkcd.com) - -## Details - -This package uses the function `comic(id=nothing; open=true)` to create: - -```julia -struct XKCDComic - id::Int - title::String - img::String - hover::String -end -``` - +A Julia package for retrieving the image (and importantly, the hovertext) for the XKCD webcomic: [xkcd.com](https://xkcd.com). ## Usage +The `comic` function retrieves the comic's data and optionally opens the image in a browser. + ```julia using XKCD -XKCD.comicdata(552) +XKCD.comic(552; open=false) # JSON3.Object{Base.CodeUnits{UInt8,JSON3.VectorString{Array{UInt8,1}}},Array{UInt64,1}} with 11 entries: # :month => "3" # :num => 552 @@ -38,8 +24,4 @@ XKCD.comicdata(552) # :img => "https://imgs.xkcd.com/comics/correlation.png" # :title => "Correlation" # :day => "6" - -c = XKCD.comic(552) -# XKCDComic (1, "Correlation", https://imgs.xkcd.com/comics/correlation.png) -# Correlation doesn't imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing 'look over there'. ``` \ No newline at end of file diff --git a/src/XKCD.jl b/src/XKCD.jl index e516315..2e6ddbb 100644 --- a/src/XKCD.jl +++ b/src/XKCD.jl @@ -2,40 +2,25 @@ module XKCD using HTTP, JSON3 +#-----------------------------------------------------------------------------# comicdata function comicdata(i::Union{Nothing, Integer} = nothing) part = isnothing(i) ? "" : "/$i" JSON3.read(HTTP.get("https://xkcd.com$part/info.0.json").body) end -#-----------------------------------------------------------------------------# XKCDComic -struct XKCDComic - id::Int - title::String - img::String - hover::String -end - -Base.Dict(x::XKCDComic) = Dict(:id=>x.id, :title=>x.title, :img=>x.img, :hover=>x.hover) - -function Base.show(io::IO, x::XKCDComic) - println(io, "XKCDComic ($(x.id), \"$(x.title)\", $(x.img))") - printstyled(io, " " * x.hover, color=:light_black) -end - #-----------------------------------------------------------------------------# comic """ comic(i=nothing; open=true) -Get comic number `i` (most recent if `nothing`) and optionally open the image in browser. Returns -an `XKCDComic` struct with fields `id`, `title`, `img`, and `hover`. +Get comic number `i` (most recent if `nothing`) and optionally open the image in browser. """ function comic(i::Union{Nothing, Int} = nothing; open=true) data = comicdata(i) - img, hover, title, id = data.img, data.alt, data.safe_title, data.num + img = data.img open && Sys.isapple() && run(`open $img`) open && Sys.islinux() && run(`xdg-open $img`) open && Sys.iswindows() &&run(`start $img`) - XKCDComic(id, title, img, hover) + data end end # module diff --git a/test/runtests.jl b/test/runtests.jl index a61cd5c..6201525 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,10 +3,10 @@ using Test @testset "XKCD.jl" begin c = XKCD.comic(open=false) - @test c.id >= 2250 + @test c.num >= 2250 c = XKCD.comic(999, open=false) - @test c.id == 999 + @test c.num == 999 @test c.title == "Cougars" @test c.img == "https://imgs.xkcd.com/comics/cougars.png" end