module ANN_Wrapper
wrapper class for ANN
API
Constants
- ANN_API_URL
- ANN_REPORTS_URL
- ANN_URL
ANN
API anime url
Public Instance Methods
batch_items(ids, api_url=ANN_API_URL)
click to toggle source
fetch up to 50 items(Animes or Mangas) in one request
# File lib/ann_wrapper.rb, line 23 def batch_items(ids, api_url=ANN_API_URL) # append id to API url and send request url = "#{api_url}?title=#{ids.first.to_s}" ids[1..-1].each do |id| url << "/#{id.to_s}" end ann = fetch(url) return [ann] if ann.is_a?(ANN_Error) all_items = ann.xpath("//ann/#{@@type}") warnings = ann.xpath('//ann/warning') return [ANN_Error.new(get_xml_error(ann))] if all_items.empty? and warnings.empty? all_items = all_items.map { |item| Object.const_get("ANN_#{@@type.capitalize}").new(item) } warnings = warnings.map { |warning| ANN_Error.new(get_xml_error(warning)) } all_items.push(*warnings) end
fetch_item(id, api_url=ANN_API_URL)
click to toggle source
fetch anime and convert to ANN_Anime
# File lib/ann_wrapper.rb, line 46 def fetch_item(id, api_url=ANN_API_URL) batch_items([id], api_url).first end
fetch_titles(options = {})
click to toggle source
fetch list of titles via reports
# File lib/ann_wrapper.rb, line 51 def fetch_titles(options = {}) options[:type] ||= "anime" options[:nskip] ||= 0 options[:nlist] ||= 50 options[:name] ||= "" options[:api_url] ||= ANN_REPORTS_URL url = "#{options[:api_url]}?id=155&type=#{options[:type]}&name=#{options[:name]}&nskip=#{options[:nskip]}&nlist=#{options[:nlist]}" report = fetch(url) return report if report.is_a?(ANN_Error) reports = report.xpath('//report/item') return ANN_Error.new(get_xml_error(report)) if reports.nil? reports.map { |item| ANN_Report.new(item) } end
method_missing(meth, *args, &block)
click to toggle source
Calls superclass method
# File lib/ann_wrapper.rb, line 71 def method_missing(meth, *args, &block) if meth.to_s =~ /^(fetch|batch)_(anime|manga)$/ @@type = $2 $1 == 'fetch' ? fetch_item(*args) : batch_items(*args) else super end end
Private Instance Methods
fetch(url)
click to toggle source
fetch data from ANN
API via http GET request returns Nokogiri or ANN_Error
# File lib/ann_wrapper.rb, line 83 def fetch(url) begin # send http GET request with uri parsed from url resp = Net::HTTP.get_response(URI.parse(url)) # get the response body and try converting to Nokogiri object Nokogiri.XML(resp.body) rescue ANN_Error.new("Could not reach valid URL") end end
get_xml_error(xobj)
click to toggle source
attempt to grab error message from XMLObject
# File lib/ann_wrapper.rb, line 96 def get_xml_error(xobj) begin xobj.at_xpath('//ann/warning').content rescue NoMethodError "unrecognized response body" end end