royw-tmdb 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 4
2
+ :patch: 5
3
3
  :major: 0
4
4
  :minor: 1
@@ -1,91 +1,53 @@
1
- # NOTE extremely ugly and non-DRY. Probably good candidate for meta programming.
2
1
 
3
2
  # override the classes' read_page method and replace with one
4
3
  # that will cache pages in spec/samples/{url}
5
4
 
6
- class TmdbMovie
7
- private
8
- def read_page(page)
9
- html = nil
10
- filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
11
- if File.exist?(filespec)
12
- html = open(filespec).read
13
- else
14
- html = open(page).read
15
- cache_html_files(page, html)
16
- end
17
- html
18
- end
5
+ module CacheExtensions
6
+ # == Synopsis
7
+ # Attach the read_page and cache_file methods to the given
8
+ # class (cls) and use the given directory for the cache files
9
+ def self.attach_to(cls, directory='/tmp')
19
10
 
20
- # this is used to save imdb pages so they may be used by rspec
21
- def cache_html_files(page, html)
22
- begin
23
- filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
24
- unless File.exist?(filespec)
25
- puts "caching #{filespec}"
26
- File.mkdirs(File.dirname(filespec))
27
- File.open(filespec, 'w') { |f| f.puts html }
11
+ # define the read_page(page) method on the given class: cls
12
+ cls.send('define_method', "read_page") do |page|
13
+ data = nil
14
+ filespec = page.gsub(/^http:\//, directory).gsub(/\/$/, '.html')
15
+ if File.exist?(filespec)
16
+ data = open(filespec).read
17
+ else
18
+ data = open(page).read
19
+ cache_file(page, data)
28
20
  end
29
- rescue Exception => eMsg
30
- puts eMsg.to_s
31
- end
32
- end
33
- end
34
-
35
- class TmdbSearch
36
- private
37
- def read_page(page)
38
- html = nil
39
- filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
40
- if File.exist?(filespec)
41
- html = open(filespec).read
42
- else
43
- html = open(page).read
44
- cache_html_files(page, html)
21
+ data
45
22
  end
46
- html
47
- end
48
23
 
49
- # this is used to save imdb pages so they may be used by rspec
50
- def cache_html_files(page, html)
51
- begin
52
- filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
53
- unless File.exist?(filespec)
54
- puts "caching #{filespec}"
55
- File.mkdirs(File.dirname(filespec))
56
- File.open(filespec, 'w') { |f| f.puts html }
24
+ # define the cache_file(page, data) method on the given class: cls
25
+ cls.send('define_method', "cache_file") do |page, data|
26
+ begin
27
+ filespec = page.gsub(/^http:\//, directory).gsub(/\/$/, '.html')
28
+ unless File.exist?(filespec)
29
+ puts "caching #{filespec}"
30
+ File.mkdirs(File.dirname(filespec))
31
+ File.open(filespec, 'w') { |f| f.puts data }
32
+ end
33
+ rescue Exception => eMsg
34
+ puts eMsg.to_s
57
35
  end
58
- rescue Exception => eMsg
59
- puts eMsg.to_s
60
- end
61
- end
62
- end
63
-
64
- class TmdbImage
65
- private
66
- def read_page(page)
67
- html = nil
68
- filespec = page.gsub(/^http:\//, 'spec/samples')
69
- if File.exist?(filespec)
70
- html = open(filespec).read
71
- else
72
- html = open(page).read
73
- cache_html_files(page, html)
74
36
  end
75
- html
76
37
  end
77
38
 
78
- # this is used to save imdb pages so they may be used by rspec
79
- def cache_html_files(page, html)
80
- begin
81
- filespec = page.gsub(/^http:\//, 'spec/samples')
82
- unless File.exist?(filespec)
83
- puts "caching #{filespec}"
84
- File.mkdirs(File.dirname(filespec))
85
- File.open(filespec, 'w') { |f| f.puts html }
39
+ # == Synopsis
40
+ # Find all classes that have a read_page instance method and
41
+ # then overwrite that read_page method with one that handles
42
+ # the caching. Use the given directory for the cache files.
43
+ def self.attach_to_read_page_classes(directory='/tmp')
44
+ ObjectSpace.each_object(Class) do |cls|
45
+ if(cls.public_instance_methods(false).include?("read_page") ||
46
+ cls.protected_instance_methods(false).include?("read_page") ||
47
+ cls.private_instance_methods(false).include?("read_page"))
48
+ CacheExtensions.attach_to(cls, directory)
86
49
  end
87
- rescue Exception => eMsg
88
- puts eMsg.to_s
89
50
  end
90
51
  end
91
52
  end
53
+
data/spec/spec_helper.rb CHANGED
@@ -2,14 +2,16 @@ require 'spec'
2
2
 
3
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
- require 'tmdb'
6
5
 
7
- require 'cache_extensions'
6
+ require 'tmdb'
8
7
 
9
8
  $samples_dir = File.dirname(__FILE__) + '/samples'
10
9
 
11
10
  TMPDIR = File.join(File.dirname(__FILE__), '../tmp')
12
11
 
12
+ require 'cache_extensions'
13
+ CacheExtensions.attach_to_read_page_classes($samples_dir)
14
+
13
15
  Spec::Runner.configure do |config|
14
16
 
15
17
  end
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.1.4
4
+ version: 0.1.5
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-19 00:00:00 -07:00
12
+ date: 2009-04-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15