Class: Shinybooru::Booru
- Inherits:
-
Object
- Object
- Shinybooru::Booru
- Defined in:
- lib/shinybooru.rb
Overview
A gem which returns an easy to use object for Gelbooru requests
Instance Attribute Summary collapse
-
#online ⇒ Object
Returns the value of attribute online.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #booru_get(page) ⇒ Object
- #check_connection ⇒ Object
- #errors ⇒ Object
-
#initialize(site = nil) ⇒ Booru
constructor
A new instance of Booru.
- #posts(args = {}) ⇒ Object
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
#online ⇒ Object
Returns the value of attribute online.
8 9 10 |
# File 'lib/shinybooru.rb', line 8 def online @online end |
#url ⇒ Object
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_connection ⇒ Object
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 |
#errors ⇒ Object
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] = 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 = .join('%20') unless .is_a? String req += '&tags=' + end if sfw = '-rating%3aquestionable%20-rating%3explicit' req += if '%20' + else '&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 |