Class: Shinybooru::Booru

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

Overview

A gem which returns an easy to use object for Gelbooru requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site = nil) ⇒ Booru

Returns a new instance of Booru.


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/shinybooru.rb', line 10

def initialize(site = nil)
  good_sites = %w(gelbooru.com safebooru.org)
  # Default to safebooru
  @url = if good_sites.include? site
           site
         else
           'safebooru.org'
         end
  @booru = HTTP::Requestor.new @url
  check_connection
end

Instance Attribute Details

#onlineObject

Returns the value of attribute online.


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

def online
  @online
end

#urlObject

Returns the value of attribute url.


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

def url
  @url
end

Instance Method Details

#booru_get(page) ⇒ Object


35
36
37
# File 'lib/shinybooru.rb', line 35

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

#check_connectionObject


22
23
24
25
26
27
28
29
# File 'lib/shinybooru.rb', line 22

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

#errorsObject


31
32
33
# File 'lib/shinybooru.rb', line 31

def errors
  !@online
end

#posts(args = {}) ⇒ Object


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
70
71
72
73
74
75
76
77
78
# File 'lib/shinybooru.rb', line 39

def posts(args = {})
  limit = args[:limit].nil? ? 10 : args[:limit]
  tags = args[:tags].nil? ? [] : args[:tags]
  sfw = args[:sfw] == true
  # Always sfw if safebooru, so no need for rating tags
  sfw = false if @url == 'safebooru.org'

  raise Shinybooru::OfflineError unless @online

  req = '&limit=' + limit.to_s

  if tags
    tags = tags.join('%20') unless tags.is_a? String
    req += '&tags=' + tags
  end

  if sfw
    explicit_tags = '-rating%3aquestionable%20-rating%3explicit'
    req += if tags
             '%20' + explicit_tags
           else
             '&tags=' + 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
end