porras-imdb 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,6 +1,4 @@
1
1
 
2
- ImdbMovie
3
-
4
2
  ImdbMovie Indiana Jones and the Last Crusade
5
3
  - should query IMDB url
6
4
  - should get director
@@ -8,6 +6,7 @@ ImdbMovie Indiana Jones and the Last Crusade
8
6
  - should get the rating
9
7
  - should get cast members
10
8
  - should get the writers
9
+ - should get the year
11
10
  - should get the release date
12
11
  - should get the genres
13
12
  - should get the plot
@@ -40,24 +39,25 @@ ImdbMovie Han robado una estrella
40
39
  - should get the company
41
40
  - should not get any photos
42
41
 
43
- ImdbSearch
44
-
45
- ImdbSearch Indiana Jones
42
+ ImdbSearch search that returns multiple movies
46
43
  - should query IMDB url
44
+ - should not allow to change the query
47
45
 
48
- ImdbSearch Indiana Jones movies
46
+ ImdbSearch search that returns multiple movies movies
49
47
  - should be a collection of ImdbMovie instances
50
48
  - should include 'Indiana Jones and the Last Crusade'
51
49
  - should have titles
52
50
  - should not have titles with HTML tags
51
+ - should not have duplicate movies
53
52
 
54
- ImdbSearch searches with potential encoding issues
53
+ ImdbSearch search that redirects to the lone matching movie movies
54
+ - should be a collection containing a single ImdbMovie instance
55
+ - should have the correct ID
56
+ - should have the correct title
55
57
 
56
58
  ImdbSearch searches with potential encoding issues movies
57
59
  - should include 'Misión en Marbella'
58
60
 
59
- String
60
-
61
61
  String unescape_html
62
62
  - should convert & to &
63
63
  - should convert ó to ó
@@ -65,6 +65,6 @@ String unescape_html
65
65
  String strip_tags
66
66
  - should strip HTML tags
67
67
 
68
- Finished in 3.044303 seconds
68
+ Finished in 0.829689 seconds
69
69
 
70
- 42 examples, 0 failures
70
+ 48 examples, 0 failures
@@ -27,6 +27,10 @@ class ImdbMovie
27
27
  def writers
28
28
  document.search("h5[text()^='Writers'] ~ a").map { |link| link.innerHTML.strip.unescape_html }.reject { |w| w == 'more' }.uniq rescue []
29
29
  end
30
+
31
+ def year
32
+ document.search('a[@href^="/Sections/Years/"]').innerHTML.to_i
33
+ end
30
34
 
31
35
  def release_date
32
36
  date = document.search("//h5[text()^='Release Date']/..").innerHTML[/^\d{1,2} \w+ \d{4}/]
@@ -1,23 +1,37 @@
1
1
  class ImdbSearch
2
2
 
3
- attr_accessor :query
4
-
5
3
  def initialize(query)
6
- self.query = query
4
+ @query = query
7
5
  end
8
6
 
9
7
  def movies
10
- @movies ||= document.search('a[@href^="/title/tt"]').reject do |element|
11
- element.innerHTML.strip_tags.empty?
12
- end.map do |element|
13
- ImdbMovie.new(element['href'][/\d+/], element.innerHTML.strip_tags.unescape_html)
14
- end
8
+ @movies ||= (exact_match? ? parse_movie : parse_movies)
15
9
  end
16
10
 
17
11
  private
18
12
 
19
13
  def document
20
- @document ||= Hpricot(open("http://www.imdb.com/find?q=#{CGI::escape(query)};s=tt").read)
14
+ @document ||= Hpricot(open("http://www.imdb.com/find?q=#{CGI::escape(@query)};s=tt").read)
15
+ end
16
+
17
+ def parse_movies
18
+ document.search('a[@href^="/title/tt"]').reject do |element|
19
+ element.innerHTML.strip_tags.empty?
20
+ end.map do |element|
21
+ [element['href'][/\d+/], element.innerHTML.strip_tags.unescape_html]
22
+ end.uniq.map do |values|
23
+ ImdbMovie.new(*values)
24
+ end
25
+ end
26
+
27
+ def parse_movie
28
+ id = document.at("a[@name='poster']")['href'][/\d+$/]
29
+ title = document.at("h1").innerHTML.split('<span').first.strip.unescape_html
30
+ [ImdbMovie.new(id, title)]
31
+ end
32
+
33
+ def exact_match?
34
+ document.at("title[text()='IMDb Search']").nil?
21
35
  end
22
36
 
23
37
  end
@@ -40,6 +40,10 @@ describe ImdbMovie do
40
40
  @imdb_movie.writers.should include('Philip Kaufman')
41
41
  @imdb_movie.writers.should_not include('more')
42
42
  end
43
+
44
+ it "should get the year" do
45
+ @imdb_movie.year.should == 1989
46
+ end
43
47
 
44
48
  it "should get the release date" do
45
49
  @imdb_movie.release_date.should be_an_instance_of(Date)
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe ImdbSearch do
4
4
 
5
- describe 'Indiana Jones' do
5
+ describe 'search that returns multiple movies' do
6
6
 
7
7
  before(:each) do
8
8
  @imdb_search = ImdbSearch.new('indiana jones')
@@ -13,6 +13,10 @@ describe ImdbSearch do
13
13
  @imdb_search.should_receive(:open).with("http://www.imdb.com/find?q=indiana+jones;s=tt").and_return(open("#{$samples_dir}/sample_search.html"))
14
14
  @imdb_search.send(:document)
15
15
  end
16
+
17
+ it "should not allow to change the query" do
18
+ lambda { @imdb_search.query = 'wadus' }.should raise_error(NoMethodError)
19
+ end
16
20
 
17
21
  describe "movies" do
18
22
 
@@ -39,7 +43,37 @@ describe ImdbSearch do
39
43
  movie.title.should_not match(/<.+>/)
40
44
  end
41
45
  end
46
+
47
+ it "should not have duplicate movies" do
48
+ all_movie_ids = @imdb_search.movies.collect {|m| m.id}
49
+ unique_movie_ids = all_movie_ids.uniq
50
+ all_movie_ids.should == unique_movie_ids
51
+ end
52
+ end
42
53
 
54
+ end
55
+
56
+ describe 'search that redirects to the lone matching movie' do
57
+
58
+ before(:each) do
59
+ @imdb_search = ImdbSearch.new('some extremely specific search for indiana jones')
60
+ @imdb_search.stub!(:open).and_return(open("#{$samples_dir}/sample_movie.html"))
61
+ end
62
+
63
+ describe "movies" do
64
+
65
+ it "should be a collection containing a single ImdbMovie instance" do
66
+ @imdb_search.movies.size.should == 1
67
+ @imdb_search.movies.first.should be_an_instance_of(ImdbMovie)
68
+ end
69
+
70
+ it "should have the correct ID" do
71
+ @imdb_search.movies.first.id.should == '0097576'
72
+ end
73
+
74
+ it "should have the correct title" do
75
+ @imdb_search.movies.first.title.should == 'Indiana Jones and the Last Crusade'
76
+ end
43
77
  end
44
78
 
45
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: porras-imdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Gil