lib/wcc/blogs/post.rb in wcc-blogs-client-0.7.2 vs lib/wcc/blogs/post.rb in wcc-blogs-client-0.7.3
- old
+ new
@@ -1,18 +1,21 @@
# frozen_string_literal: true
+require_relative './concerns/caching'
+
module WCC::Blogs
class Post
extend WCC::Blogs::Utils
+ include WCC::Blogs::Concerns::Caching
require 'time'
- def self.find(slug)
- new(WCC::Blogs.client.blog_show(slug).raw, client: WCC::Blogs.client)
+ def self.find(slug, client: WCC::Blogs.client)
+ new(client.blog_show(slug).raw, client: client)
end
- def self.find_all
- PostSummary.find_all.map(&:full_post)
+ def self.find_all(client: WCC::Blogs.client)
+ PostSummary.find_all(client: client).map(&:full_post)
end
attr_reader :raw
def initialize(raw, client: WCC::Blogs.client)
@@ -24,10 +27,11 @@
author&.fullName
end
def html
@html ||= @client.get(_links.fragment)
+ .assert_ok!
.body
.force_encoding('UTF-8')
end
def metadata
@@ -36,15 +40,10 @@
def published?
true
end
- def time_to_read
- # TODO
- nil
- end
-
def to_param
slug.sub(%r{^/}, '')
end
define_camelcase_alias(
@@ -55,15 +54,18 @@
'slug',
'host',
'path',
'digest',
'fragment_path',
- 'read_time'
+ 'read_time',
+ 'word_count'
) do |camelcase|
raw[camelcase]
end
+ alias time_to_read read_time
+
define_camelcase_alias(
'date',
'updated_at'
) do |camelcase|
value = raw[camelcase]
@@ -101,8 +103,12 @@
define_camelcase_alias('related_posts') do |camelcase|
related = raw[camelcase] || []
related.map { |val| WCC::Blogs::LinkedBlogPostSummary.new(val, client: @client) }
end
- alias cache_key digest
+ private
+
+ def cache_key_without_version
+ "WCC::Blogs::Post/#{slug}"
+ end
end
end