Class: Trefoil::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/trefoil/post.rb

Overview

Represents a ‘post` object. Post object data varies greatly based on board, content, and whether it is an OP. Possible fields are available for viewing in the (API documentation)

Instance Method Summary collapse

Constructor Details

#initialize(client, board, thread, data) ⇒ Post

Intended for internal use



9
10
11
12
13
14
# File 'lib/trefoil/post.rb', line 9

def initialize(client, board, thread, data)
  @client = client
  @board = board
  @thread = thread
  @data = data
end

Instance Method Details

#[](key) ⇒ Hash, ...

Access underlying post data with symbolized names Possible fields are available for viewing in the (API documentation)

Parameters:

  • key (Symbol)

    The API field

Returns:

  • (Hash, Array, String, Fixnum)


20
21
22
# File 'lib/trefoil/post.rb', line 20

def [](key)
  @data[key]
end

#image?true, false

Whether this post has an image associated with it

Returns:

  • (true, false)


32
33
34
# File 'lib/trefoil/post.rb', line 32

def image?
  !@data[:filename].is_nil?
end

#image_urlImage?

Url to the image of this post, if any.

Returns:

  • (Image, nil)

    An Image or nil if no image is present.



38
39
40
41
42
# File 'lib/trefoil/post.rb', line 38

def image_url
  return nil unless image?

  Image.new(@data)
end

#opPost

The OP to the thread this post is in.

Returns:

  • (Post)

    A post object for the OP, the instance it was called on if it is the OP.



58
59
60
61
62
63
64
# File 'lib/trefoil/post.rb', line 58

def op
  if @data[:resto].zero
    self
  else
    thread.posts[0]
  end
end

#op?true, false

Whether this is the first post in a thread. All op’s has a resto of 0

Returns:

  • (true, false)


26
27
28
# File 'lib/trefoil/post.rb', line 26

def op?
  @data[:resto].zero?
end

#urlString

Url to this post

Returns:

  • (String)

    The url to this post.



46
47
48
49
50
51
52
53
54
# File 'lib/trefoil/post.rb', line 46

def url
  direct_link = if @data[:resto].zero?
                  @data[:no]
                else
                  "#{@data[:resto]}p##{@data[:no]}"
                end

  "http://boards.4chan.org/#{@board}/thread/#{direct_link}"
end