Move response types to models

This commit is contained in:
Edward Loveall 2021-05-15 17:05:28 -04:00
parent 9e96f29852
commit c954fc1006
No known key found for this signature in database
GPG key ID: 789A4AE983AC8901
5 changed files with 87 additions and 88 deletions

View file

@ -7,69 +7,6 @@ class Articles::Show < BrowserAction
else
response = MediumClient.post_data(post_id)
end
html ShowPage, medium_response_body: PostResponse::Root.from_json(response.body)
end
end
class PostResponse
class Base
include JSON::Serializable
end
class Root < Base
property data : Data
end
class Data < Base
property post : Post
end
class Post < Base
property title : String
property creator : Creator
property content : Content
end
class Creator < Base
property name : String
property id : String
end
class Content < Base
property bodyModel : BodyModel
end
class BodyModel < Base
property paragraphs : Array(Paragraph)
end
class Paragraph < Base
property text : String
property type : ParagraphType
property iframe : IFrame?
property layout : String?
end
enum ParagraphType
H3
H4
P
PRE
BQ
ULI
OLI
IFRAME
IMG
end
class IFrame < Base
property mediaResource : MediaResource
end
class MediaResource < Base
property id : String
end
class Metadata < Base
html ShowPage, post_response: response
end
end

View file

@ -13,8 +13,8 @@ require "./medium_client"
# query param and go look for a file with a matching filename.
class LocalClient < MediumClient
def self.post_data(post_id : String) : HTTP::Client::Response
def self.post_data(post_id : String) : PostResponse::Root
body = File.read("tmp/posts/#{post_id}.json")
HTTP::Client::Response.new(HTTP::Status::OK, body: body)
PostResponse::Root.from_json(body)
end
end

View file

@ -4,12 +4,13 @@ class MediumClient
# https://stackoverflow.com/questions/2669690/
JSON_HIJACK_STRING = "])}while(1);</x>"
def self.post_data(post_id : String) : HTTP::Client::Response
def self.post_data(post_id : String) : PostResponse::Root
client = HTTP::Client.new("medium.com", tls: true)
client.post("/_/graphql", headers: headers, body: body(post_id))
response = client.post("/_/graphql", headers: headers, body: body(post_id))
PostResponse::Root.from_json(response.body)
end
def self.embed_data(media_id : String) : MediaResponse::Root
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)
@ -69,22 +70,3 @@ class MediumClient
end
end
end
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
property iframeSrc : String
end
end

View file

@ -0,0 +1,18 @@
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
property iframeSrc : String
end
end

View file

@ -0,0 +1,62 @@
class PostResponse
class Base
include JSON::Serializable
end
class Root < Base
property data : Data
end
class Data < Base
property post : Post
end
class Post < Base
property title : String
property creator : Creator
property content : Content
end
class Creator < Base
property name : String
property id : String
end
class Content < Base
property bodyModel : BodyModel
end
class BodyModel < Base
property paragraphs : Array(Paragraph)
end
class Paragraph < Base
property text : String
property type : ParagraphType
property iframe : IFrame?
property layout : String?
end
enum ParagraphType
H3
H4
P
PRE
BQ
ULI
OLI
IFRAME
IMG
end
class IFrame < Base
property mediaResource : MediaResource
end
class MediaResource < Base
property id : String
end
class Metadata < Base
end
end