Use href from iframe media response
Turns out, href exists in the mediaResponse query. I can use that instead of fetching that separately.
This commit is contained in:
parent
09995cde5c
commit
e64e9f0853
8 changed files with 4 additions and 86 deletions
|
@ -1,27 +0,0 @@
|
||||||
require "../spec_helper"
|
|
||||||
|
|
||||||
include Nodes
|
|
||||||
|
|
||||||
describe IFrameMediaResolver do
|
|
||||||
around_each do |example|
|
|
||||||
original_client = IFrameMediaResolver.http_client
|
|
||||||
IFrameMediaResolver.http_client = FakeMediumClient
|
|
||||||
example.run
|
|
||||||
IFrameMediaResolver.http_client = original_client
|
|
||||||
end
|
|
||||||
|
|
||||||
it "returns a url of the embedded page" do
|
|
||||||
iframe = PostResponse::IFrame.from_json <<-JSON
|
|
||||||
{
|
|
||||||
"mediaResource": {
|
|
||||||
"id": "d4515fff7ecd02786e75fc8997c94bbf"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
JSON
|
|
||||||
resolver = IFrameMediaResolver.new(iframe: iframe)
|
|
||||||
|
|
||||||
result = resolver.fetch_href
|
|
||||||
|
|
||||||
result.should eq("https://example.com")
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -3,13 +3,6 @@ require "../spec_helper"
|
||||||
include Nodes
|
include Nodes
|
||||||
|
|
||||||
describe ParagraphConverter do
|
describe ParagraphConverter do
|
||||||
around_each do |example|
|
|
||||||
original_client = IFrameMediaResolver.http_client
|
|
||||||
IFrameMediaResolver.http_client = FakeMediumClient
|
|
||||||
example.run
|
|
||||||
IFrameMediaResolver.http_client = original_client
|
|
||||||
end
|
|
||||||
|
|
||||||
it "converts a simple structure with no markups" do
|
it "converts a simple structure with no markups" do
|
||||||
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
||||||
[
|
[
|
||||||
|
@ -289,7 +282,7 @@ describe ParagraphConverter do
|
||||||
"href": null,
|
"href": null,
|
||||||
"iframe": {
|
"iframe": {
|
||||||
"mediaResource": {
|
"mediaResource": {
|
||||||
"id": "7c6231d165bf9fc1853f259a7b55bd14"
|
"href": "https://example.com"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"layout": null,
|
"layout": null,
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
class FakeMediumClient < MediumClient
|
|
||||||
def self.media_data(media_id : String) : MediaResponse::Root
|
|
||||||
MediaResponse::Root.from_json(
|
|
||||||
<<-JSON
|
|
||||||
{"payload": {"value": {"href": "https://example.com"}}}
|
|
||||||
JSON
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,13 +0,0 @@
|
||||||
class IFrameMediaResolver
|
|
||||||
class_property http_client : MediumClient.class = MediumClient
|
|
||||||
|
|
||||||
getter iframe
|
|
||||||
|
|
||||||
def initialize(@iframe : PostResponse::IFrame)
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_href
|
|
||||||
response = @@http_client.media_data(iframe.mediaResource.id)
|
|
||||||
response.payload.value.href
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -21,9 +21,7 @@ class ParagraphConverter
|
||||||
when PostResponse::ParagraphType::IFRAME
|
when PostResponse::ParagraphType::IFRAME
|
||||||
paragraph = paragraphs.shift
|
paragraph = paragraphs.shift
|
||||||
if iframe = paragraph.iframe
|
if iframe = paragraph.iframe
|
||||||
resolver = IFrameMediaResolver.new(iframe: iframe)
|
node = IFrame.new(href: iframe.mediaResource.href)
|
||||||
href = resolver.fetch_href
|
|
||||||
node = IFrame.new(href: href)
|
|
||||||
else
|
else
|
||||||
node = Empty.new
|
node = Empty.new
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,13 +7,6 @@ class MediumClient
|
||||||
PostResponse::Root.from_json(response.body)
|
PostResponse::Root.from_json(response.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.media_data(media_id : String) : MediaResponse::Root
|
|
||||||
client = HTTP::Client.new("medium.com", tls: true)
|
|
||||||
response = client.get("/media/#{media_id}", headers: headers)
|
|
||||||
body = response.body.sub(JSON_HIJACK_STRING, nil)
|
|
||||||
MediaResponse::Root.from_json(body)
|
|
||||||
end
|
|
||||||
|
|
||||||
private def self.headers : HTTP::Headers
|
private def self.headers : HTTP::Headers
|
||||||
HTTP::Headers{
|
HTTP::Headers{
|
||||||
"Accept" => "application/json",
|
"Accept" => "application/json",
|
||||||
|
@ -46,7 +39,7 @@ class MediumClient
|
||||||
href
|
href
|
||||||
iframe {
|
iframe {
|
||||||
mediaResource {
|
mediaResource {
|
||||||
id
|
href
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata {
|
metadata {
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
class MediaResponse
|
|
||||||
class Base
|
|
||||||
include JSON::Serializable
|
|
||||||
end
|
|
||||||
|
|
||||||
class Root < Base
|
|
||||||
property payload : Payload
|
|
||||||
end
|
|
||||||
|
|
||||||
class Payload < Base
|
|
||||||
property value : Value
|
|
||||||
end
|
|
||||||
|
|
||||||
class Value < Base
|
|
||||||
property href : String
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -78,7 +78,7 @@ class PostResponse
|
||||||
end
|
end
|
||||||
|
|
||||||
class MediaResource < Base
|
class MediaResource < Base
|
||||||
property id : String
|
property href : String
|
||||||
end
|
end
|
||||||
|
|
||||||
class Metadata < Base
|
class Metadata < Base
|
||||||
|
|
Loading…
Reference in a new issue