ero_getter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create @ero_getter
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport'
4
+ gem 'httpclient'
5
+ gem 'rubyzip'
6
+
7
+ group :development, :test do
8
+ gem 'rspec'
9
+ gem 'bundler'
10
+ gem 'jeweler'
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 masarakki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = ero_getter
2
+
3
+
4
+ == Contributing to ero_getter
5
+
6
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
7
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
8
+ * Fork the project.
9
+ * Start a feature/bugfix branch.
10
+ * Commit and push until you are happy with your contribution.
11
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
12
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
13
+
14
+ == Copyright
15
+
16
+ Copyright (c) 2012 masarakki. See LICENSE.txt for
17
+ further details.
18
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "ero_getter"
18
+ gem.homepage = "http://github.com/masarakki/ero_getter"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{ero getter}
21
+ gem.description = %Q{ero getter}
22
+ gem.email = "masaki@hisme.net"
23
+ gem.authors = ["masarakki"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "ero_getter #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,32 @@
1
+ require 'active_support/inflector'
2
+ require 'httpclient'
3
+
4
+ class EroGetter::Downloader::Base
5
+ def initialize(url)
6
+ raise unless url.match url_regex
7
+ @url = url
8
+ end
9
+
10
+ def base_dir
11
+ self.class.to_s.underscore
12
+ end
13
+
14
+ def http_client
15
+ @http_client ||= HTTPClient.new
16
+ end
17
+
18
+ class << self
19
+ def name(site_name)
20
+ define_method(:name) do
21
+ site_name
22
+ end
23
+ end
24
+
25
+ def url(regex)
26
+ define_method(:url_regex) do
27
+ regex
28
+ end
29
+ EroGetter.add_mapping(regex, self)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ require 'zip/zip'
2
+ require 'digest/md5'
3
+
4
+ class EroGetter::Downloader
5
+ autoload :Base, 'ero_getter/downloader/base'
6
+
7
+ class << self
8
+ def base_path
9
+ path = File.join(ENV['HOME'], 'ero_getter')
10
+ mkdir(path) unless Dir.exists?(path)
11
+ path
12
+ end
13
+
14
+ def mkdir(path)
15
+ basedir = File.dirname(path)
16
+ mkdir(basedir) unless File.exists?(basedir)
17
+ Dir.mkdir(path)
18
+ end
19
+
20
+ def unzip(zip_data)
21
+ tmp_file = File.join(base_path, Digest::MD5.hexdigest(zip_data))
22
+ begin
23
+ File.open(tmp_file, 'wb') { |f| f.write zip_data }
24
+ result = []
25
+ Zip::ZipInputStream.open(tmp_file) do |zip|
26
+ while entry = zip.get_next_entry
27
+ filename = entry.name_in(entry.name_encoding)
28
+ if entry.file? && !filename.match(/\.(txt|html)$/)
29
+ result << [filename, entry.get_input_stream {|f| f.read}]
30
+ end
31
+ end
32
+ end
33
+ ensure
34
+ File.delete tmp_file if File.exists?(tmp_file)
35
+ end
36
+ result
37
+ end
38
+ end
39
+ end
data/lib/ero_getter.rb ADDED
@@ -0,0 +1,25 @@
1
+ class EroGetter
2
+ autoload :Downloader, 'ero_getter/downloader'
3
+
4
+ def detect(url)
5
+ self.class.url_mapping.each_pair do |regex, klazz|
6
+ return klazz if url.match regex
7
+ end
8
+ nil
9
+ end
10
+
11
+ def download(url)
12
+ klazz = detect(url)
13
+ raise unless klazz
14
+ klazz.new(url).run
15
+ end
16
+
17
+ class << self
18
+ def url_mapping
19
+ @@url_mapping ||= {}
20
+ end
21
+ def add_mapping(regex, strategy)
22
+ url_mapping[regex] = strategy
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe EroGetter::Downloader::Base do
4
+ let!(:regex) { %r{http://example.net/\d+.html} }
5
+ before(:all) do
6
+ _regex = regex
7
+ TestClass = Class.new(EroGetter::Downloader::Base) do
8
+ name 'NijiEro BBS'
9
+ url _regex
10
+ end
11
+ end
12
+
13
+ describe "assign url_mapping" do
14
+ it { EroGetter.url_mapping.should have_key regex }
15
+ it { EroGetter.url_mapping[regex].should == TestClass }
16
+ end
17
+
18
+ describe :instance_methods do
19
+ subject { @klazz }
20
+ context :good do
21
+ before { @klazz = TestClass.new('http://example.net/10101010.html') }
22
+ its(:name) { should == 'NijiEro BBS' }
23
+ its(:url_regex) { should == regex }
24
+ its(:base_dir) { should == 'test_class' }
25
+ its(:http_client) { should be_a HTTPClient }
26
+ end
27
+ context :url_mismatch do
28
+ it {
29
+ lambda {
30
+ TestClass.new('http://example.com/10101010.html')
31
+ }.should raise_error
32
+ }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe EroGetter::Downloader do
4
+ subject { @downloader }
5
+ before { @downloader = EroGetter::Downloader.new }
6
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe EroGetter do
4
+ let(:regex) { %r{http://example.com/\d+.html} }
5
+ subject { @ero_getter }
6
+ describe :class_methods do
7
+ subject { EroGetter }
8
+ describe :url_mapping do
9
+ its(:url_mapping) { should == {} }
10
+ context :add_mapping do
11
+ before do
12
+ @strategy = Class.new
13
+ @regex = regex
14
+ EroGetter.add_mapping(@regex, @strategy)
15
+ end
16
+ its(:url_mapping) { should == {@regex => @strategy} }
17
+ end
18
+ end
19
+ end
20
+
21
+ describe :instance_methods do
22
+ let(:url) { 'http://example.com/0101010.html' }
23
+ before do
24
+ @strategy = Class.new
25
+ @regex = regex
26
+ EroGetter.add_mapping(@regex, @strategy)
27
+ @ero_getter = EroGetter.new
28
+ end
29
+
30
+ describe :detect do
31
+ context :match do
32
+ it { subject.detect(url).should == @strategy }
33
+ end
34
+ context :mismatch do
35
+ it { subject.detect(url.gsub(/com/, 'net')).should be_nil }
36
+ end
37
+ end
38
+
39
+ describe :download do
40
+ context :match do
41
+ it "detect and run" do
42
+ @instance = @strategy.new
43
+ @strategy.should_receive(:new).with(url).and_return(@instance)
44
+ @instance.should_receive(:run)
45
+ subject.download(url)
46
+ end
47
+ end
48
+
49
+ context :mismatch do
50
+ it {
51
+ lambda {
52
+ subject.download(url.gsub(/com/, 'net'))
53
+ }.should raise_error
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ero_getter'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ero_getter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - masarakki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: httpclient
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rubyzip
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ero getter
111
+ email: masaki@hisme.net
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ files:
118
+ - .document
119
+ - .rspec
120
+ - .rvmrc
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.rdoc
124
+ - Rakefile
125
+ - VERSION
126
+ - lib/ero_getter.rb
127
+ - lib/ero_getter/downloader.rb
128
+ - lib/ero_getter/downloader/base.rb
129
+ - spec/downloader/base_spec.rb
130
+ - spec/downloader_spec.rb
131
+ - spec/ero_getter_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: http://github.com/masarakki/ero_getter
134
+ licenses:
135
+ - MIT
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ segments:
147
+ - 0
148
+ hash: 72716279475598011
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.24
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: ero getter
161
+ test_files: []