class Itest5ch::Thread

Attributes

board[RW]

@!attribute [rw] board

@return [String]
dat[RW]

@!attribute [rw] dat

@return [Integer]
name[W]

@!attribute [w] name

subdomain[RW]

@!attribute [rw] subdomain

@return [String]

Public Class Methods

new(args) click to toggle source

@overload initialize(subdomain:, board:, dat:, name: nil)

Set attributes

@param subdomain [String]
@param board     [String]
@param dat       [Integer]
@param name      [String]

@example
  thread = Itest5ch::Thread.new(subdomain: "egg", board: "applism", dat: 1234567890)

@overload initialize(url)

Set thread url (PC or SmartPhone)

@param url [String] thread url

@example with SmartPhone url
  thread = Itest5ch::Thread.new("http://itest.5ch.net/egg/test/read.cgi/applism/1234567890")

@example with PC url
  thread = Itest5ch::Thread.new("http://egg.5ch.net/test/read.cgi/applism/1234567890")

@raise [ArgumentError] `arg` is not either Hash and String

# File lib/itest5ch/thread.rb, line 47
def initialize(args)
  case args
  when Hash
    initialize_with_hash(args)
  when String
    initialize_with_string(args)
  else
    raise ArgumentError, "args is either Hash or String is required"
  end
end
normalize_message(message) click to toggle source

@param message [String]

@return [String]

# File lib/itest5ch/thread.rb, line 84
def self.normalize_message(message)
  message = coder.decode(message).scrub("")
  message = CGI.unescapeHTML(message)
  message.gsub(/\s*<br>\s*/i, "\n").strip
end

Private Class Methods

coder() click to toggle source
# File lib/itest5ch/thread.rb, line 90
def self.coder
  @coder ||= HTMLEntities.new
end

Public Instance Methods

==(other) click to toggle source

@param other [Itest5ch::Thread]

@return [Boolean]

# File lib/itest5ch/thread.rb, line 61
def ==(other)
  other.is_a?(Thread) && subdomain == other.subdomain && board == other.board &&
    dat == other.dat && name == other.name
end
comments() click to toggle source

@return [Array<Itest5ch::Comment>]

# File lib/itest5ch/thread.rb, line 67
def comments
  fetch_data["comments"].map do |comment|
    Comment.new(
      number:  comment[0].to_i,
      name:    comment[1],
      mail:    comment[2],
      date:    time_at(comment[3].to_i),
      id:      comment[4],
      message: self.class.normalize_message(comment[6]),
      thread:  self,
    )
  end
end
fetch_name() click to toggle source

@return [String] thread name

# File lib/itest5ch/thread.rb, line 111
def fetch_name
  fetch_data["thread"][5]
end
json_url() click to toggle source

@return [String]

# File lib/itest5ch/thread.rb, line 116
def json_url
  "http://itest.5ch.net/public/newapi/client.php?subdomain=#{subdomain}&board=#{board}&dat=#{dat}&rand=#{rand}"
end
name() click to toggle source

@return [String]

# File lib/itest5ch/thread.rb, line 106
def name
  @name ||= fetch_name
end
pc_url() click to toggle source

@return [String]

# File lib/itest5ch/thread.rb, line 101
def pc_url
  "http://#{subdomain}.5ch.net/test/read.cgi/#{board}/#{dat}"
end
smartphone_url() click to toggle source

@return [String]

# File lib/itest5ch/thread.rb, line 96
def smartphone_url
  "http://itest.5ch.net/#{subdomain}/test/read.cgi/#{board}/#{dat}"
end

Private Instance Methods

fetch_data() click to toggle source

@return [Hash]

# File lib/itest5ch/thread.rb, line 152
def fetch_data
  get_json(json_url, referer: smartphone_url)
end
initialize_with_hash(hash) click to toggle source

@param hash [Hash]

# File lib/itest5ch/thread.rb, line 123
def initialize_with_hash(hash)
  assert_required_keys!(hash, :subdomain, :board, :dat)

  @subdomain = hash[:subdomain]
  @board     = hash[:board]
  @dat       = hash[:dat]
  @name      = hash[:name]
end
initialize_with_string(url) click to toggle source

@param url [String]

# File lib/itest5ch/thread.rb, line 133
def initialize_with_string(url)
  if (m = url.match(%r{https?://itest\.5ch\.net/(.+)/test/read\.cgi/([^/]+)/([0-9]+)}))
    @subdomain = m[1]
    @board     = m[2]
    @dat       = m[3].to_i
    return
  end

  if (m = url.match(%r{https?://(.+)\.5ch\.net/test/read\.cgi/([^/]+)/([0-9]+)}))
    @subdomain = m[1]
    @board     = m[2]
    @dat       = m[3].to_i
    return
  end

  raise ArgumentError, "'#{url}' is invalid url format"
end
rand() click to toggle source

@return [String] random 10 char string

# File lib/itest5ch/thread.rb, line 157
def rand
  SecureRandom.hex(5)
end
time_at(unixtime) click to toggle source

@param unixtime [Integer]

@return [ActiveSupport::TimeWithZone] When `Time.zone` is initialized @return [Time] When `Time.zone` is not initialized or without activesupport

# File lib/itest5ch/thread.rb, line 165
def time_at(unixtime)
  if Time.respond_to?(:zone) && Time.zone.respond_to?(:at)
    Time.zone.at(unixtime)
  else
    Time.at(unixtime)
  end
end