Discover Web App Manifest in HTML

... and return a hash of the JSON data.
This commit is contained in:
2018-06-28 21:11:52 -07:00
parent ee49c0bfaf
commit 193319945a
6 changed files with 132 additions and 15 deletions

View File

@@ -13,8 +13,25 @@ module Manifique
end
def fetch_web_manifest
do_get_request @url
# binding.pry
res = do_get_request @url
links = parse_http_link_header(res)
doc = Nokogiri::HTML(res.body)
manifest_url = discover_web_manifest_url(links, doc)
unless manifest_url.match(/^https?\:\/\//)
# Link is just the manifest path, not an absolute URL
manifest_url = @url + manifest_url
end
res = do_get_request manifest_url
begin
manifest_data = JSON.parse(res.body)
rescue
manifest_data = false
end
manifest_data
end
private
@@ -31,5 +48,20 @@ module Manifique
res
end
end
def parse_http_link_header(response)
link_parser = Nitlink::Parser.new
link_parser.parse(response)
end
def discover_web_manifest_url(links, doc)
# TODO implement/test link header discovery
# if url = links.by_rel('manifest').target.to_s or
if url = doc.at_css("link[rel=manifest]").attributes["href"].value
return url
else
raise "No Web App Manifest found on #{@url}"
end
end
end
end