Sha256: 4497e80d1c62dd99bdac49771c8766ea42d4e74c67f7da4c70110ef75a7dd529

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require_relative 'lazy_model'
require_relative 'gildable'
require_relative 'inboxable'
require_relative 'moderatable'
require_relative 'postable'
require_relative 'replyable'

require_relative 'listing'
require_relative 'subreddit'
require_relative 'user'

module Redd
  module Models
    # A comment.
    class Comment < LazyModel
      include Gildable
      include Inboxable
      include Moderatable
      include Postable
      include Replyable

      # Create a Comment from its fullname.
      # @param client [APIClient] the api client to initialize the object with
      # @param id [String] the fullname
      # @return [Comment]
      def self.from_id(client, id)
        new(client, name: id)
      end

      private

      def after_initialize
        @attributes[:replies] =
          if @attributes[:replies].is_a?(Hash)
            @client.unmarshal(@attributes[:replies])
          else
            Models::Listing.new(@client, children: [], after: nil, before: nil)
          end
        @attributes[:author] = User.from_id(@client, @attributes.fetch(:author))
        @attributes[:subreddit] = Subreddit.from_id(@client, @attributes.fetch(:subreddit))
      end

      def default_loader
        # Ensure we have the comment's id.
        id = @attributes.fetch(:id) { @attributes.fetch(:name).sub('t1_', '') }

        # If we have the link_id, we can load the listing with replies.
        if @attributes.key?(:link_id)
          link_id = @attributes[:link_id].sub('t3_', '')
          return @client.get("/comments/#{link_id}/_/#{id}").body[1][:data][:children][0][:data]
        end
        # We can only load the comment in isolation if we don't have the link_id.
        @client.get('/api/info', id: "t1_#{id}").body[:data][:children][0][:data]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redd-0.8.4 lib/redd/models/comment.rb