Sha256: 72de9fa5ddac91e82e2cbaf58c9fb695127ce958af54e921e82d81410c768336

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Reddit
  # A reddit comment.
  class Comment
    attr_reader :body, :name, :ups, :downs, :url, :domain, :author, :id, :created_at, :replies
    
    # Initializes the data for the comment.  Takes a hash of the various attributes as taken from the API.
    def initialize(attributes)
      @score = attributes['score']
      @name = attributes['name']
      @ups = attributes['ups']
      @downs = attributes['downs']
      @author = User.new(attributes['author']) unless attributes['author'].nil?
      @id = attributes['id']
      @created_at = Time.at(attributes['created']) unless attributes['created'].nil?
      @body = attributes['body']
      @replies = []
      
      unless attributes['replies'].nil?
        attributes['replies']['data']['children'].each do |reply|
          @replies << Comment.new(reply['data'])
        end
      end
    end
    
    # Returns the score for this comment.
    def score
      return @ups - @downs
    end

    # returns a number representing how controversial this comment is
    def controversy_score
      (@ups + @downs).to_f / [score.abs, 1].max
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bterlson-reddit-0.3.0 lib/reddit/comment.rb