Module: ANN_Wrapper

Extended by:
ANN_Wrapper
Included in:
ANN_Wrapper
Defined in:
lib/ann_wrapper.rb

Overview

wrapper class for ANN API

Constant Summary collapse

ANN_URL =

ANN API anime url

"http://cdn.animenewsnetwork.com/encyclopedia"
ANN_API_URL =
"#{ANN_URL}/api.xml"
ANN_REPORTS_URL =
"#{ANN_URL}/reports.xml"
@@type =
"anime"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



71
72
73
74
75
76
77
78
# 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

Instance Method Details

#batch_items(ids, api_url = ANN_API_URL) ⇒ Object

fetch up to 50 items(Animes or Mangas) in one request



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object

fetch anime and convert to ANN_Anime



46
47
48
# 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 = {}) ⇒ Object

fetch list of titles via reports



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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