Move response types to models
This commit is contained in:
parent
9e96f29852
commit
c954fc1006
5 changed files with 87 additions and 88 deletions
|
@ -7,69 +7,6 @@ class Articles::Show < BrowserAction
|
||||||
else
|
else
|
||||||
response = MediumClient.post_data(post_id)
|
response = MediumClient.post_data(post_id)
|
||||||
end
|
end
|
||||||
html ShowPage, medium_response_body: PostResponse::Root.from_json(response.body)
|
html ShowPage, post_response: response
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,8 @@ require "./medium_client"
|
||||||
# query param and go look for a file with a matching filename.
|
# query param and go look for a file with a matching filename.
|
||||||
|
|
||||||
class LocalClient < MediumClient
|
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")
|
body = File.read("tmp/posts/#{post_id}.json")
|
||||||
HTTP::Client::Response.new(HTTP::Status::OK, body: body)
|
PostResponse::Root.from_json(body)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,12 +4,13 @@ class MediumClient
|
||||||
# https://stackoverflow.com/questions/2669690/
|
# https://stackoverflow.com/questions/2669690/
|
||||||
JSON_HIJACK_STRING = "])}while(1);</x>"
|
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 = 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
|
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)
|
client = HTTP::Client.new("medium.com", tls: true)
|
||||||
response = client.get("/media/#{media_id}", headers: headers)
|
response = client.get("/media/#{media_id}", headers: headers)
|
||||||
body = response.body.sub(JSON_HIJACK_STRING, nil)
|
body = response.body.sub(JSON_HIJACK_STRING, nil)
|
||||||
|
@ -69,22 +70,3 @@ class MediumClient
|
||||||
end
|
end
|
||||||
end
|
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
|
|
||||||
|
|
18
src/models/media_response.cr
Normal file
18
src/models/media_response.cr
Normal 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
|
62
src/models/post_response.cr
Normal file
62
src/models/post_response.cr
Normal 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
|
Loading…
Reference in a new issue