danbooru::http: parse html responses.
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
require "danbooru/http/html_adapter"
|
||||||
|
require "danbooru/http/xml_adapter"
|
||||||
|
|
||||||
module Danbooru
|
module Danbooru
|
||||||
class Http
|
class Http
|
||||||
class DownloadError < StandardError; end
|
class DownloadError < StandardError; end
|
||||||
|
|||||||
12
app/logical/danbooru/http/html_adapter.rb
Normal file
12
app/logical/danbooru/http/html_adapter.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module Danbooru
|
||||||
|
class Http
|
||||||
|
class HtmlAdapter < HTTP::MimeType::Adapter
|
||||||
|
HTTP::MimeType.register_adapter "text/html", self
|
||||||
|
HTTP::MimeType.register_alias "text/html", :html
|
||||||
|
|
||||||
|
def decode(str)
|
||||||
|
Nokogiri::HTML5(str)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
12
app/logical/danbooru/http/xml_adapter.rb
Normal file
12
app/logical/danbooru/http/xml_adapter.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
module Danbooru
|
||||||
|
class Http
|
||||||
|
class XmlAdapter < HTTP::MimeType::Adapter
|
||||||
|
HTTP::MimeType.register_adapter "application/xml", self
|
||||||
|
HTTP::MimeType.register_alias "application/xml", :xml
|
||||||
|
|
||||||
|
def decode(str)
|
||||||
|
Hash.from_xml(str).with_indifferent_access
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -38,6 +38,19 @@ class DanbooruHttpTest < ActiveSupport::TestCase
|
|||||||
assert_equal(true, response.parse["gzipped"])
|
assert_equal(true, response.parse["gzipped"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "automatically parse html responses" do
|
||||||
|
response = Danbooru::Http.get("https://httpbin.org/html")
|
||||||
|
assert_equal(200, response.status)
|
||||||
|
assert_instance_of(Nokogiri::HTML5::Document, response.parse)
|
||||||
|
assert_equal("Herman Melville - Moby-Dick", response.parse.css("h1").text)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "automatically parse xml responses" do
|
||||||
|
response = Danbooru::Http.get("https://httpbin.org/xml")
|
||||||
|
assert_equal(200, response.status)
|
||||||
|
assert_equal(true, response.parse[:slideshow].present?)
|
||||||
|
end
|
||||||
|
|
||||||
should "cache requests" do
|
should "cache requests" do
|
||||||
response1 = Danbooru::Http.cache(1.minute).get("https://httpbin.org/uuid")
|
response1 = Danbooru::Http.cache(1.minute).get("https://httpbin.org/uuid")
|
||||||
assert_equal(200, response1.status)
|
assert_equal(200, response1.status)
|
||||||
|
|||||||
Reference in New Issue
Block a user