Class: Shinybooru::Booru

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBooru

Returns a new instance of Booru.



9
10
11
12
# File 'lib/shinybooru.rb', line 9

def initialize
  @booru = HTTP::Requestor.new "gelbooru.com"
  checkConnection
end

Instance Attribute Details

#onlineObject

Returns the value of attribute online.



7
8
9
# File 'lib/shinybooru.rb', line 7

def online
  @online
end

Instance Method Details

#booru_get(page) ⇒ Object



27
28
29
# File 'lib/shinybooru.rb', line 27

def booru_get (page)
  @booru.get '/index.php?page=dapi&s=post&q=index' + page
end

#checkConnectionObject



14
15
16
17
18
19
20
21
# File 'lib/shinybooru.rb', line 14

def checkConnection
  conn = @booru.get 'index.php'
  if conn
    @online = true
  end
rescue TimeoutError
  @online = false
end

#errorsObject



23
24
25
# File 'lib/shinybooru.rb', line 23

def errors
  !@online
end

#posts(args = Hash.new) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/shinybooru.rb', line 31

def posts (args=Hash.new)
  limit = args[:limit]
  limit = 1 if limit.nil?
  nsfw = args[:nsfw]
  nsfw = true if nsfw.nil?
  tags = args[:tags]
  tags = [] if tags.nil?
  if @online
    req = '&limit=' + limit.to_s
    if tags
      unless tags.is_a? String
        tags = tags.join('%20')
      end
      req += '&tags=' + tags
    end
    unless nsfw
      explicit_tags = '-rating%3aquestionable%20-rating%3explicit'
      unless tags
        req += '&tags=' + explicit_tags
      else
        req += '%20' + explicit_tags
      end
    end
    data = Nokogiri::Slop((booru_get req).body)
    posts = []
    data.posts.children.each do |post|
      if post.is_a? Nokogiri::XML::Element
        posts.push Shinybooru::Post.new(post)
      end
    end
    if posts.length > 1
      posts
    else
      posts[0]
    end
  else
    raise Shinybooru::OfflineError
  end
end