diff --git a/src/actions/articles/show.cr b/src/actions/articles/show.cr index 7f10779..efa10d8 100644 --- a/src/actions/articles/show.cr +++ b/src/actions/articles/show.cr @@ -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 diff --git a/src/actions/clients/local_client.cr b/src/actions/clients/local_client.cr index f9a2463..197abc7 100644 --- a/src/actions/clients/local_client.cr +++ b/src/actions/clients/local_client.cr @@ -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 diff --git a/src/actions/clients/medium_client.cr b/src/actions/clients/medium_client.cr index aee8ad4..4d140e1 100644 --- a/src/actions/clients/medium_client.cr +++ b/src/actions/clients/medium_client.cr @@ -4,12 +4,13 @@ class MediumClient # https://stackoverflow.com/questions/2669690/ JSON_HIJACK_STRING = "])}while(1);" - 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 diff --git a/src/models/media_response.cr b/src/models/media_response.cr new file mode 100644 index 0000000..8ee915d --- /dev/null +++ b/src/models/media_response.cr @@ -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 diff --git a/src/models/post_response.cr b/src/models/post_response.cr new file mode 100644 index 0000000..93c92f5 --- /dev/null +++ b/src/models/post_response.cr @@ -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