Class: Itest5ch::Thread

Inherits:
Object
  • Object
show all
Includes:
AssertMethods, HttpMethods
Defined in:
lib/itest5ch/thread.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AssertMethods

#assert_required!, #assert_required_keys!

Methods included from HttpMethods

#get_html, #get_json

Constructor Details

#initialize(subdomain: , board: , dat: , name: nil) ⇒ Thread #initialize(url) ⇒ Thread

Returns a new instance of Thread.

Overloads:

  • #initialize(subdomain: , board: , dat: , name: nil) ⇒ Thread

    Set attributes

    Examples:

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

    Parameters:

    • subdomain (String) (defaults to: )
    • board (String) (defaults to: )
    • dat (Integer) (defaults to: )
    • name (String) (defaults to: nil)
  • #initialize(url) ⇒ Thread

    Set thread url (PC or SmartPhone)

    Examples:

    with SmartPhone url

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

    with PC url

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

    Parameters:

    • url (String)

      thread url

Raises:

  • (ArgumentError)

    arg is not either Hash and String



47
48
49
50
51
52
53
54
55
56
# 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

Instance Attribute Details

#boardString

Returns:

  • (String)


15
16
17
# File 'lib/itest5ch/thread.rb', line 15

def board
  @board
end

#datInteger

Returns:

  • (Integer)


19
20
21
# File 'lib/itest5ch/thread.rb', line 19

def dat
  @dat
end

#nameString

Returns:

  • (String)


106
107
108
# File 'lib/itest5ch/thread.rb', line 106

def name
  @name ||= fetch_name
end

#subdomainString

Returns:

  • (String)


11
12
13
# File 'lib/itest5ch/thread.rb', line 11

def subdomain
  @subdomain
end

Class Method Details

.normalize_message(message) ⇒ String

Parameters:

  • message (String)

Returns:

  • (String)


84
85
86
87
88
# 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

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


61
62
63
64
# 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

#commentsArray<Itest5ch::Comment>

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
# 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_nameString

Returns thread name.

Returns:

  • (String)

    thread name



111
112
113
# File 'lib/itest5ch/thread.rb', line 111

def fetch_name
  fetch_data["thread"][5]
end

#json_urlString

Returns:

  • (String)


116
117
118
# 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

#pc_urlString

Returns:

  • (String)


101
102
103
# File 'lib/itest5ch/thread.rb', line 101

def pc_url
  "http://#{subdomain}.5ch.net/test/read.cgi/#{board}/#{dat}"
end

#smartphone_urlString

Returns:

  • (String)


96
97
98
# File 'lib/itest5ch/thread.rb', line 96

def smartphone_url
  "http://itest.5ch.net/#{subdomain}/test/read.cgi/#{board}/#{dat}"
end