Class: Fourchanify::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/fourchanify/models/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#boardObject

Returns the value of attribute board.



3
4
5
# File 'lib/fourchanify/models/request.rb', line 3

def board
  @board
end

#image_countObject

Returns the value of attribute image_count.



3
4
5
# File 'lib/fourchanify/models/request.rb', line 3

def image_count
  @image_count
end

#imagesObject

Returns the value of attribute images.



3
4
5
# File 'lib/fourchanify/models/request.rb', line 3

def images
  @images
end

#thread_idObject

Returns the value of attribute thread_id.



3
4
5
# File 'lib/fourchanify/models/request.rb', line 3

def thread_id
  @thread_id
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/fourchanify/models/request.rb', line 3

def url
  @url
end

Class Method Details

.download(url) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/fourchanify/models/request.rb', line 43

def self.download(url)
  request = Request.prepare(url)
  directory_name = request.create_directory
  request.broadcast
  request.images.each do |image|
    image.download(directory_name)
  end
  puts "\n"
end

.get_first_thread_no(board) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/fourchanify/models/request.rb', line 27

def self.get_first_thread_no(board)
  sleep 1
  api_url = "http://api.4chan.org/#{board}/threads.json"
  begin
    json = JSON.parse(open(api_url).read)
    json.first['threads'].first['no']
  rescue
    raise JSONError, "JSON processing failed. Please try again"
  end
end

.get_image_count(board, thread_id) ⇒ Object



38
39
40
41
# File 'lib/fourchanify/models/request.rb', line 38

def self.get_image_count(board, thread_id)
  posts = self.get_posts(board, thread_id)
  posts.first["images"]
end

.get_posts(board, thread_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fourchanify/models/request.rb', line 16

def self.get_posts(board, thread_id)
  sleep 1
  api_url = "http://api.4chan.org/#{board}/res/#{thread_id}.json"
  begin
    json = JSON.parse(open(api_url).read)
    json["posts"]
  rescue
    raise JSONError, "JSON processing failed. Please try again"
  end
end

.prepare(url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fourchanify/models/request.rb', line 5

def self.prepare(url)
  fourchan_url = FourchanUrler::Request.new(url)

  request = Request.new
  request.url = url
  request.board = fourchan_url.board
  request.thread_id = fourchan_url.thread_id
  request.prepare_images
  request
end

Instance Method Details

#broadcastObject



53
54
55
56
# File 'lib/fourchanify/models/request.rb', line 53

def broadcast
  puts "=== Fourchanify ==="
  puts "images: #{self.image_count}"
end

#create_directoryObject



58
59
60
61
62
# File 'lib/fourchanify/models/request.rb', line 58

def create_directory
  directory_name = "#{self.board}_#{self.thread_id}"
  Dir.mkdir("./#{directory_name}")
  directory_name
end

#prepare_imagesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fourchanify/models/request.rb', line 64

def prepare_images
  posts = Request.get_posts(self.board, self.thread_id)
  posts_with_images = posts.select {|p| p["filename"] && p["ext"] }

  # images in replies + the image of OP
  self.image_count = posts.first["images"] + 1
  self.images = []

  posts_with_images.each do |p|
    image = Image.prepare(:filename => p["filename"], :ext => p["ext"],
      :fsize => p["fsize"], :tim => p["tim"], :board => self.board)
    self.images << image
  end
end