sue445/itest5ch

View on GitHub
lib/itest5ch/comment.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
module Itest5ch
  require "base64"

  class Comment
    # @!attribute number
    #   @return [Integer]
    attr_accessor :number

    # @!attribute name
    #   @return [String]
    attr_accessor :name

    # @!attribute mail
    #   @return [String]
    attr_accessor :mail

    # @!attribute date
    #   @return [Time]
    attr_accessor :date

    # @!attribute id
    #   @return [String]
    attr_accessor :id

    # @!attribute message
    #   @return [String]
    attr_accessor :message

    # @!attribute thread
    #   @return [Itest5ch::Thread]
    attr_accessor :thread

    # @param number  [Integer]
    # @param name    [String]
    # @param mail    [String]
    # @param date    [Time]
    # @param id      [String]
    # @param message [String]
    # @param thread  [Itest5ch::Thread]
    def initialize(number:, name:, mail:, date:, id:, message:, thread:) # rubocop:disable Metrics/ParameterLists
      @number  = number
      @name    = name
      @mail    = mail
      @date    = date
      @id      = id
      @message = message
      @thread  = thread
    end

    # @return [Array<Integer>]
    def anchor_numbers
      numbers =
        message.scan(/[>>]+([0-90-9\-]+)/).map do |result|
          str = result.first.tr("0-9", "0-9")
          if (m = str.match(/([0-9]+)-([0-9]+)/))
            (m[1].to_i..m[2].to_i).to_a
          else
            str.to_i
          end
        end

      numbers.flatten.compact
    end

    alias_method :reply_numbers, :anchor_numbers

    # @return [String]
    def pc_url
      "#{thread.pc_url}/#{number}"
    end

    # @return [String]
    def smartphone_url
      "#{thread.smartphone_url}/#{number}"
    end

    # Get Id checker url for http://hissi.org
    #
    # @return [String]
    # @return [nil] {#id} is empty
    #
    # @see http://hissi.org
    def id_checker_url
      return nil if !id || id.empty?

      ymd = date.strftime("%Y%m%d")
      encoded_id = Base64.strict_encode64(id).delete("=")
      "http://hissi.org/read.php/#{thread.board}/#{ymd}/#{encoded_id}.html"
    end
  end
end