royw-tmdb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 0
4
4
  :minor: 0
@@ -1,14 +1,18 @@
1
1
  # == Synopsis
2
2
  # add a mkdirs method to the File class
3
3
  class File
4
- ##
5
- # make directories including any missing in the path
6
- #
7
- # @param [String] dirspec the path to make sure exists
8
- def File.mkdirs(dirspec)
9
- unless File.exists?(dirspec)
10
- mkdirs(File.dirname(dirspec))
11
- Dir.mkdir(dirspec)
4
+ class << self
5
+ my_extension("mkdirs") do
6
+ ##
7
+ # make directories including any missing in the path
8
+ #
9
+ # @param [String] dirspec the path to make sure exists
10
+ def File.mkdirs(dirspec)
11
+ unless File.exists?(dirspec)
12
+ mkdirs(File.dirname(dirspec))
13
+ Dir.mkdir(dirspec)
14
+ end
15
+ end
12
16
  end
13
17
  end
14
18
  end
@@ -0,0 +1,27 @@
1
+ ######################################################################
2
+ # my extensions to Module. (taken from rake, named changed to not clash
3
+ # when rake is used for this rails project.
4
+ #
5
+ class Module
6
+ # Check for an existing method in the current class before extending. IF
7
+ # the method already exists, then a warning is printed and the extension is
8
+ # not added. Otherwise the block is yielded and any definitions in the
9
+ # block will take effect.
10
+ #
11
+ # Usage:
12
+ #
13
+ # class String
14
+ # rake_extension("xyz") do
15
+ # def xyz
16
+ # ...
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ def my_extension(method)
22
+ unless instance_methods.include?(method.to_s) || instance_methods.include?(method.to_sym)
23
+ yield
24
+ end
25
+ end
26
+ end # module Module
27
+
@@ -1,16 +1,32 @@
1
1
  require 'cgi'
2
2
  require 'iconv'
3
3
 
4
- module ImdbStringExtensions
5
-
6
- def unescape_html
7
- Iconv.conv("UTF-8", 'ISO-8859-1', CGI::unescapeHTML(self))
4
+ class String
5
+ my_extension("unescape_html") do
6
+ def unescape_html
7
+ Iconv.conv("UTF-8", 'ISO-8859-1', CGI::unescapeHTML(self))
8
+ end
8
9
  end
9
-
10
- def strip_tags
11
- gsub(/<\/?[^>]*>/, "")
10
+
11
+ my_extension("strip_tags") do
12
+ def strip_tags
13
+ gsub(/<\/?[^>]*>/, "")
14
+ end
15
+ end
16
+
17
+ my_extension("ext") do
18
+ # Replace the file extension with +newext+. If there is no extenson on
19
+ # the string, append the new extension to the end. If the new extension
20
+ # is not given, or is the empty string, remove any existing extension.
21
+ #
22
+ # +ext+ is a user added method for the String class.
23
+ def ext(newext='')
24
+ return self.dup if ['.', '..'].include? self
25
+ if newext != ''
26
+ newext = (newext =~ /^\./) ? newext : ("." + newext)
27
+ end
28
+ dup.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || self + newext
29
+ end
12
30
  end
13
-
14
31
  end
15
32
 
16
- String.send :include, ImdbStringExtensions
@@ -1,11 +1,15 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
1
4
  require 'rubygems'
2
5
  require 'open-uri'
3
6
  require 'date'
4
7
  require 'xmlsimple'
5
8
 
6
- require File.dirname(__FILE__) + '/tmdb/optional_logger'
7
- require File.dirname(__FILE__) + '/tmdb/tmdb_movie'
8
- require File.dirname(__FILE__) + '/tmdb/tmdb_profile'
9
- require File.dirname(__FILE__) + '/string_extensions'
10
- require File.dirname(__FILE__) + '/file_extensions'
11
- require File.dirname(__FILE__) + '/object_extensions'
9
+ require 'tmdb/optional_logger'
10
+ require 'tmdb/tmdb_movie'
11
+ require 'tmdb/tmdb_profile'
12
+ require 'module_extensions'
13
+ require 'string_extensions'
14
+ require 'file_extensions'
15
+ require 'object_extensions'
@@ -2,11 +2,9 @@ class TmdbMovie
2
2
 
3
3
  attr_reader :query, :document
4
4
 
5
- API_KEY = '7a2f6eb9b6aa01651000f0a9324db835'
6
-
7
- def initialize(ident)
5
+ def initialize(ident, key)
8
6
  @imdb_id = 'tt' + ident.gsub(/^tt/, '') unless ident.blank?
9
- @query = "http://api.themoviedb.org/2.0/Movie.imdbLookup?imdb_id=#{@imdb_id}&api_key=#{API_KEY}"
7
+ @query = "http://api.themoviedb.org/2.0/Movie.imdbLookup?imdb_id=#{@imdb_id}&api_key=#{key}"
10
8
  end
11
9
 
12
10
  def fanarts
@@ -14,12 +14,13 @@ class TmdbProfile
14
14
 
15
15
  # options:
16
16
  # :imdb_id => String (either with or without leading 'tt')
17
+ # :api_key => String containing themovieDb.com's API key (required)
17
18
  # :filespec => nil or a valid pathspec
18
19
  # :logger => nil or a logger instance
19
20
  def self.all(options={})
20
21
  result = []
21
22
  if has_option?(options, :imdb_id) || (has_option?(options, :filespec) && File.exist?(options[:filespec]))
22
- result << TmdbProfile.new(options[:imdb_id], options[:filespec], options[:logger])
23
+ result << TmdbProfile.new(options[:imdb_id], options[:api_key], options[:filespec], options[:logger])
23
24
  end
24
25
  result
25
26
  end
@@ -35,8 +36,9 @@ class TmdbProfile
35
36
  options.has_key?(key) && !options[key].blank?
36
37
  end
37
38
 
38
- def initialize(ident, filespec, logger)
39
+ def initialize(ident, key, filespec, logger)
39
40
  @imdb_id = ident
41
+ @api_key = key
40
42
  @filespec = filespec
41
43
  @logger = OptionalLogger.new(logger)
42
44
  load
@@ -65,7 +67,7 @@ class TmdbProfile
65
67
  @movie = from_xml(open(@filespec).read)
66
68
  elsif !@imdb_id.blank?
67
69
  @logger.debug { "loading movie from tmdb.com, filespec=> #{@filespec.inspect}" }
68
- @movie = TmdbMovie.new(@imdb_id.gsub(/^tt/, '')).to_hash
70
+ @movie = TmdbMovie.new(@imdb_id.gsub(/^tt/, ''), @api_key).to_hash
69
71
  save(@filespec) unless @filespec.blank?
70
72
  end
71
73
  unless @movie.blank?
@@ -3,6 +3,8 @@ require 'spec_helper'
3
3
  # Time to add your specs!
4
4
  # http://rspec.info/
5
5
 
6
+ TMDB_API_KEY = '7a2f6eb9b6aa01651000f0a9324db835'
7
+
6
8
  describe "TmdbMovie" do
7
9
 
8
10
  before(:all) do
@@ -10,7 +12,7 @@ describe "TmdbMovie" do
10
12
  end
11
13
 
12
14
  before(:each) do
13
- @profile = TmdbMovie.new('tt0465234')
15
+ @profile = TmdbMovie.new('tt0465234', TMDB_API_KEY)
14
16
  end
15
17
 
16
18
  after(:each) do
@@ -66,7 +68,7 @@ describe "TmdbMovie" do
66
68
  end
67
69
 
68
70
  it "should handle The Sand Pebble" do
69
- profile = TmdbMovie.new('tt0060934')
71
+ profile = TmdbMovie.new('tt0060934', TMDB_API_KEY)
70
72
  profile.idents.should be_nil
71
73
  end
72
74
 
@@ -4,6 +4,8 @@ require 'tempfile'
4
4
  # Time to add your specs!
5
5
  # http://rspec.info/
6
6
 
7
+ TMDB_API_KEY = '7a2f6eb9b6aa01651000f0a9324db835'
8
+
7
9
  describe "TmdbProfile" do
8
10
 
9
11
  before(:all) do
@@ -12,7 +14,7 @@ describe "TmdbProfile" do
12
14
 
13
15
  before(:each) do
14
16
  # tt0465234 => National Treasure: Book of Secrets
15
- @profile = TmdbProfile.first(:imdb_id => 'tt0465234')
17
+ @profile = TmdbProfile.first(:imdb_id => 'tt0465234', :api_key => TMDB_API_KEY)
16
18
  end
17
19
 
18
20
  after(:each) do
@@ -25,7 +27,7 @@ describe "TmdbProfile" do
25
27
 
26
28
  it "should save the profile to a file" do
27
29
  filespec = get_temp_filename
28
- profile = TmdbProfile.first(:imdb_id => 'tt0465234', :filespec => filespec)
30
+ profile = TmdbProfile.first(:imdb_id => 'tt0465234', :api_key => TMDB_API_KEY, :filespec => filespec)
29
31
  (File.exist?(filespec).should be_true) && (File.size(filespec).should > 0)
30
32
  end
31
33
 
@@ -94,7 +96,7 @@ describe "TmdbProfile" do
94
96
  it "should be able to convert to xml and then from xml" do
95
97
  hash = nil
96
98
  begin
97
- profile = TmdbProfile.first(:imdb_id => 'tt0465234')
99
+ profile = TmdbProfile.first(:imdb_id => 'tt0465234', :api_key => TMDB_API_KEY)
98
100
  xml = profile.to_xml
99
101
  hash = XmlSimple.xml_in(xml)
100
102
  rescue
@@ -106,26 +108,26 @@ describe "TmdbProfile" do
106
108
  # now let's test caching the profile to/from a file
107
109
 
108
110
  it "should not create a file if a :filespec option is passed that is nil" do
109
- profile = TmdbProfile.first(:imdb_id => 'tt0465234', :filespec => nil)
111
+ profile = TmdbProfile.first(:imdb_id => 'tt0465234', :api_key => TMDB_API_KEY, :filespec => nil)
110
112
  Dir.glob(File.join(TMPDIR, "imdb_profile_spec*")).empty?.should be_true
111
113
  end
112
114
 
113
115
  it "should create a file if a :filespec option is passed" do
114
116
  filespec = get_temp_filename
115
- profile = TmdbProfile.first(:imdb_id => 'tt0465234', :filespec => filespec)
117
+ profile = TmdbProfile.first(:imdb_id => 'tt0465234', :api_key => TMDB_API_KEY, :filespec => filespec)
116
118
  (File.exist?(filespec) && (File.size(filespec) > 0)).should be_true
117
119
  end
118
120
 
119
121
  it "should load from a file if a :filespec option is passed and the file exists" do
120
122
  filespec = get_temp_filename
121
- profile1 = TmdbProfile.first(:imdb_id => 'tt0465234', :filespec => filespec)
122
- profile2 = TmdbProfile.first(:filespec => filespec)
123
+ profile1 = TmdbProfile.first(:imdb_id => 'tt0465234', :api_key => TMDB_API_KEY, :filespec => filespec)
124
+ profile2 = TmdbProfile.first(:api_key => TMDB_API_KEY, :filespec => filespec)
123
125
  profile1.imdb_id.should == profile2.imdb_id
124
126
  end
125
127
 
126
128
  it "should not load from a file if a :filespec option is passed and the file does not exists" do
127
129
  filespec = get_temp_filename
128
- profile = TmdbProfile.first(:filespec => filespec)
130
+ profile = TmdbProfile.first(:api_key => TMDB_API_KEY, :filespec => filespec)
129
131
  profile.should be_nil
130
132
  end
131
133
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: royw-tmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roy Wright
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-15 00:00:00 -07:00
12
+ date: 2009-04-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,6 +28,7 @@ files:
28
28
  - Rakefile
29
29
  - VERSION.yml
30
30
  - lib/file_extensions.rb
31
+ - lib/module_extensions.rb
31
32
  - lib/object_extensions.rb
32
33
  - lib/string_extensions.rb
33
34
  - lib/tmdb.rb