royw-imdb 0.1.1 → 0.1.2
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/VERSION.yml +1 -1
- data/spec/cache_extensions.rb +37 -75
- data/spec/spec_helper.rb +10 -2
- metadata +2 -2
data/VERSION.yml
CHANGED
data/spec/cache_extensions.rb
CHANGED
@@ -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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
filespec = page.gsub(/^http:\//,
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
30
|
-
puts eMsg.to_s
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class ImdbSearch
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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 ImdbImage
|
65
|
-
private
|
66
|
-
def read_page(page)
|
67
|
-
html = nil
|
68
|
-
filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
|
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
|
-
#
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
@@ -1,5 +1,13 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
3
|
|
3
|
-
require
|
4
|
+
require 'imdb'
|
4
5
|
|
5
6
|
$samples_dir = File.dirname(__FILE__) + '/samples'
|
7
|
+
|
8
|
+
require 'cache_extensions'
|
9
|
+
CacheExtensions.attach_to_read_page_classes($samples_dir)
|
10
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: royw-imdb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.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-
|
12
|
+
date: 2009-04-20 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|