class Wikiquote
Public Class Methods
getQuotesForSection(page_id, section_id)
click to toggle source
getQuotesForSection (Fixnum page_id, Fixnum section_id) Get all quotes from given section id Return an array of string. Each string is a quote
# File lib/wikiquote-api.rb, line 58 def self.getQuotesForSection(page_id, section_id) uri = URI(@@url) params = { format: "json", action: "parse", pageid: page_id, noimages: "", section: section_id } uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) ret = [] if res.is_a?(Net::HTTPSuccess) arr = JSON.parse(res.body) unless arr["error"] page = Nokogiri::HTML(arr["parse"]["text"]["*"]) arr1 = page.css("ul li") arr2 = page.css("ul li ul li") short = arr1 + arr2 - (arr1 & arr2) long = arr1 + arr2 ret = short.collect{ |l| l.text } end else # puts "Request didn't succeed" end ret end
getRandomQuote(title)
click to toggle source
getRandomQuote (String CelebrityName) Get a random quote from give celebrity name Return a string which is the randomly picked quote
# File lib/wikiquote-api.rb, line 85 def self.getRandomQuote(title) res = "" begin page_id = self.getTitle(title) unless page_id == -1 quotes = self.getQuotesForSection(page_id, 1) res = quotes[rand(quotes.length)] end rescue end res end
getSectionsForPage(page_id)
click to toggle source
getSectionForPage (Fixnum page_id) Get the sections for the given page id Return an hash like : {section_id: sectionName, …} or an empty hash if an error occurs
# File lib/wikiquote-api.rb, line 33 def self.getSectionsForPage(page_id) uri = URI(@@url) params = { format: "json", action: "parse", pageid: page_id, prop: "sections" } uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) hash = {} if res.is_a?(Net::HTTPSuccess) unless page_id == -1 arr = JSON.parse(res.body)["parse"]["sections"] arr.each do |elem| hash[elem["number"]] = elem["anchor"] end end else # puts "Request didn't succeed" end hash end
getTitle(title)
click to toggle source
getTitle (String CelebrityName) Get page id of a page which correspond to argv title. Return a positive int or -1 if an error occurs
# File lib/wikiquote-api.rb, line 13 def self.getTitle(title) uri = URI(@@url) new_title = title.split(' ').collect{ |elem| elem.capitalize }.join(' ') params = { format: "json", action: "query", titles: new_title } uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) if res.is_a?(Net::HTTPSuccess) res = JSON.parse(res.body)["query"]["pages"].first[0].to_i else # puts "Request didn't succeed" res = -1 end res end
resetUrl()
click to toggle source
resetUrl Used after a language change to reset url
# File lib/wikiquote-api.rb, line 103 def self.resetUrl() @@url = "https://#{@@lang}.wikiquote.org/w/api.php" end
setLang(lang)
click to toggle source
setLang Change Wikiquote
language Return true if succeed or false if error
# File lib/wikiquote-api.rb, line 111 def self.setLang(lang) success = true if lang.length == 2 begin url = URI.parse("https://#{lang}.wikiquote.org/") req = Net::HTTP.new(url.host, url.port) res = req.request_head(url.path) success = false unless res.code != "404" rescue success = false end if success @@lang = lang else puts "This lang is not supported" end else puts "The lang in parameter must be a two letters string" end self.resetUrl() success end