ero_getter 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.2.0
data/ero_getter.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ero_getter"
8
- s.version = "1.1.2"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["masarakki"]
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
38
38
  "spec/ero_getter/base_spec.rb",
39
39
  "spec/ero_getter_spec.rb",
40
40
  "spec/samples/nijigazou_sokuhou/first.html",
41
+ "spec/samples/nijigazou_sokuhou/global_last.html",
41
42
  "spec/samples/nijigazou_sokuhou/last.html",
42
43
  "spec/samples/nijigazou_sokuhou/middle.html",
43
44
  "spec/samples/pm_style/test.html",
@@ -15,7 +15,7 @@ class NijigazouSokuhou < EroGetter::Base
15
15
  File.join(chara, d)
16
16
  end
17
17
 
18
- after ["//a[@rel='prev']", "//a[@rel='next']"] do |path|
18
+ connection ['a[rel=prev]', 'a[rel=next]'] do |path|
19
19
  path.text.match(Regexp.escape(title_part))
20
20
  end
21
21
 
@@ -5,6 +5,7 @@ class PmStyle < EroGetter::Base
5
5
  path.parent[:href] if path[:alt] =~ /file\d+/
6
6
  end
7
7
  sub_directory do
8
- targets.first.match(/pm_(.+)[a|b|c].zip$/)[1]
8
+ matches = targets.first.match(/pm_(.+?)(\d+)[a|b|c].zip$/)
9
+ "pm_#{matches[1]}_#{"%06d" % matches[2]}"
9
10
  end
10
11
  end
@@ -122,13 +122,14 @@ class EroGetter::Base
122
122
  end
123
123
  end
124
124
 
125
- def after(xpath, &block)
125
+ def connection(css, &block)
126
126
  [:prev, :next].each_with_index do |method_name, index|
127
127
  var_name = "@#{method_name}".to_sym
128
128
  define_method(method_name) do
129
129
  unless instance_variable_defined?(var_name)
130
- tag = document.xpath(xpath[index]).first
131
- instance_variable_set(var_name, instance_exec(tag, &block) ? tag[:href] : nil)
130
+ tag = document.css(css[index]).first
131
+ instance_variable_set(var_name,
132
+ tag && instance_exec(tag, &block) ? tag[:href] : nil)
132
133
  end
133
134
  instance_variable_get(var_name)
134
135
  end
@@ -43,5 +43,13 @@ describe NijigazouSokuhou do
43
43
  its(:next) { should be_nil }
44
44
  its(:prev) { should == url }
45
45
  end
46
+
47
+ context :global_last do
48
+ before do
49
+ fake(:get, url, 'nijigazou_sokuhou/global_last.html')
50
+ end
51
+
52
+ its(:next) { should be_nil }
53
+ end
46
54
  end
47
55
  end
@@ -14,6 +14,6 @@ describe PmStyle do
14
14
  'http://hpcgi2.nifty.com/r-seven/page.cgi?file=copy2/pm/120526/pm_chu2800b.zip',
15
15
  'http://hpcgi2.nifty.com/r-seven/page.cgi?file=copy2/pm/120526/pm_chu2800c.zip']
16
16
  }
17
- its(:sub_directory) { should == 'chu2800' }
17
+ its(:sub_directory) { should == 'pm_chu_002800' }
18
18
  end
19
19
 
@@ -2,112 +2,154 @@ require 'spec_helper'
2
2
 
3
3
  describe EroGetter::Base do
4
4
  let(:regex) { %r{http://example.net/\d+.html} }
5
- before do
6
- _regex = regex
7
- fake(:get, regex, 'sample.html')
8
- @klazz = Class.new(EroGetter::Base) do
9
- name 'NijiEro BBS'
10
- url _regex
5
+ let(:url) { 'http://example.net/10101010.html' }
6
+ subject { @dl }
11
7
 
12
- target "ul#sources li a" do |elm|
13
- elm[:href]
14
- end
8
+ context :without_connection do
9
+ before do
10
+ _regex = regex
11
+ fake(:get, regex, 'sample.html')
12
+ @klazz = Class.new(EroGetter::Base) do
13
+ name 'NijiEro BBS'
14
+ url _regex
15
+
16
+ target "ul#sources li a" do |elm|
17
+ elm[:href]
18
+ end
15
19
 
16
- sub_directory do
17
- targets.map{|x| x.split(%r{/}).last }.join('/')
20
+ sub_directory do
21
+ targets.map{|x| x.split(%r{/}).last }.join('/')
22
+ end
18
23
  end
24
+ @klazz.stub(:to_s).and_return('TestClass')
19
25
  end
20
- @klazz.stub(:to_s).and_return('TestClass')
21
- end
22
26
 
23
- describe "assign url_mapping" do
24
- it { EroGetter.url_mapping.should have_key regex }
25
- it { EroGetter.url_mapping[regex].should == @klazz }
26
- end
27
+ describe "assign url_mapping" do
28
+ it { EroGetter.url_mapping.should have_key regex }
29
+ it { EroGetter.url_mapping[regex].should == @klazz }
30
+ end
27
31
 
28
- describe :instance_methods do
29
- subject { @dl }
30
- context :good do
31
- before do
32
- @dl = @klazz.new('http://example.net/10101010.html')
33
- EroGetter.stub(:mkdir).and_return(true)
34
- EroGetter.stub('directory').and_return('/tmp')
35
- end
36
- its(:name) { should == 'NijiEro BBS' }
37
- its(:url_regex) { should == regex }
38
- its(:base_dir) { should == 'test_class' }
39
- its(:http_client) { should be_a HTTPClient }
40
- its(:document) { should be_a Nokogiri::HTML::Document }
41
- its(:title) { should == 'EroGetter Server' }
42
- its(:url) { should == 'http://example.net/10101010.html' }
43
- its(:direction) { should == :none }
44
- its(:targets) { should == ['https://github.com/masarakki/ero_getter_server',
45
- 'https://github.com/masarakki/ero_getter_chrome_extension'] }
46
- its(:sub_directory) { should == 'ero_getter_server/ero_getter_chrome_extension' }
47
- its(:directory) { should == '/tmp/test_class/ero_getter_server/ero_getter_chrome_extension' }
48
- describe :after_run do
49
- context :not_set_after do
50
- its(:run_next?) { should be_false }
51
- its(:run_prev?) { should be_false }
32
+ describe :instance_methods do
33
+ context :good do
34
+ before do
35
+ @dl = @klazz.new(url)
36
+ EroGetter.stub(:mkdir).and_return(true)
37
+ EroGetter.stub('directory').and_return('/tmp')
52
38
  end
39
+ its(:name) { should == 'NijiEro BBS' }
40
+ its(:url_regex) { should == regex }
41
+ its(:base_dir) { should == 'test_class' }
42
+ its(:http_client) { should be_a HTTPClient }
43
+ its(:document) { should be_a Nokogiri::HTML::Document }
44
+ its(:title) { should == 'EroGetter Server' }
45
+ its(:url) { should == url }
46
+ its(:direction) { should == :none }
47
+ its(:targets) { should == ['https://github.com/masarakki/ero_getter_server',
48
+ 'https://github.com/masarakki/ero_getter_chrome_extension'] }
49
+ its(:sub_directory) { should == 'ero_getter_server/ero_getter_chrome_extension' }
50
+ its(:directory) { should == '/tmp/test_class/ero_getter_server/ero_getter_chrome_extension' }
53
51
 
54
- context :direction_none do
55
- context :has_next do
56
- before do
57
- @dl.stub(:next).and_return('hoge')
58
- end
59
- its(:run_next?) { should be_true }
52
+ describe :after_run do
53
+ context :not_set_after do
54
+ its(:run_next?) { should be_false }
60
55
  its(:run_prev?) { should be_false }
61
56
  end
62
- context :has_prev do
63
- before do
64
- @dl.stub(:prev).and_return('hoge')
57
+
58
+ context :direction_none do
59
+ context :has_next do
60
+ before do
61
+ @dl.stub(:next).and_return('hoge')
62
+ end
63
+ its(:run_next?) { should be_true }
64
+ its(:run_prev?) { should be_false }
65
65
  end
66
- its(:run_next?) { should be_false }
67
- its(:run_prev?) { should be_true }
68
- end
69
- context :has_next_and_prev do
70
- before do
71
- @dl.stub(:prev).and_return('hoge')
72
- @dl.stub(:next).and_return('hoge')
66
+ context :has_prev do
67
+ before do
68
+ @dl.stub(:prev).and_return('hoge')
69
+ end
70
+ its(:run_next?) { should be_false }
71
+ its(:run_prev?) { should be_true }
72
+ end
73
+ context :has_next_and_prev do
74
+ before do
75
+ @dl.stub(:prev).and_return('hoge')
76
+ @dl.stub(:next).and_return('hoge')
77
+ end
78
+ its(:run_next?) { should be_true }
79
+ its(:run_prev?) { should be_true }
73
80
  end
74
- its(:run_next?) { should be_true }
75
- its(:run_prev?) { should be_true }
76
81
  end
77
- end
78
82
 
79
- context :direction_prev do
80
- before do
81
- @dl.stub(:direction).and_return(:prev)
82
- end
83
- context :has_next do
83
+ context :direction_prev do
84
84
  before do
85
- @dl.stub(:next).and_return('hoge')
85
+ @dl.stub(:direction).and_return(:prev)
86
+ end
87
+ context :has_next do
88
+ before do
89
+ @dl.stub(:next).and_return('hoge')
90
+ end
91
+ its(:run_next?) { should be_false }
86
92
  end
87
- its(:run_next?) { should be_false }
88
93
  end
89
- end
90
94
 
91
- context :direction_next do
92
- before do
93
- @dl.stub(:direction).and_return(:next)
94
- end
95
- context :has_pref do
95
+ context :direction_next do
96
96
  before do
97
- @dl.stub(:prev).and_return('hoge')
97
+ @dl.stub(:direction).and_return(:next)
98
+ end
99
+ context :has_pref do
100
+ before do
101
+ @dl.stub(:prev).and_return('hoge')
102
+ end
103
+ its(:run_prev?) { should be_false }
98
104
  end
99
- its(:run_prev?) { should be_false }
100
105
  end
101
106
  end
102
107
  end
108
+
109
+ context :url_mismatch do
110
+ it {
111
+ lambda {
112
+ @klazz.new('http://example.com/10101010.html')
113
+ }.should raise_error
114
+ }
115
+ end
103
116
  end
117
+ end
118
+
119
+ describe :connection do
120
+ before do
121
+ _regex = regex
122
+ klazz = Class.new(EroGetter::Base) do
123
+ url _regex
104
124
 
105
- context :url_mismatch do
106
- it {
107
- lambda {
108
- @klazz.new('http://example.com/10101010.html')
109
- }.should raise_error
110
- }
125
+ connection ['a[rel=prev]', 'a[rel=next]'] do |path|
126
+ path
127
+ end
128
+ end
129
+ @dl = klazz.new(url)
130
+ end
131
+
132
+ context :css_not_found do
133
+ before do
134
+ @dl.document.stub(:css).and_return([])
135
+ end
136
+ its(:prev) { should be_nil }
137
+ its(:next) { should be_nil }
138
+ end
139
+ context :css_find_but_invalid do
140
+ before do
141
+ @dl.document.stub(:css).and_return([false, true, true])
142
+ end
143
+ its(:prev) { should be_nil }
144
+ its(:next) { should be_nil }
145
+ end
146
+ context :css_find_and_valid do
147
+ before do
148
+ x = {:href => 'unko'}
149
+ @dl.document.stub(:css).and_return([x])
150
+ end
151
+ its(:prev) { should == 'unko' }
152
+ its(:next) { should == 'unko' }
111
153
  end
112
154
  end
113
155
  end