ann_wrapper 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ require_relative 'ann'
2
+
3
+ class ANN_Media < ANN
4
+
5
+ # @return [Nokogiri::XML::NodeSet] return all info with provided key
6
+ def find_info(obj, key)
7
+ obj.search("info[@type=\"#{key}\"]")
8
+ end
9
+
10
+ # create methods inside calling object
11
+ def create_methods(obj, dictionary)
12
+ dictionary.each do |name, key|
13
+ create_method(name) do
14
+ # find_info in calling object
15
+ info = find_info(key)
16
+ return nil if info.nil?
17
+ info.map do |i|
18
+ i.content
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ # @return [[String]] returns title(s)
25
+ def title; end
26
+ # @return [[String]] returns synopsis
27
+ def synopsis; end
28
+ # @return [[String]] returns array of genres
29
+ def genres; end
30
+ # @return [[String]] returns array of themes
31
+ def themes; end
32
+ # @return [[String]] returns array of vintage(s)
33
+ def vintage; end
34
+
35
+ # @return [[ANN_Image]] returns array of ANN_Image
36
+ def images
37
+ @images ||= find_info("Picture").xpath("./img").map do |i|
38
+ ANN_Image.new(i['src'], i['width'], i['height'])
39
+ end
40
+ end
41
+
42
+ # @return [[ANN_Staff]] returns array of ANN_Staff
43
+ def staff(obj)
44
+ @staff ||= obj.xpath("./staff").map do |s|
45
+ task = s.at_xpath("task")
46
+ person = s.at_xpath("person")
47
+ ANN_Staff.new(person['id'], task.content, person.content)
48
+ end
49
+ end
50
+
51
+ # @return [Hash] returns hash of titles grouped by language abbreviation
52
+ def alt_titles
53
+ titles = find_info("Alternative title").group_by {|title| title['lang']}
54
+ titles.each do |key, value|
55
+ value.map! do |title|
56
+ title.content
57
+ end
58
+ end
59
+ end
60
+
61
+ # @return [[ANN_Rating]] returns array of ANN_Episode
62
+ def ratings(obj)
63
+ @ratings ||= obj.xpath("./ratings").map do |r|
64
+ ANN_Rating.new(r['nb_votes'], r['weighted_score'], r['bayesian_score'])
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'ann'
2
+
3
+ class ANN_Report < ANN
4
+ # initialize and build access methods
5
+ def initialize(ann_report)
6
+ @id, @type, @name, @precision, @vintage = ""
7
+ @ann_report = ann_report
8
+
9
+ self.instance_variables.each do |iv|
10
+ var_name = iv[1..-1]
11
+ create_method(var_name) { get_info_on(var_name) }
12
+ end
13
+ end
14
+
15
+ # get info from xml
16
+ def get_info_on(var_name)
17
+ body = @ann_report.at_xpath(var_name)
18
+ body.content unless body.nil?
19
+ end
20
+
21
+ # @return [Hash] hash of self
22
+ def to_h
23
+ # create hash excluding some methods
24
+ to_hash([:to_h, :ann_report, :get_info_on])
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module AnnWrapper
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
@@ -1,152 +1,122 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
 
4
+
5
+
3
6
  describe ANN_Anime do
4
7
 
5
- def array_of_strings(input)
6
- expect(input).to be_instance_of Array
7
- expect(input[0]).to be_instance_of String
8
+ shared_examples_for "an Array of" do |method, object, expected, index=0|
9
+ let(:result) {ANN_Wrapper.fetch_anime(11770).send(method.to_sym)}
10
+ it "#{object} objects" do
11
+ expect(result).to be_an_instance_of Array
12
+ expect(result.first).to be_an_instance_of object
13
+ end
14
+ it "containing the correct #{method}" do
15
+ expect(result[index]).to eql expected
16
+ end
8
17
  end
9
18
 
10
- before(:each) do
11
- @anime = ANN_Wrapper.fetch_anime(11770)
19
+ shared_examples_for "a String" do |method, expected|
20
+ it "containing the correct #{method}" do
21
+ anime = ANN_Wrapper.fetch_anime 11770
22
+ result = anime.send(:"#{method}")
23
+ expect(result).to be_an_instance_of String
24
+ expect(result).to eql expected
25
+ end
12
26
  end
13
27
 
14
- describe "#id" do
15
- it "returns a string containing an anime id" do
16
- expect(@anime.id).to be_instance_of String
17
- expect(@anime.id.size).to be > 0
28
+ shared_examples_for "a Hash of" do |method, object, key, expected|
29
+ let(:result) {ANN_Wrapper.fetch_anime(11770).send(method.to_sym)}
30
+ it "#{object}s" do
31
+ expect(result).to be_an_instance_of Hash
32
+ expect(result.first).to be_an_instance_of object
18
33
  end
19
- it "returns the correct anime id" do
20
- expect(@anime.id).to eql "11770"
34
+ it "containing the correct #{method}" do
35
+ expect(result[key].first).to eql expected
21
36
  end
22
37
  end
23
38
 
24
- describe "#title" do
25
- it "returns an array of strings" do
26
- array_of_strings(@anime.title)
27
- end
28
- it "returns the correct titles" do
29
- expect(@anime.title[0]).to eql "Steins;Gate"
39
+ describe "#find_info" do
40
+ let(:anime) {ANN_Wrapper.fetch_anime(11770)}
41
+ context "invalid key" do
42
+ it "returns an empty array" do
43
+ expect(anime.find_info("DOES NOT EXIST").size).to eql 0
44
+ end
30
45
  end
31
46
  end
32
47
 
48
+ describe "#id" do
49
+ it_returns "a String", :id, "11770"
50
+ end
51
+
52
+ describe "#title" do
53
+ it_returns "an Array of", :title, String, "Steins;Gate"
54
+ end
55
+
33
56
  describe "#alt_titles" do
34
- it "returns a hash of alternate titles" do
35
- expect(@anime.alt_titles).to be_instance_of Hash
36
- array_of_strings(@anime.alt_titles["JA"])
37
- end
38
- it "JA array is not empty" do
39
- expect(@anime.alt_titles["JA"][0].size).to be > 0
40
- end
57
+ it_returns "a Hash of", :alt_titles, Array, "PT", "Steins-Gate e a Teoria do Caos"
41
58
  end
42
59
 
43
60
  describe "#synopsis" do
44
- it "returns an Array containing a synopsis string" do
45
- array_of_strings @anime.synopsis
46
- end
47
- it "returns the correct synopsis" do
48
- expect(@anime.synopsis[0]).to include "Rintaro Okabe is a self-proclaimed"
49
- end
61
+ it_returns "an Array of", :synopsis, String, "Rintaro Okabe is a self-proclaimed \"mad scientist\" who believes that an international organization is conspiring to reshape the world according to its own interests. He and his friend Itaru Hashida inadvertently create a gadget able to send messages to the past. The discovery and experimentation of this instrument become the catalyst of fundamental alterations to the present. Oblivious of the consequences of their actions, Rintaro and his friends end up creating modifications of grievous proportions. He must then try to find a way to return as close as possible to the original timeline in order to save his precious lab members."
50
62
  end
51
63
 
52
64
  describe "#num_episodes" do
53
- it "returns an Array containing a string of the number of episodes" do
54
- array_of_strings @anime.num_episodes
55
- end
56
- it "returns the correct number of episodes" do
57
- expect(@anime.num_episodes).to eql ["24"]
58
- end
65
+ it_returns "an Array of", :num_episodes, String, "24"
59
66
  end
60
67
 
61
68
  describe "#vintage" do
62
- it "returns an Array containing vintage strings" do
63
- array_of_strings @anime.vintage
64
- end
65
- it "returns the correct vintage" do
66
- expect(@anime.vintage[0]).to eql "2011-04-03 (Advanced screening)"
67
- end
69
+ it_returns "an Array of", :vintage, String, "2011-04-03 (Advanced screening)"
68
70
  end
69
71
 
70
72
  describe "#genres" do
71
- it "returns an Array containing genre strings" do
72
- array_of_strings @anime.genres
73
- end
74
- it "returns the correct genres" do
75
- expect(@anime.genres[0]).to eql "adventure"
76
- end
73
+ it_returns "an Array of", :genres, String, "adventure"
77
74
  end
78
75
 
79
76
  describe "#themes" do
80
- it "returns an Array containing theme strings" do
81
- array_of_strings @anime.themes
82
- end
83
- it "returns the correct themes" do
84
- expect(@anime.themes[0]).to eql "butterfly effect"
85
- end
77
+ it_returns "an Array of", :themes, String, "butterfly effect"
86
78
  end
87
79
 
88
80
  describe "#op_theme" do
89
- it "returns an Array containing op_theme strings" do
90
- array_of_strings @anime.op_theme
91
- end
92
- it "returns the correct op themes" do
93
- expect(@anime.op_theme[0]).to eql "\"Hacking to the Gate\" by Kanako Ito"
94
- end
81
+ it_returns "an Array of", :op_theme, String, "\"Hacking to the Gate\" by Kanako Ito"
95
82
  end
96
83
 
97
84
  describe "#ed_theme" do
98
- it "returns an Array containing ed_theme strings" do
99
- array_of_strings @anime.ed_theme
100
- end
101
- it "returns the correct ed_themes" do
102
- expect(@anime.ed_theme[2]).to eql "#3: \"Another Heaven\" by Kanako Itou (ep 24)"
103
- end
85
+ it_returns "an Array of", :ed_theme, String, "#3: \"Another Heaven\" by Kanako Itou (ep 24)", 2
86
+ end
87
+
88
+ describe "#ratings" do
89
+ it_returns "an Array of", :ratings, ANN_Rating, ANN_Rating.new("3788", "9.1129", "9.1075")
90
+ end
91
+
92
+ describe "#episodes" do
93
+ it_returns "an Array of", :episodes, ANN_Episode, ANN_Episode.new("1", "Prologue of the Beginning and End", "EN")
104
94
  end
105
95
 
106
96
  describe "#cast" do
107
- it "returns an Array containing ANN_Cast" do
108
- expect(@anime.cast).to be_instance_of Array
109
- expect(@anime.cast[0]).to be_instance_of ANN_Cast
110
- end
111
- it "correctly stores cast information in an ANN_Cast" do
112
- first_cast = ANN_Cast.new("1386", "Yugo \"Braun\" Tennoji", "Christopher R. Sabat", "EN")
113
- expect(@anime.cast[0]).to eql first_cast
114
- end
97
+ it_returns "an Array of", :cast, ANN_Cast, ANN_Cast.new("110469", "Rintarō Okabe", "Peter Lehn", "DE")
115
98
  end
116
99
 
117
100
  describe "#staff" do
118
- it "returns an Array containing ANN_Staff" do
119
- expect(@anime.staff).to be_instance_of Array
120
- expect(@anime.staff[1]).to be_instance_of ANN_Staff
121
- end
122
- it "correctly stores staff information in an ANN_Staff" do
123
- first_staff = ANN_Staff.new("9693", "Director", "Hiroshi Hamasaki")
124
- expect(@anime.staff[1]).to eql first_staff
125
- end
101
+ it_returns "an Array of", :staff, ANN_Staff, ANN_Staff.new("9693", "Director", "Hiroshi Hamasaki"), 1
126
102
  end
127
103
 
128
104
  describe "#images" do
129
- it "returns an Array containing ANN_Image" do
130
- expect(@anime.images).to be_instance_of Array
131
- expect(@anime.images[0]).to be_instance_of ANN_Image
132
- end
133
- it "correctly stores image information in an ANN_Image" do
134
- first_image = ANN_Image.new("http://cdn.animenewsnetwork.com/thumbnails/fit200x200/encyc/A11770-1864351140.1370764886.jpg", "200", "125")
135
- expect(@anime.images[0]).to eql first_image
136
- end
105
+ it_returns "an Array of", :images, ANN_Image, ANN_Image.new("http://cdn.animenewsnetwork.com/thumbnails/hotlink-fit200x200/encyc/A11770-1864351140.1370764886.jpg", "200", "125")
137
106
  end
138
107
 
139
108
  describe "#to_h" do
140
- it "returns a hash of all information" do
141
- anime_hash = @anime.to_h
109
+ let(:anime) {ANN_Wrapper.fetch_anime(11770)}
110
+ it "returns a hash of anime information" do
111
+ anime_hash = anime.to_h
142
112
  expect(anime_hash).to be_instance_of Hash
143
113
  expect(anime_hash[:id]).to eql "11770"
144
114
  end
145
115
  it "correctly converts structs to hash" do
146
- anime_hash = @anime.to_h
116
+ anime_hash = anime.to_h
147
117
  staff_hash = ANN_Staff.new("9693", "Director", "Hiroshi Hamasaki").hash
148
118
  expect(anime_hash[:staff][1]).to eql staff_hash
149
119
  end
150
120
  end
151
121
  end
152
-
122
+
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe ANN_Manga do
5
+
6
+ shared_examples_for "an Array of" do |method, object, expected, index=0|
7
+ let(:result) {ANN_Wrapper.fetch_manga(16086).send(method.to_sym)}
8
+ it "#{object} objects" do
9
+ expect(result).to be_an_instance_of Array
10
+ expect(result.first).to be_an_instance_of object
11
+ end
12
+ it "containing the correct #{method}" do
13
+ expect(result[index]).to eql expected
14
+ end
15
+ end
16
+
17
+ shared_examples_for "a String" do |method, expected|
18
+ it "containing the correct #{method}" do
19
+ manga = ANN_Wrapper.fetch_manga 16086
20
+ result = manga.send(:"#{method}")
21
+ expect(result).to be_an_instance_of String
22
+ expect(result).to eql expected
23
+ end
24
+ end
25
+
26
+ shared_examples_for "a Hash of" do |method, object, key, expected|
27
+ let(:result) {ANN_Wrapper.fetch_manga(16086).send(method.to_sym)}
28
+ it "#{object}s" do
29
+ expect(result).to be_an_instance_of Hash
30
+ expect(result.first).to be_an_instance_of object
31
+ end
32
+ it "containing the correct #{method}" do
33
+ expect(result[key].first).to eql expected
34
+ end
35
+ end
36
+
37
+ describe "#find_info" do
38
+ let(:manga) {ANN_Wrapper.fetch_manga(16086)}
39
+ context "invalid key" do
40
+ it "returns an empty array" do
41
+ expect(manga.find_info("DOES NOT EXIST").size).to eql 0
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#id" do
47
+ it_returns "a String", :id, "16086"
48
+ end
49
+
50
+ describe "#title" do
51
+ it_returns "an Array of", :title, String, "Tokyo Ghoul"
52
+ end
53
+
54
+ describe "#alt_titles" do
55
+ it_returns "a Hash of", :alt_titles, Array, "JA", "東京喰種トーキョーグール"
56
+ end
57
+
58
+ describe "#synopsis" do
59
+ it_returns "an Array of", :synopsis, String, "Ken Kaneki is a bookworm college student who meets a girl named Rize at a cafe he frequents. They're the same age and have the same interests, so they quickly become close. Little does Kaneki know that Rize is a ghoul - a kind of monster that lives by hunting and devouring human flesh. When part of her special organ - \"the red child\" - is transplanted into Kaneki, he becomes a ghoul himself, trapped in a warped world where humans are not the top of the food chain."
60
+
61
+ end
62
+
63
+ describe "#vintage" do
64
+ it_returns "an Array of", :vintage, String, "2011-09-08 to 2014-09-18 (serialized in Weekly Young Jump)"
65
+ end
66
+
67
+ describe "#ratings" do
68
+ it_returns "an Array of", :ratings, ANN_Rating, ANN_Rating.new("32", "8.5385", "8.4562")
69
+ end
70
+
71
+ describe "#num_tankoubon" do
72
+ it_returns "an Array of", :num_tankoubon, String, "9"
73
+ end
74
+
75
+ describe "#num_pages" do
76
+ it_returns "an Array of", :num_pages, String, "202"
77
+ end
78
+
79
+ describe "#genres" do
80
+ it_returns "an Array of", :genres, String, "horror"
81
+ end
82
+
83
+ describe "#themes" do
84
+ it_returns "an Array of", :themes, String, "monsters"
85
+ end
86
+
87
+ describe "#staff" do
88
+ it_returns "an Array of", :staff, ANN_Staff, ANN_Staff.new("123563", "Story & Art", "Sui Ishida")
89
+ end
90
+
91
+ describe "#images" do
92
+ it_returns "an Array of", :images, ANN_Image, ANN_Image.new("http://cdn.animenewsnetwork.com/thumbnails/hotlink-fit200x200/encyc/A16086-3098213756.1401567064.jpg", "141", "200")
93
+ end
94
+
95
+ describe "#to_h" do
96
+ let(:manga) {ANN_Wrapper.fetch_manga(16086)}
97
+ it "returns a hash of manga information" do
98
+ manga_hash = manga.to_h
99
+ expect(manga_hash).to be_instance_of Hash
100
+ expect(manga_hash[:id]).to eql "16086"
101
+ end
102
+ it "correctly converts structs to hash" do
103
+ manga_hash = manga.to_h
104
+ staff_hash = ANN_Staff.new("123563", "Story & Art", "Sui Ishida").hash
105
+ expect(manga_hash[:staff][0]).to eql staff_hash
106
+ end
107
+ end
108
+ end
109
+
@@ -0,0 +1,62 @@
1
+ # clear global definitions
2
+
3
+ require 'spec_helper'
4
+
5
+
6
+ describe ANN_Report do
7
+ shared_examples_for "a String" do |method, expected|
8
+ it "containing the correct #{method}" do
9
+ report = ANN_Wrapper.fetch_titles({type: "anime", nskip: 0, nlist: 5})[0]
10
+ result = report.send(:"#{method}")
11
+ expect(result).to be_an_instance_of String
12
+ expect(result).to eql expected
13
+ end
14
+ end
15
+
16
+ describe "#id" do
17
+ it_returns "a String", :id, "15847"
18
+ end
19
+
20
+ describe "#type" do
21
+ it_returns "a String", :type, "TV"
22
+ end
23
+
24
+ describe "#name" do
25
+ it_returns "a String", :name, "Nandaka Velonica"
26
+ end
27
+
28
+ describe "#precision" do
29
+ it_returns "a String", :precision, "TV"
30
+ end
31
+
32
+ describe "#vintage" do
33
+ it_returns "a String", :vintage, "2014-03-10 to 2014-03-21"
34
+ end
35
+
36
+ describe "get_info_on" do
37
+ let(:report) {ANN_Wrapper.fetch_titles({type: "anime", nskip: 0, nlist: 5})[0]}
38
+ context "when a valid info key is provided" do
39
+ it "returns the correct value for the key" do
40
+ result = report.get_info_on("id")
41
+ expect(result).to be_an_instance_of String
42
+ expect(result).to eql "15847"
43
+ end
44
+ end
45
+
46
+ context "when an invalid info key is provided" do
47
+ it "returns nil" do
48
+ result = report.get_info_on("INVALID")
49
+ expect(result).to be nil
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#to_h" do
55
+ let(:report) {ANN_Wrapper.fetch_titles({type: "anime", nskip: 0, nlist: 5})[0]}
56
+ it "returns a hash of report information" do
57
+ report_hash = report.to_h
58
+ expect(report_hash).to be_instance_of Hash
59
+ expect(report_hash[:id]).to eql "15847"
60
+ end
61
+ end
62
+ end