Sha256: abe31d1ff36d9328f1327b8deb95ca1366021358a81f5399d7cc645aa38b58ff

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

require 'date'
require 'highline'
require 'stringio'
require 'rainbow/ext/string'
require 'usaidwat/ext/string'
require 'usaidwat/ext/time'

Rainbow.enabled = true unless ENV['USAIDWAT_ENV'] == 'cucumber'

module USaidWat
  module CLI
    class CommentFormatter
      def initialize
        @count = 0
      end
      
      def format(comment)
        cols = HighLine::SystemExtensions.terminal_size[0]
        out = StringIO.new
        out.write("\n\n") unless @count == 0
        out.write("#{comment.subreddit}\n".color(:green))
        out.write("#{comment_link(comment)}\n".color(:yellow))
        out.write("#{comment.link_title.strip.truncate(cols)}\n".color(:red))
        out.write("#{comment_date(comment)}\n".color(:blue))
        out.write("\n")
        out.write("#{comment.body.strip.convert_entities}\n")
        @count += 1
        out.rewind
        out.read
      end
      
      private
        def comment_link(comment)
          link = comment.link_id.split("_")[-1]
          "http://www.reddit.com/r/#{comment.subreddit}/comments/#{link}/z/#{comment.id}"
        end

        def comment_date(comment)
          DateTime.strptime(comment.created_utc.to_s, "%s").to_time.localtime.ago
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
usaidwat-1.0.1 lib/usaidwat/formatter.rb
usaidwat-1.0.0 lib/usaidwat/formatter.rb
usaidwat-0.1.7 lib/usaidwat/formatter.rb