Sha256: 4781883ee9bd3f287c6b83946f6352b7d4bb7957024970cfb102147429f39c52

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require_relative 'lazy_model'

module Redd
  module Models
    # A reddit user.
    class WikiPage < LazyModel
      # Edit the wiki page.
      # @param content [String] the new wiki page contents
      # @param reason [String, nil] an optional reason for editing the page
      def edit(content, reason: nil)
        params = { page: @attributes.fetch(:title), content: content }
        params[:reason] = reason if reason
        @client.post("/r/#{@attributes.fetch(:subreddit).display_name}/api/wiki/edit", params)
      end

      private

      def default_loader
        title = @attributes.fetch(:title)
        if @attributes.key?(:subreddit)
          sr_name = @attributes[:subreddit].display_name
          return @client.get("/r/#{sr_name}/wiki/#{title}").body[:data]
        end
        @client.get("/wiki/#{title}").body[:data]
      end

      def after_initialize
        return unless @attributes[:revision_by]
        @attributes[:revision_by] = @client.unmarshal(@attributes[:revision_by])
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redd-0.8.8 lib/redd/models/wiki_page.rb
redd-0.8.7 lib/redd/models/wiki_page.rb
redd-0.8.6 lib/redd/models/wiki_page.rb
redd-0.8.5 lib/redd/models/wiki_page.rb