# frozen_string_literal: true module WCC::Blogs class Post extend WCC::Blogs::Utils require 'time' def self.find(slug) new(WCC::Blogs.client.blog_show(slug).raw, client: WCC::Blogs.client) end def self.find_all PostSummary.find_all.map(&:full_post) end attr_reader :raw def initialize(raw, client: WCC::Blogs.client) @raw = raw @client = client end def author_full_name return unless author [author.firstName, author.lastName].compact.join(' ') end def html @html ||= @client.get(_links.fragment) .body .force_encoding('UTF-8') end def metadata WCC::Blogs::Metadata.new(raw['metadata']) if raw['metadata'] end def published? true end def time_to_read # TODO nil end def to_param slug.sub(%r{^/}, '') end define_camelcase_alias( 'id', 'title', 'subtitle', 'summary', 'slug', 'host', 'path', 'digest', 'fragment_path' ) do |camelcase| raw[camelcase] end define_camelcase_alias( 'date', 'updated_at' ) do |camelcase| value = raw[camelcase] Time.parse(value) if value && value.length end define_camelcase_alias( 'author', 'hero_image', 'thumbnail_image' ) do |camelcase| OpenStruct.new(raw[camelcase]) if raw[camelcase] end def _links OpenStruct.new(raw['_links'] || {}) end define_camelcase_alias( 'published_properties', 'categories', 'tags' ) do |camelcase| targets = raw[camelcase] || [] targets.map { |val| OpenStruct.new(val) if val } end alias cache_key digest end end