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:
Edward Loveall 2021-08-08 16:37:56 -04:00
parent 09995cde5c
commit e64e9f0853
No known key found for this signature in database
GPG key ID: 789A4AE983AC8901
8 changed files with 4 additions and 86 deletions

View file

@ -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

View file

@ -3,13 +3,6 @@ require "../spec_helper"
include Nodes
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
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
[
@ -289,7 +282,7 @@ describe ParagraphConverter do
"href": null,
"iframe": {
"mediaResource": {
"id": "7c6231d165bf9fc1853f259a7b55bd14"
"href": "https://example.com"
}
},
"layout": null,

View file

@ -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

View file

@ -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

View file

@ -21,9 +21,7 @@ class ParagraphConverter
when PostResponse::ParagraphType::IFRAME
paragraph = paragraphs.shift
if iframe = paragraph.iframe
resolver = IFrameMediaResolver.new(iframe: iframe)
href = resolver.fetch_href
node = IFrame.new(href: href)
node = IFrame.new(href: iframe.mediaResource.href)
else
node = Empty.new
end

View file

@ -7,13 +7,6 @@ class MediumClient
PostResponse::Root.from_json(response.body)
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
HTTP::Headers{
"Accept" => "application/json",
@ -46,7 +39,7 @@ class MediumClient
href
iframe {
mediaResource {
id
href
}
}
metadata {

View file

@ -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

View file

@ -78,7 +78,7 @@ class PostResponse
end
class MediaResource < Base
property id : String
property href : String
end
class Metadata < Base