ruby-tmdb 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -26,6 +26,9 @@ ruby-tmdb is an ActiveRecord-style API wrapper for {TheMovieDB.org}[http://www.t
26
26
  @movie.posters.first.data
27
27
  # => [binary blob representing JPEG]
28
28
 
29
+ @movie.cast.first.bio.movies
30
+ # => [<TmdbMovie>,<TmdbMovie>,<TmdbMovie>,<TmdbMovie>]
31
+
29
32
  @actor = TmdbCast.find(:name => "Brad Pitt", :limit => 1)
30
33
  # => <TmdbCast>
31
34
 
data/Rakefile CHANGED
@@ -16,6 +16,7 @@ begin
16
16
  gemspec.authors = ["Aaron Gough"]
17
17
  gemspec.rdoc_options << '--line-numbers' << '--inline-source'
18
18
  gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
19
+ gemspec.add_dependency "deepopenstruct"
19
20
  gemspec.add_development_dependency "webmock"
20
21
  end
21
22
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -4,6 +4,7 @@ class Tmdb
4
4
  require 'uri'
5
5
  require 'cgi'
6
6
  require 'yaml'
7
+ require 'deepopenstruct'
7
8
 
8
9
  @@api_key = ""
9
10
  @@api_response = {}
@@ -21,12 +22,8 @@ class Tmdb
21
22
  end
22
23
 
23
24
  def self.api_call(method, data, language = "en")
24
- raise ArgumentError, "Tmdb.api_key must be set before using the API" if(Tmdb.api_key.nil? || Tmdb.api_key.empty?)
25
25
  url = Tmdb.base_api_url + method + '/' + language + '/yaml/' + Tmdb.api_key + '/' + CGI::escape(data.to_s)
26
- # Memoize this API call
27
- response = @@api_response[url] ||= begin
28
- Tmdb.get_url(url)
29
- end
26
+ response = Tmdb.get_url(url)
30
27
  if(response.code.to_i != 200)
31
28
  return nil
32
29
  end
@@ -55,4 +52,35 @@ class Tmdb
55
52
  end
56
53
  end
57
54
 
55
+ def self.data_to_object(data)
56
+ object = DeepOpenStruct.load(data)
57
+ object.raw_data = data
58
+ ["posters", "backdrops", "profile"].each do |image_array_name|
59
+ if(object.respond_to?(image_array_name))
60
+ image_array = object.send(image_array_name)
61
+ image_array.each_index do |x|
62
+ image_array[x] = image_array[x].image
63
+ image_array[x].instance_eval <<-EOD
64
+ def self.data
65
+ return Tmdb.get_url(self.url).body
66
+ end
67
+ EOD
68
+ end
69
+ end
70
+ if(object.profile)
71
+ object.profiles = object.profile
72
+ end
73
+ end
74
+ unless(object.cast.nil?)
75
+ object.cast.each_index do |x|
76
+ object.cast[x].instance_eval <<-EOD
77
+ def self.bio
78
+ return TmdbCast.find(:id => self.id, :limit => 1)
79
+ end
80
+ EOD
81
+ end
82
+ end
83
+ return object
84
+ end
85
+
58
86
  end
@@ -1,6 +1,10 @@
1
1
  class TmdbCast
2
2
 
3
3
  def self.find(options)
4
+ options = {
5
+ :expand_results => true
6
+ }.merge(options)
7
+
4
8
  raise ArgumentError, "At least one of: id, name, should be supplied" if(options[:id].nil? && options[:name].nil?)
5
9
 
6
10
  results = []
@@ -18,7 +22,7 @@ class TmdbCast
18
22
  results = results.slice(0, options[:limit])
19
23
  end
20
24
 
21
- results.map!{|c| TmdbCast.new(c) }
25
+ results.map!{|c| TmdbCast.new(c, options[:expand_results]) }
22
26
 
23
27
  if(results.length == 1)
24
28
  return results[0]
@@ -27,45 +31,9 @@ class TmdbCast
27
31
  end
28
32
  end
29
33
 
30
- def initialize(raw_data)
31
- @raw_data = raw_data
32
- @raw_data = Tmdb.api_call('Person.getInfo', @raw_data["id"]).first
33
- @raw_data["profiles"] = @raw_data["profile"]
34
- @raw_data.delete("profile")
35
- @raw_data.each_pair do |key, value|
36
- instance_eval <<-EOD
37
- def #{key}
38
- @raw_data["#{key}"]
39
- end
40
- EOD
41
- if(value.is_a?(Array))
42
- value.each_index do |x|
43
- if(value[x].is_a?(Hash) && value[x].length == 1)
44
- if(value[x].keys[0] == "image")
45
- value[x][value[x].keys[0]].instance_eval <<-EOD
46
- def self.data
47
- Tmdb.get_url(self["url"]).body
48
- end
49
- EOD
50
- end
51
- value[x] = value[x][value[x].keys[0]]
52
- end
53
- if(value[x].is_a?(Hash))
54
- value[x].each_pair do |key2, value2|
55
- value[x].instance_eval <<-EOD
56
- def self.#{key2}
57
- self["#{key2}"]
58
- end
59
- EOD
60
- end
61
- end
62
- end
63
- end
64
- end
65
- end
66
-
67
- def raw_data
68
- @raw_data
34
+ def self.new(raw_data, expand_results = false)
35
+ raw_data = Tmdb.api_call('Person.getInfo', raw_data["id"]).first
36
+ return Tmdb.data_to_object(raw_data)
69
37
  end
70
38
 
71
39
  def ==(other)
@@ -1,6 +1,10 @@
1
1
  class TmdbMovie
2
2
 
3
3
  def self.find(options)
4
+ options = {
5
+ :expand_results => true
6
+ }.merge(options)
7
+
4
8
  raise ArgumentError, "At least one of: id, title, imdb should be supplied" if(options[:id].nil? && options[:imdb].nil? && options[:title].nil?)
5
9
 
6
10
  results = []
@@ -9,6 +13,7 @@ class TmdbMovie
9
13
  end
10
14
  unless(options[:imdb].nil? || options[:imdb].to_s.empty?)
11
15
  results << Tmdb.api_call("Movie.imdbLookup", options[:imdb])
16
+ options[:expand_results] = true
12
17
  end
13
18
  unless(options[:title].nil? || options[:title].to_s.empty?)
14
19
  results << Tmdb.api_call("Movie.search", options[:title])
@@ -21,7 +26,7 @@ class TmdbMovie
21
26
  results = results.slice(0, options[:limit])
22
27
  end
23
28
 
24
- results.map!{|m| TmdbMovie.new(m) }
29
+ results.map!{|m| TmdbMovie.new(m, options[:expand_results]) }
25
30
 
26
31
  if(results.length == 1)
27
32
  return results[0]
@@ -30,50 +35,9 @@ class TmdbMovie
30
35
  end
31
36
  end
32
37
 
33
- def initialize(raw_data)
34
- @raw_data = raw_data
35
- @raw_data = Tmdb.api_call('Movie.getInfo', @raw_data["id"]).first
36
- @raw_data.each_pair do |key, value|
37
- instance_eval <<-EOD
38
- def #{key}
39
- @raw_data["#{key}"]
40
- end
41
- EOD
42
- if(value.is_a?(Array))
43
- value.each_index do |x|
44
- if(value[x].is_a?(Hash) && value[x].length == 1)
45
- if(value[x].keys[0] == "image")
46
- value[x][value[x].keys[0]].instance_eval <<-EOD
47
- def self.data
48
- Tmdb.get_url(self["url"]).body
49
- end
50
- EOD
51
- end
52
- value[x] = value[x][value[x].keys[0]]
53
- end
54
- if(value[x].is_a?(Hash))
55
- value[x].each_pair do |key2, value2|
56
- value[x].instance_eval <<-EOD
57
- def self.#{key2}
58
- self["#{key2}"]
59
- end
60
- EOD
61
- if(key == "cast")
62
- value[x].instance_eval <<-EOD
63
- def self.bio
64
- TmdbCast.find(:id => #{value[x]["id"]}, :limit => 1)
65
- end
66
- EOD
67
- end
68
- end
69
- end
70
- end
71
- end
72
- end
73
- end
74
-
75
- def raw_data
76
- @raw_data
38
+ def self.new(raw_data, expand_results = false)
39
+ raw_data = Tmdb.api_call('Movie.getInfo', raw_data["id"]).first if(expand_results)
40
+ return Tmdb.data_to_object(raw_data)
77
41
  end
78
42
 
79
43
  def ==(other)
data/ruby-tmdb.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-tmdb}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aaron Gough"]
12
- s.date = %q{2010-07-21}
12
+ s.date = %q{2010-08-19}
13
13
  s.description = %q{An ActiveRecord-style API wrapper for TheMovieDB.org}
14
14
  s.email = %q{aaron@aarongough.com}
15
15
  s.extra_rdoc_files = [
@@ -17,8 +17,7 @@ Gem::Specification.new do |s|
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
- ".gitignore",
21
- "MIT-LICENSE",
20
+ "MIT-LICENSE",
22
21
  "README.rdoc",
23
22
  "Rakefile",
24
23
  "VERSION",
@@ -37,11 +36,9 @@ Gem::Specification.new do |s|
37
36
  "test/fixtures/person_get_info.txt",
38
37
  "test/fixtures/person_search.txt",
39
38
  "test/setup/.gitignore",
40
- "test/setup/setup_api_key.rb",
41
39
  "test/setup/test_unit_extensions.rb",
42
40
  "test/setup/url_mocks.rb",
43
41
  "test/test_helper.rb",
44
- "test/unit/test_direct_require.rb",
45
42
  "test/unit/tmdb_cast_test.rb",
46
43
  "test/unit/tmdb_movie_test.rb",
47
44
  "test/unit/tmdb_test.rb"
@@ -52,11 +49,9 @@ Gem::Specification.new do |s|
52
49
  s.rubygems_version = %q{1.3.7}
53
50
  s.summary = %q{An ActiveRecord-style API wrapper for TheMovieDB.org}
54
51
  s.test_files = [
55
- "test/setup/setup_api_key.rb",
56
- "test/setup/test_unit_extensions.rb",
52
+ "test/setup/test_unit_extensions.rb",
57
53
  "test/setup/url_mocks.rb",
58
54
  "test/test_helper.rb",
59
- "test/unit/test_direct_require.rb",
60
55
  "test/unit/tmdb_cast_test.rb",
61
56
  "test/unit/tmdb_movie_test.rb",
62
57
  "test/unit/tmdb_test.rb"
@@ -67,11 +62,14 @@ Gem::Specification.new do |s|
67
62
  s.specification_version = 3
68
63
 
69
64
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
65
+ s.add_runtime_dependency(%q<deepopenstruct>, [">= 0"])
70
66
  s.add_development_dependency(%q<webmock>, [">= 0"])
71
67
  else
68
+ s.add_dependency(%q<deepopenstruct>, [">= 0"])
72
69
  s.add_dependency(%q<webmock>, [">= 0"])
73
70
  end
74
71
  else
72
+ s.add_dependency(%q<deepopenstruct>, [">= 0"])
75
73
  s.add_dependency(%q<webmock>, [">= 0"])
76
74
  end
77
75
  end
data/test/test_helper.rb CHANGED
@@ -2,6 +2,8 @@ TEST_LIVE_API = false
2
2
 
3
3
  require 'rubygems'
4
4
  require 'test/unit'
5
+ require 'yaml'
6
+ require 'net/http'
5
7
 
6
8
  unless(TEST_LIVE_API)
7
9
  require 'webmock/test_unit'
@@ -16,5 +18,6 @@ require_files.each do |file|
16
18
  require File.expand_path(file)
17
19
  end
18
20
 
19
- #load(File.join('unit', 'test_direct_require.rb'), true)
20
- system('ruby ' + File.expand_path(File.join(File.dirname(__FILE__), 'unit', 'test_direct_require.rb')))
21
+ File.open(File.join(File.dirname(__FILE__), 'setup', 'tmdb_api_key.txt')) do |file|
22
+ Tmdb.api_key = file.read
23
+ end
@@ -6,6 +6,13 @@ class TmdbCastTest < Test::Unit::TestCase
6
6
  register_api_url_stubs
7
7
  end
8
8
 
9
+ test "cast data should be able to be dumped and re-loaded" do
10
+ assert_nothing_raised do
11
+ cast = TmdbCast.find(:id => 287)
12
+ TmdbCast.new(cast.raw_data)
13
+ end
14
+ end
15
+
9
16
  test "find by id should return full cast data" do
10
17
  cast = TmdbCast.find(:id => 287)
11
18
  assert_cast_methodized(cast, 287)
@@ -17,8 +24,8 @@ class TmdbCastTest < Test::Unit::TestCase
17
24
  assert_equal cast1, cast2
18
25
  end
19
26
 
20
- test "find by name should return full cast data" do
21
- cast = TmdbCast.find(:name => "Brad Pitt").first
27
+ test "find by name should return full cast data when :expand_results = true" do
28
+ cast = TmdbCast.find(:name => "Brad Pitt", :expand_results => true).first
22
29
  assert_cast_methodized(cast, 287)
23
30
  end
24
31
 
@@ -29,14 +36,14 @@ class TmdbCastTest < Test::Unit::TestCase
29
36
  end
30
37
 
31
38
  test "find by id should return a single cast member" do
32
- assert_kind_of TmdbCast, TmdbCast.find(:id => 287)
39
+ assert_kind_of OpenStruct, TmdbCast.find(:id => 287)
33
40
  end
34
41
 
35
42
  test "find by name should return an array of cast members" do
36
43
  cast_members = TmdbCast.find(:name => "vince")
37
44
  assert_kind_of Array, cast_members
38
45
  cast_members.each do |actor|
39
- assert_kind_of TmdbCast, actor
46
+ assert_kind_of OpenStruct, actor
40
47
  end
41
48
  end
42
49
 
@@ -58,7 +65,7 @@ class TmdbCastTest < Test::Unit::TestCase
58
65
 
59
66
  test "should only return a single item if limit=1" do
60
67
  actor = TmdbCast.find(:name => "Vince", :limit => 1)
61
- assert_kind_of TmdbCast, actor
68
+ assert_kind_of OpenStruct, actor
62
69
  end
63
70
 
64
71
  test "should return X items if limit=X" do
@@ -67,7 +74,7 @@ class TmdbCastTest < Test::Unit::TestCase
67
74
  assert_kind_of Array, actors
68
75
  assert_equal x, actors.length
69
76
  actors.each do |actor|
70
- assert_kind_of TmdbCast, actor
77
+ assert_kind_of OpenStruct, actor
71
78
  end
72
79
  end
73
80
  end
@@ -6,6 +6,13 @@ class TmdbMovieTest < Test::Unit::TestCase
6
6
  register_api_url_stubs
7
7
  end
8
8
 
9
+ test "movie should be able to be dumped and re-loaded" do
10
+ assert_nothing_raised do
11
+ movie = TmdbMovie.find(:id => 187)
12
+ TmdbMovie.new(movie.raw_data)
13
+ end
14
+ end
15
+
9
16
  test "find by id should return the full movie data" do
10
17
  movie = TmdbMovie.find(:id => 187)
11
18
  assert_movie_methodized(movie, 187)
@@ -22,8 +29,8 @@ class TmdbMovieTest < Test::Unit::TestCase
22
29
  assert_movie_methodized(movie, 187)
23
30
  end
24
31
 
25
- test "find by title should return the full movie data" do
26
- movie = TmdbMovie.find(:title => "Sin City", :limit => 1)
32
+ test "find by title should return the full movie data when expand_results set to true" do
33
+ movie = TmdbMovie.find(:title => "Transformers: Revenge of the Fallen", :limit => 1, :expand_results => true)
27
34
  assert_movie_methodized(movie, 187)
28
35
  end
29
36
 
@@ -34,23 +41,23 @@ class TmdbMovieTest < Test::Unit::TestCase
34
41
  end
35
42
 
36
43
  test "find by id should return a single movie" do
37
- assert_kind_of TmdbMovie, TmdbMovie.find(:id => 187)
44
+ assert_kind_of OpenStruct, TmdbMovie.find(:id => 187)
38
45
  end
39
46
 
40
47
  test "find by imdb should return a single movie" do
41
- assert_kind_of TmdbMovie, TmdbMovie.find(:imdb => "tt0401792")
48
+ assert_kind_of OpenStruct, TmdbMovie.find(:imdb => "tt0401792")
42
49
  end
43
50
 
44
51
  test "find by title should return an array of movies" do
45
52
  movies = TmdbMovie.find(:title => "Iron Man")
46
53
  assert_kind_of Array, movies
47
54
  movies.each do |movie|
48
- assert_kind_of TmdbMovie, movie
55
+ assert_kind_of OpenStruct, movie
49
56
  end
50
57
  end
51
58
 
52
59
  test "find by title with limit=1 should return a single movie" do
53
- assert_kind_of TmdbMovie, TmdbMovie.find(:title => "Iron Man", :limit => 1)
60
+ assert_kind_of OpenStruct, TmdbMovie.find(:title => "Iron Man", :limit => 1)
54
61
  end
55
62
 
56
63
  test "find by title with limit=X should return an array of X movies" do
@@ -59,7 +66,7 @@ class TmdbMovieTest < Test::Unit::TestCase
59
66
  assert_kind_of Array, movies
60
67
  assert_equal x, movies.length
61
68
  movies.each do |movie|
62
- assert_kind_of TmdbMovie, movie
69
+ assert_kind_of OpenStruct, movie
63
70
  end
64
71
  end
65
72
  end
@@ -28,15 +28,6 @@ class TmdbTest < Test::Unit::TestCase
28
28
  assert_equal 404, test_response.code.to_i
29
29
  end
30
30
 
31
- test "api_call should raise exception if api_key is not set" do
32
- old_api_key = Tmdb.api_key
33
- Tmdb.api_key = ""
34
- assert_raises ArgumentError do
35
- Tmdb.api_call('Movie.search', 'Transformers')
36
- end
37
- Tmdb.api_key = old_api_key
38
- end
39
-
40
31
  test "should perform Movie.search API call and return array of results" do
41
32
  movies = Tmdb.api_call('Movie.search', 'Transformers')
42
33
  assert_kind_of Array, movies
@@ -116,5 +107,43 @@ class TmdbTest < Test::Unit::TestCase
116
107
  person = Tmdb.api_call('Person.getInfo', 287)[0]
117
108
  assert_not_nil person[person.keys[0]]
118
109
  end
110
+
111
+ test "data_to_object should create object from nested data structures" do
112
+ test_data = {
113
+ :test1 => [
114
+ 1,2,3,4
115
+ ],
116
+ :test2 => 1
117
+ }
118
+ test_object = Tmdb.data_to_object(test_data)
119
+ assert_nothing_raised do
120
+ assert_equal [1,2,3,4], test_object.test1
121
+ assert_equal 1, test_object.test2
122
+ end
123
+ end
124
+
125
+ test "data_to_object should include raw_data method that returns original data" do
126
+ test_data = {
127
+ :test1 => [1,2,3]
128
+ }
129
+ test_object = Tmdb.data_to_object(test_data)
130
+ assert_equal test_object.raw_data, test_data
131
+ end
132
+
133
+ test "data_to_object should convert arrays containing images to nicer format" do
134
+ test_data = {
135
+ "backdrops" => [
136
+ {
137
+ "image" => {
138
+ :test => 1
139
+ }
140
+ }
141
+ ]
142
+ }
143
+ test_object = Tmdb.data_to_object(test_data)
144
+ assert_nothing_raised do
145
+ assert_equal 1, test_object.backdrops[0].test
146
+ end
147
+ end
119
148
 
120
149
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Aaron Gough
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-21 00:00:00 -04:00
17
+ date: 2010-08-19 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: webmock
21
+ name: deepopenstruct
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -28,8 +28,21 @@ dependencies:
28
28
  segments:
29
29
  - 0
30
30
  version: "0"
31
- type: :development
31
+ type: :runtime
32
32
  version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: webmock
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
33
46
  description: An ActiveRecord-style API wrapper for TheMovieDB.org
34
47
  email: aaron@aarongough.com
35
48
  executables: []
@@ -40,7 +53,6 @@ extra_rdoc_files:
40
53
  - MIT-LICENSE
41
54
  - README.rdoc
42
55
  files:
43
- - .gitignore
44
56
  - MIT-LICENSE
45
57
  - README.rdoc
46
58
  - Rakefile
@@ -60,11 +72,9 @@ files:
60
72
  - test/fixtures/person_get_info.txt
61
73
  - test/fixtures/person_search.txt
62
74
  - test/setup/.gitignore
63
- - test/setup/setup_api_key.rb
64
75
  - test/setup/test_unit_extensions.rb
65
76
  - test/setup/url_mocks.rb
66
77
  - test/test_helper.rb
67
- - test/unit/test_direct_require.rb
68
78
  - test/unit/tmdb_cast_test.rb
69
79
  - test/unit/tmdb_movie_test.rb
70
80
  - test/unit/tmdb_test.rb
@@ -103,11 +113,9 @@ signing_key:
103
113
  specification_version: 3
104
114
  summary: An ActiveRecord-style API wrapper for TheMovieDB.org
105
115
  test_files:
106
- - test/setup/setup_api_key.rb
107
116
  - test/setup/test_unit_extensions.rb
108
117
  - test/setup/url_mocks.rb
109
118
  - test/test_helper.rb
110
- - test/unit/test_direct_require.rb
111
119
  - test/unit/tmdb_cast_test.rb
112
120
  - test/unit/tmdb_movie_test.rb
113
121
  - test/unit/tmdb_test.rb
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- *.gem
@@ -1,3 +0,0 @@
1
- File.open(File.join(File.dirname(__FILE__), 'tmdb_api_key.txt')) do |file|
2
- Tmdb.api_key = file.read
3
- end
@@ -1,25 +0,0 @@
1
- require "test/unit"
2
-
3
- require_files = []
4
- require_files << File.join(File.dirname(__FILE__), "..", "..", "lib", "ruby-tmdb.rb")
5
- require_files.concat Dir[File.join(File.dirname(__FILE__), '..', 'setup', '*.rb')]
6
-
7
- require_files.each do |file|
8
- require File.expand_path(file)
9
- end
10
-
11
- class DirectRequireTest < Test::Unit::TestCase
12
-
13
- test "TmdbMovie should not raise exception when directly required without using rubygems" do
14
- assert_nothing_raised do
15
- TmdbMovie.find(:id => 187)
16
- end
17
- end
18
-
19
- test "TmdbCast should not raise exception when directly required without using rubygems" do
20
- assert_nothing_raised do
21
- TmdbCast.find(:id => 287)
22
- end
23
- end
24
-
25
- end