lib/wcc/blogs/post.rb in wcc-blogs-client-0.2.0 vs lib/wcc/blogs/post.rb in wcc-blogs-client-0.3.0
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
module WCC::Blogs
class Post
+ extend WCC::Blogs::Utils
require 'time'
def self.find(slug)
path = '/blog/' + slug.sub(%r{^/}, '') + '.json'
new(JSON.parse(WCC::Blogs.client.get(path))).tap do |blog|
@@ -21,26 +22,25 @@
return unless author
[author.firstName, author.lastName].compact.join(' ')
end
- def date
- publish_at || created_at
- end
-
def html
@html ||=
WCC::Blogs.config.cache.fetch(['BlogPost', fragment_path]) do
WCC::Blogs.client.get(fragment_path, host || WCC::Blogs.config.base_url)
end
end
+ def metadata
+ WCC::Blogs::Metadata.new(raw['metadata']) if raw['metadata']
+ end
+
def published?
return false if publishing_targets.empty?
- publishing_targets.any? { |t| t['key'] == WCC::Blogs.config.publishing_target } &&
- (!publish_at || (publish_at <= Time.now))
+ publishing_targets.any? { |t| t['key'] == WCC::Blogs.config.publishing_target }
end
def time_to_read
# TODO
nil
@@ -48,75 +48,44 @@
def to_param
slug.sub(%r{^/}, '')
end
- def self.camelcase(str)
- str.gsub(/_(\w)/) { Regexp.last_match(1).upcase }
+ define_camelcase_alias(
+ 'id',
+ 'title',
+ 'subtitle',
+ 'slug',
+ 'host',
+ 'path',
+ 'digest',
+ 'fragment_path'
+ ) do |camelcase|
+ raw[camelcase]
end
- %w[
- id
- title
- subtitle
- slug
- host
- path
- digest
- fragment_path
- ].each do |method|
- attribute = camelcase(method)
+ define_camelcase_alias(
+ 'date'
+ ) do |camelcase|
+ value = raw[camelcase]
- define_method method do
- raw[attribute]
- end
+ return unless value && value.length
- alias_method attribute, method if attribute != method
+ Time.parse(value)
end
- %w[
- created_at
- publish_at
- updated_at
- ].each do |method|
- attribute = camelcase(method)
-
- define_method method do
- value = raw[attribute]
- return unless value && value.length
-
- Time.parse(value)
- end
-
- alias_method attribute, method if attribute != method
+ define_camelcase_alias(
+ 'author',
+ 'hero_image',
+ 'thumbnail_image'
+ ) do |camelcase|
+ OpenStruct.new(raw[camelcase]) if raw[camelcase]
end
- %w[
- author
- hero_image
- thumbnail_image
- ].each do |method|
- attribute = camelcase(method)
+ define_camelcase_alias('publishing_targets') do |camelcase|
+ return [] unless raw[camelcase]
- define_method method do
- OpenStruct.new(raw[attribute]) if raw[attribute]
- end
-
- alias_method attribute, method if attribute != method
- end
-
- %w[
- publishing_targets
- ].each do |method|
- attribute = camelcase(method)
-
- define_method method do
- return [] unless raw[attribute]
-
- raw[attribute].map { |val| OpenStruct.new(val) if val }
- end
-
- alias_method attribute, method if attribute != method
+ raw[camelcase].map { |val| OpenStruct.new(val) if val }
end
alias cache_key digest
end
end