Class: Fourchan::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-fourchan.rb

Instance Method Summary collapse

Constructor Details

#initialize(board, thread) ⇒ Post

Returns a new instance of Post.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple-fourchan.rb', line 11

def initialize( board, thread)
  @board = board
  thread = thread.to_i
  @posts = []
  begin
    temp = JSON.parse(open("http://api.4chan.org/#{@board}/res/#{thread}.json").read)["posts"]
      temp.each do |post|
        tim = post["tim"]
        ext = post["ext"]
        post.merge!({"image" => "http://images.4chan.org/#{@board}/src/#{tim}#{ext}"}) unless tim.nil?
        post.merge!({"thumb" => "http://thumbs.4chan.org/#{@board}/thumb/#{tim}s.jpg"}) unless tim.nil?
        post.merge!({"link" => "http://boards.4chan.org/#{@board}/res/#{thread}"})
        @posts << OpenStruct.new(post)
      end
    @posts
  rescue Exception => e
    puts "The thread number #{thread} cannot be fetch. Please try later."
  end
end

Instance Method Details

#allObject



31
32
33
# File 'lib/simple-fourchan.rb', line 31

def all
  @posts
end