ruby-tmdb 0.0.19

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Aaron Gough (http://thingsaaronmade.com/)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ == ruby-tmdb
2
+
3
+ ruby-tmdb is an ActiveRecord-style API wrapper for {TheMovieDB.org}[http://www.themoviedb.org/] ({TMDb}[http://www.themoviedb.org/]). ruby-tmdb is designed to make common tasks like retrieving all movies that have a given actor in them and fetching image data much easier than they would be if dealing directly with the URL based API. ruby-tmdb also provides query caching to minimize duplicate remote API queries.
4
+
5
+ === Installation
6
+
7
+ gem install ruby-tmdb
8
+
9
+ === Usage
10
+
11
+ require 'rubygems'
12
+ require 'ruby-tmdb'
13
+
14
+ # setup your API key
15
+ Tmdb.api_key = "t478f8de5776c799de5a"
16
+
17
+ @movie = TmdbMovie.find(:title => "Iron Man", :limit => 1)
18
+ # => <TmdbMovie>
19
+
20
+ @movie.name
21
+ # => "Iron Man"
22
+
23
+ @movie.posters.length
24
+ # => 12
25
+
26
+ @movie.posters.first.data
27
+ # => [binary blob representing JPEG]
28
+
29
+ @movie.cast.first.bio.movies
30
+ # => [<TmdbMovie>,<TmdbMovie>,<TmdbMovie>,<TmdbMovie>]
31
+
32
+ @actor = TmdbCast.find(:name => "Brad Pitt", :limit => 1)
33
+ # => <TmdbCast>
34
+
35
+ @actor.birthplace
36
+ # => "Shawnee, Oklahoma, United States"
37
+
38
+ Find all movies whose titles match a given string
39
+
40
+ @movies = TmdbMovie.find(:title => 'Iron Man')
41
+
42
+ Find the movie most likely to be associated with a given title
43
+
44
+ @movie = TmdbMovie.find(:title => 'Sin City', :limit => 1)
45
+
46
+ Find a single movie by it's TMDb ID
47
+
48
+ @movie = TmdbMovie.find(:id => 187)
49
+
50
+ Find a single movie by it's IMDB ID
51
+
52
+ @movie = TmdbMovie.find(:imdb => 'tt0401792')
53
+
54
+ === Author & Credits
55
+
56
+ Author:: {Aaron Gough}[mailto:aaron@aarongough.com]
57
+
58
+ Copyright (c) 2010 {Aaron Gough}[http://thingsaaronmade.com/] ({thingsaaronmade.com}[http://thingsaaronmade.com/]), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ begin
9
+ require 'jeweler'
10
+ Jeweler::Tasks.new do |gemspec|
11
+ gemspec.name = "ruby-tmdb"
12
+ gemspec.summary = "An ActiveRecord-style API wrapper for TheMovieDB.org"
13
+ gemspec.description = "An ActiveRecord-style API wrapper for TheMovieDB.org"
14
+ gemspec.email = "aaron@aarongough.com"
15
+ gemspec.homepage = "https://github.com/aarongough/ruby-tmdb"
16
+ gemspec.authors = ["Aaron Gough"]
17
+ gemspec.rdoc_options << '--line-numbers' << '--inline-source'
18
+ gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
19
+ gemspec.add_development_dependency "webmock"
20
+ end
21
+ rescue LoadError
22
+ puts "Jeweler not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+
26
+ desc 'Test ruby-tmdb.'
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib/*.rb'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = true
32
+ end
33
+
34
+
35
+ desc 'Generate documentation for ruby-tmdb.'
36
+ Rake::RDocTask.new(:rdoc) do |rdoc|
37
+ rdoc.rdoc_dir = 'rdoc'
38
+ rdoc.title = 'ruby-tmdb'
39
+ rdoc.options << '--line-numbers' << '--inline-source'
40
+ rdoc.rdoc_files.include('README.rdoc')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ rdoc.rdoc_files.include('app/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.19
@@ -0,0 +1,50 @@
1
+ class Tmdb
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'cgi'
6
+
7
+ @@api_key = ""
8
+ @@api_response = {}
9
+
10
+ def self.api_key
11
+ @@api_key
12
+ end
13
+
14
+ def self.api_key=(key)
15
+ @@api_key = key
16
+ end
17
+
18
+ def self.base_api_url
19
+ "http://api.themoviedb.org/2.1/"
20
+ end
21
+
22
+ def self.api_call(method, data, language = "en")
23
+ url = Tmdb.base_api_url + method + '/' + language + '/yaml/' + Tmdb.api_key + '/' + CGI::escape(data.to_s)
24
+ response = @@api_response[url] ||= begin
25
+ Tmdb.get_url(url)
26
+ end
27
+ if(response.code.to_i != 200)
28
+ return []
29
+ end
30
+ YAML::load(response.body)
31
+ end
32
+
33
+ # Get a URL and return a response object, follow upto 'limit' re-directs on the way
34
+ def self.get_url(uri_str, limit = 10)
35
+ return false if limit == 0
36
+ begin
37
+ response = Net::HTTP.get_response(URI.parse(uri_str))
38
+ rescue SocketError, Errno::ENETDOWN
39
+ response = Net::HTTPBadRequest.new( '404', 404, "Not Found" )
40
+ return response
41
+ end
42
+ case response
43
+ when Net::HTTPSuccess then response
44
+ when Net::HTTPRedirection then get_url(response['location'], limit - 1)
45
+ else
46
+ Net::HTTPBadRequest.new( '404', 404, "Not Found" )
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,76 @@
1
+ class TmdbCast
2
+
3
+ def self.find(options)
4
+ raise ArgumentError, "At least one of: id, name, should be supplied" if(options[:id].nil? && options[:name].nil?)
5
+
6
+ results = []
7
+ unless(options[:id].nil? || options[:id].to_s.empty?)
8
+ results << Tmdb.api_call('Person.getInfo', options[:id])
9
+ end
10
+ unless(options[:name].nil? || options[:name].to_s.empty?)
11
+ results << Tmdb.api_call('Person.search', options[:name])
12
+ end
13
+
14
+ results.flatten!
15
+
16
+ unless(options[:limit].nil?)
17
+ raise ArgumentError, ":limit must be an integer greater than 0" unless(options[:limit].is_a?(Fixnum) && options[:limit] > 0)
18
+ results = results.slice(0, options[:limit])
19
+ end
20
+
21
+ results.map!{|c| TmdbCast.new(c) }
22
+
23
+ if(results.length == 1)
24
+ return results[0]
25
+ else
26
+ return results
27
+ end
28
+ end
29
+
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
69
+ end
70
+
71
+ def ==(other)
72
+ return false unless(other.is_a?(TmdbCast))
73
+ @raw_data == other.raw_data
74
+ end
75
+
76
+ end
@@ -0,0 +1,84 @@
1
+ class TmdbMovie
2
+
3
+ def self.find(options)
4
+ raise ArgumentError, "At least one of: id, title, imdb should be supplied" if(options[:id].nil? && options[:imdb].nil? && options[:title].nil?)
5
+
6
+ results = []
7
+ unless(options[:id].nil? || options[:id].to_s.empty?)
8
+ results << Tmdb.api_call("Movie.getInfo", options[:id])
9
+ end
10
+ unless(options[:imdb].nil? || options[:imdb].to_s.empty?)
11
+ results << Tmdb.api_call("Movie.imdbLookup", options[:imdb])
12
+ end
13
+ unless(options[:title].nil? || options[:title].to_s.empty?)
14
+ results << Tmdb.api_call("Movie.search", options[:title])
15
+ end
16
+
17
+ results.flatten!
18
+
19
+ unless(options[:limit].nil?)
20
+ raise ArgumentError, ":limit must be an integer greater than 0" unless(options[:limit].is_a?(Fixnum) && options[:limit] > 0)
21
+ results = results.slice(0, options[:limit])
22
+ end
23
+
24
+ results.map!{|m| TmdbMovie.new(m) }
25
+
26
+ if(results.length == 1)
27
+ return results[0]
28
+ else
29
+ return results
30
+ end
31
+ end
32
+
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
77
+ end
78
+
79
+ def ==(other)
80
+ return false unless(other.is_a?(TmdbMovie))
81
+ return @raw_data == other.raw_data
82
+ end
83
+
84
+ end
data/lib/ruby-tmdb.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_files = []
2
+ require_files.concat Dir[File.join(File.dirname(__FILE__), 'ruby-tmdb', '*.rb')]
3
+
4
+ require_files.each do |file|
5
+ require File.expand_path(file)
6
+ end
data/ruby-tmdb.gemspec ADDED
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-tmdb}
8
+ s.version = "0.0.19"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Aaron Gough"]
12
+ s.date = %q{2010-05-26}
13
+ s.description = %q{An ActiveRecord-style API wrapper for TheMovieDB.org}
14
+ s.email = %q{aaron@aarongough.com}
15
+ s.extra_rdoc_files = [
16
+ "MIT-LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/ruby-tmdb.rb",
25
+ "lib/ruby-tmdb/tmdb.rb",
26
+ "lib/ruby-tmdb/tmdb_cast.rb",
27
+ "lib/ruby-tmdb/tmdb_movie.rb",
28
+ "ruby-tmdb.gemspec",
29
+ "test/fixtures/example_com.txt",
30
+ "test/fixtures/image.jpg",
31
+ "test/fixtures/incorrect_api_url.txt",
32
+ "test/fixtures/movie_get_info.txt",
33
+ "test/fixtures/movie_imdb_lookup.txt",
34
+ "test/fixtures/movie_search.txt",
35
+ "test/fixtures/person_get_info.txt",
36
+ "test/fixtures/person_search.txt",
37
+ "test/setup/.gitignore",
38
+ "test/setup/test_unit_extensions.rb",
39
+ "test/setup/url_mocks.rb",
40
+ "test/test_helper.rb",
41
+ "test/unit/tmdb_cast_test.rb",
42
+ "test/unit/tmdb_movie_test.rb",
43
+ "test/unit/tmdb_test.rb"
44
+ ]
45
+ s.homepage = %q{https://github.com/aarongough/ruby-tmdb}
46
+ s.rdoc_options = ["--charset=UTF-8", "--line-numbers", "--inline-source"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.7}
49
+ s.summary = %q{An ActiveRecord-style API wrapper for TheMovieDB.org}
50
+ s.test_files = [
51
+ "test/setup/test_unit_extensions.rb",
52
+ "test/setup/url_mocks.rb",
53
+ "test/test_helper.rb",
54
+ "test/unit/tmdb_cast_test.rb",
55
+ "test/unit/tmdb_movie_test.rb",
56
+ "test/unit/tmdb_test.rb"
57
+ ]
58
+
59
+ if s.respond_to? :specification_version then
60
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
61
+ s.specification_version = 3
62
+
63
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
+ s.add_development_dependency(%q<webmock>, [">= 0"])
65
+ else
66
+ s.add_dependency(%q<webmock>, [">= 0"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<webmock>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,25 @@
1
+ HTTP/1.1 200 OK
2
+ Server: Apache/2.2.3 (CentOS)
3
+ Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
4
+ ETag: "24ec5-1b6-4059a80bfd280"
5
+ Accept-Ranges: bytes
6
+ Content-Type: text/html; charset=UTF-8
7
+ Connection: Keep-Alive
8
+ Date: Sun, 16 May 2010 19:01:25 GMT
9
+ Age: 6053
10
+ Content-Length: 438
11
+
12
+ <HTML>
13
+ <HEAD>
14
+ <TITLE>Example Web Page</TITLE>
15
+ </HEAD>
16
+ <body>
17
+ <p>You have reached this web page by typing &quot;example.com&quot;,
18
+ &quot;example.net&quot;,
19
+ or &quot;example.org&quot; into your web browser.</p>
20
+ <p>These domain names are reserved for use in documentation and are not available
21
+ for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
22
+ 2606</a>, Section 3.</p>
23
+ </BODY>
24
+ </HTML>
25
+
Binary file
@@ -0,0 +1,13 @@
1
+ HTTP/1.1 404 Not Found
2
+ Status: 404 Not Found
3
+ Content-Type: text/html
4
+ X-Cascade: pass
5
+ Content-Length: 18
6
+ Date: Sun, 16 May 2010 19:07:32 GMT
7
+ X-Varnish: 4026308020 4014879260
8
+ Age: 8217
9
+ Via: 1.1 varnish
10
+ Connection: keep-alive
11
+ X-Cache: HIT
12
+
13
+ <h1>Not Found</h1>