scribe/src/classes/page_converter.cr

38 lines
1.3 KiB
Crystal

class PageConverter
def convert(data : PostResponse::Data) : Page
title, content = title_and_content(data)
author = data.post.creator
created_at = Time.unix_ms(data.post.createdAt)
gist_store = gist_store(content)
Page.new(
title: title,
author: author,
created_at: Time.unix_ms(data.post.createdAt),
nodes: ParagraphConverter.new.convert(content, gist_store)
)
end
def title_and_content(data : PostResponse::Data) : {String, Array(PostResponse::Paragraph)}
title = data.post.title
paragraphs = data.post.content.bodyModel.paragraphs
non_content_paragraphs = paragraphs.reject { |para| para.text == title }
{title, non_content_paragraphs}
end
private def gist_store(paragraphs) : GistStore | RateLimitedGistStore
store = GistStore.new
gist_urls = GistScanner.new(paragraphs).scan
gist_responses = gist_urls.map do |url|
params = GistParams.extract_from_url(url)
response = GithubClient.get_gist_response(params.id)
if response.is_a?(GithubClient::RateLimitedResponse)
return RateLimitedGistStore.new
end
JSON.parse(response.data.body)["files"].as_h.values.map do |json_any|
store.store_gist_file(params.id, GistFile.from_json(json_any.to_json))
end
end
store
end
end