Sha256: 7bf141408aec76cba6b028e1b0a285593d0b1f52c8463f02798fcfa1463efa19

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module Redd
  module Models
    # Methods for user-submitted content, i.e. Submissions and Comments.
    module Postable
      # Edit a thing.
      # @param text [String] The new text.
      # @return [self] the edited thing
      def edit(text)
        @client.post('/api/editusertext', thing_id: get_attribute(:name), text: text)
        @attributes[is_a?(Submission) ? :selftext : :body] = text
        self
      end

      # Delete the thing.
      def delete
        @client.post('/api/del', id: get_attribute(:name))
      end

      # Save a link or comment to the user's account.
      # @param category [String] a category to save to
      def save(category = nil)
        params = { id: fullname }
        params[:category] = category if category
        @client.post('/api/save', params)
      end

      # Remove the link or comment from the user's saved links.
      def unsave
        @client.post('/api/unsave', id: get_attribute(:name))
      end

      # Hide a link from the user.
      def hide
        @client.post('/api/hide', id: get_attribute(:fullname))
      end

      # Unhide a previously hidden link.
      def unhide
        @client.post('/api/unhide', id: get_attribute(:fullname))
      end

      # Upvote the model.
      def upvote
        vote(1)
      end

      # Downvote the model.
      def downvote
        vote(-1)
      end

      # Clear any upvotes or downvotes on the model.
      def undo_vote
        vote(0)
      end

      private

      # Send a vote.
      # @param direction [-1, 0, 1] the direction to vote in
      def vote(direction)
        fullname = get_attribute(:name)
        @client.post('/api/vote', id: fullname, dir: direction)
        @attributes[:ups] += direction
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redd-0.8.0.pre.2 lib/redd/models/postable.rb
redd-0.8.0.pre.1 lib/redd/models/postable.rb