file_downloader 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aff7e304f4ee483465531fc88771ed4bcbd27a81c9eb63616510f2b2f89f6329
4
+ data.tar.gz: aec856c86c69065b0ee85ad09620c11633245989bc707e5d6e6a1737928601fc
5
+ SHA512:
6
+ metadata.gz: '082ad13685f86dbbe1d30ba18cce0eb93facc735127c3cefd87e2ec4de31046d997a4fc377a5b202c691634e71cc26a974d9c9ed20a66ac4b37f1fdc4858ebfb'
7
+ data.tar.gz: 5923d9e2f336efafac6d864eb4f7f5eddd230a8044abcb5cd10b7b7533f67e7cf981b01e8b34ee79a0be82aa10bde067cf7738349b6e256e48cf364f93717282
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,241 @@
1
+ require:
2
+ - rubocop-rspec
3
+ inherit_from: .rubocop_todo.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.1
7
+
8
+ NewCops: enable
9
+ SuggestExtensions: false
10
+
11
+ Exclude:
12
+ - 'vendor/**/*'
13
+ - 'node_modules/**/*'
14
+ - 'db/schema.rb'
15
+ - 'config/**/*'
16
+ - 'bin/*'
17
+ - 'lib/capistrano/**/*'
18
+ - 'tmp/**/*'
19
+ # rspec が書き出しているファイルなので無視
20
+ - 'spec/spec_helper.rb'
21
+ # guard が書き出したファイルが元なので無視する
22
+ - Guardfile
23
+ # annotate が書き出したファイル
24
+ - 'lib/tasks/auto_annotate_models.rake'
25
+ DisplayCopNames: true
26
+
27
+ #################### Layout ################################
28
+
29
+ # .の位置指定、テストの.to hogehogeも見られてしまう
30
+ Layout/DotPosition:
31
+ Enabled: false
32
+
33
+ # ガード条件の後ろに空行を入れるか?
34
+ # rubocop の趣味には追随しない
35
+ Layout/EmptyLineAfterGuardClause:
36
+ Enabled: false
37
+
38
+ # メソッドを1列で完結させるかマルチラインにするか?
39
+ # whereとかマルチラインになることが多いメソッドをチェックしている
40
+ # ライン文字数で代替可能な気がする
41
+ Layout/MultilineMethodCallIndentation:
42
+ Enabled: false
43
+
44
+ Layout/LineLength:
45
+ Enabled: false
46
+
47
+ #################### Lint ################################
48
+
49
+ Lint/AmbiguousBlockAssociation:
50
+ Exclude:
51
+ - 'spec/**/*'
52
+
53
+ ##################### Metrics ##################################
54
+
55
+ Metrics/AbcSize:
56
+ Enabled: false
57
+
58
+ Metrics/BlockLength:
59
+ Enabled: false
60
+
61
+ Metrics/BlockNesting:
62
+ Enabled: false
63
+
64
+ Metrics/ClassLength:
65
+ Enabled: false
66
+
67
+ Metrics/CyclomaticComplexity:
68
+ Enabled: false
69
+
70
+ Metrics/MethodLength:
71
+ Enabled: false
72
+
73
+ Metrics/ModuleLength:
74
+ Enabled: false
75
+
76
+ Metrics/ParameterLists:
77
+ Enabled: false
78
+
79
+ Metrics/PerceivedComplexity:
80
+ Enabled: false
81
+
82
+ #################### Naming ################################
83
+
84
+ # 変数名3文字未満とか無理があるよ
85
+ # e.g. (_, db, x, y, e, i, n, ...)
86
+ Naming/MethodParameterName:
87
+ Enabled: false
88
+
89
+ ##################### RSpec ##################################
90
+
91
+ # RSpec の `context do` を許可するかどうか
92
+ # デフォルトだと `context '' do` にする必要があるが、ここは許可している
93
+ RSpec/MissingExampleGroupArgument:
94
+ Enabled: false
95
+
96
+ # `allow_any_instance_of` を許可するかどうか
97
+ # 仕方ない場合もあるので許可している
98
+ RSpec/AnyInstance:
99
+ Enabled: false
100
+
101
+ # `when` or `with` でしか context を書き始められなくする設定
102
+ # 日本語を使っている上では無理がある
103
+ RSpec/ContextWording:
104
+ Enabled: false
105
+
106
+ # `described_class.do_something` という書き方を強制するもの
107
+ # 直接クラス名を書いた方が (`MyClass.do_something`) 分かりやすいと思うのでオフにしている
108
+ RSpec/DescribedClass:
109
+ Enabled: false
110
+
111
+ # 使っていないテストケースがある都合で検出されている。対処はしたいが、一旦オフにしている
112
+ RSpec/EmptyExampleGroup:
113
+ Enabled: false
114
+
115
+ # 縦に長いテストを書かざるをえないこともあるので、オフにしている
116
+ RSpec/ExampleLength:
117
+ Enabled: false
118
+
119
+ # `change` などで Ruby のブロックのスタイルを使う
120
+ # この方が見やすいと話して決めた
121
+ RSpec/ExpectChange:
122
+ EnforcedStyle: block
123
+
124
+ RSpec/FilePath:
125
+ Enabled: false
126
+
127
+ # `let!` を許可している
128
+ RSpec/LetSetup:
129
+ Enabled: false
130
+
131
+ RSpec/MessageChain:
132
+ Enabled: false
133
+
134
+ RSpec/MessageSpies:
135
+ Enabled: false
136
+
137
+ # 一つの it の中に複数の expect を許可している
138
+ RSpec/MultipleExpectations:
139
+ Enabled: false
140
+
141
+ # subject に名前を付ける必要性を感じていない
142
+ # むしろテスト対象以外を subject に書かない方が良いのでは
143
+ RSpec/NamedSubject:
144
+ Enabled: false
145
+
146
+ # 細かく context を分ける必要があることもあるので緩めている
147
+ RSpec/NestedGroups:
148
+ Max: 5
149
+
150
+ # 現状だと Symbol で統一されているが、そちらのオプションがないため disable にしておく
151
+ RSpec/SharedExamples:
152
+ Enabled: false
153
+
154
+ RSpec/VerifiedDoubles:
155
+ Enabled: false
156
+
157
+ ##################### Style ##################################
158
+
159
+ # 日本語コメントを許可する
160
+ Style/AsciiComments:
161
+ Enabled: false
162
+
163
+ # 自動読み込みとコンフリクトすることがあるので、出来れば compact は避けたい
164
+ Style/ClassAndModuleChildren:
165
+ EnforcedStyle: nested
166
+
167
+ Style/ClassCheck:
168
+ EnforcedStyle: kind_of?
169
+
170
+ # 見やすさを優先したいこともあるので無効化
171
+ Style/ConditionalAssignment:
172
+ Enabled: false
173
+
174
+ # クラスの頭にドキュメンテーションコメントがあるかのチェック
175
+ # falseで任意に
176
+ Style/Documentation:
177
+ Enabled: false
178
+
179
+ # 書式付き文字列の指定、パーセント指定にした
180
+ Style/FormatString:
181
+ EnforcedStyle: percent
182
+
183
+ Style/FormatStringToken:
184
+ # 要ります?そんなに必要性を感じていない。この辺りは話してみたい
185
+ Enabled: false
186
+
187
+ # `# -*- frozen_string_literal: true -*-`的なコメントが無いよというエラー
188
+ Style/FrozenStringLiteralComment:
189
+ Enabled: false
190
+
191
+ Style/GuardClause:
192
+ MinBodyLength: 5
193
+
194
+ Style/IfUnlessModifier:
195
+ # 条件式を目立たせたい場合もあるので
196
+ Enabled: false
197
+
198
+ Style/Lambda:
199
+ EnforcedStyle: literal
200
+
201
+ Style/NumericLiteralPrefix:
202
+ # 見慣れない文字なので戸惑う
203
+ Enabled: false
204
+
205
+ # 桁数の大きい整数を_で区切るかどうか
206
+ Style/NumericLiterals:
207
+ MinDigits: 7
208
+ Strict: true
209
+
210
+ # Error.new(obj)を使いたいので無効化
211
+ Style/RaiseArgs:
212
+ Enabled: false
213
+
214
+ Style/RegexpLiteral:
215
+ Enabled: false
216
+
217
+ Style/RescueStandardError:
218
+ # さすがに StandardError は無指定でよいでしょう
219
+ EnforcedStyle: implicit
220
+
221
+ # 文字列はバッククォートかシングルクォートか
222
+ Style/StringLiterals:
223
+ EnforcedStyle: single_quotes
224
+
225
+ Style/SymbolArray:
226
+ # 場合によってはコードが分かりにくくなるので無効化
227
+ Enabled: false
228
+
229
+ # メソッド引数の末尾カンマをどうするか
230
+ Style/TrailingCommaInArguments:
231
+ EnforcedStyleForMultiline: consistent_comma
232
+
233
+ Style/TrailingCommaInArrayLiteral:
234
+ EnforcedStyleForMultiline: consistent_comma
235
+
236
+ Style/TrailingCommaInHashLiteral:
237
+ EnforcedStyleForMultiline: consistent_comma
238
+
239
+ # %w で囲むかどうかはどっちでも良いと思う。
240
+ Style/WordArray:
241
+ Enabled: false
data/.rubocop_todo.yml ADDED
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at wonderfulworld0315@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in file_downloader.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ file_downloader (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ast (2.4.2)
12
+ coderay (1.1.3)
13
+ crack (0.4.5)
14
+ rexml
15
+ diff-lcs (1.4.4)
16
+ hashdiff (1.0.1)
17
+ method_source (1.0.0)
18
+ parallel (1.20.1)
19
+ parser (3.0.2.0)
20
+ ast (~> 2.4.1)
21
+ pry (0.13.1)
22
+ coderay (~> 1.1)
23
+ method_source (~> 1.0)
24
+ public_suffix (4.0.6)
25
+ rainbow (3.0.0)
26
+ rake (13.0.6)
27
+ regexp_parser (2.1.1)
28
+ rexml (3.2.5)
29
+ rspec (3.10.0)
30
+ rspec-core (~> 3.10.0)
31
+ rspec-expectations (~> 3.10.0)
32
+ rspec-mocks (~> 3.10.0)
33
+ rspec-core (3.10.1)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-expectations (3.10.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.10.0)
38
+ rspec-mocks (3.10.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.10.0)
41
+ rspec-support (3.10.2)
42
+ rubocop (1.18.4)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.0.0.0)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml
48
+ rubocop-ast (>= 1.8.0, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 3.0)
51
+ rubocop-ast (1.8.0)
52
+ parser (>= 3.0.1.1)
53
+ rubocop-rspec (2.4.0)
54
+ rubocop (~> 1.0)
55
+ rubocop-ast (>= 1.1.0)
56
+ ruby-progressbar (1.11.0)
57
+ unicode-display_width (2.0.0)
58
+ webmock (3.13.0)
59
+ addressable (>= 2.3.6)
60
+ crack (>= 0.3.2)
61
+ hashdiff (>= 0.4.0, < 2.0.0)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ bundler
68
+ file_downloader!
69
+ pry
70
+ rake
71
+ rspec
72
+ rubocop
73
+ rubocop-rspec
74
+ webmock
75
+
76
+ BUNDLED WITH
77
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 sukechannnn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # FileDownloader
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/file_downloader`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'file_downloader'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install file_downloader
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/file_downloader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the FileDownloader project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/file_downloader/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "file_downloader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ DIR="tmp"
7
+ if [ ! -d $DIR ]; then
8
+ mkdir $DIR
9
+ fi
10
+ bundle install
11
+ bundle exec rspec
12
+
13
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'file_downloader/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'file_downloader'
7
+ spec.version = FileDownloader::VERSION
8
+ spec.authors = ['sukechannnn']
9
+ spec.email = ['wonderfulworld0315@gmail.com']
10
+
11
+ spec.summary = 'Complete downloading huge file even if connection interrupted few times.'
12
+ spec.description = 'Complete downloading huge file even if connection interrupted few times.'
13
+ spec.homepage = 'https://github.com/sukechannnn/file_downloader'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/sukechannnn/file_downloader'
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
24
+ 'public gem pushes.'
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = 'exe'
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ['lib']
35
+
36
+ spec.add_development_dependency 'bundler'
37
+ spec.add_development_dependency 'rake'
38
+ spec.add_development_dependency 'rspec'
39
+ spec.add_development_dependency 'rubocop'
40
+ spec.add_development_dependency 'rubocop-rspec'
41
+ spec.add_development_dependency 'webmock'
42
+ spec.add_development_dependency 'pry'
43
+ end
@@ -0,0 +1,8 @@
1
+ require 'file_downloader/version'
2
+ require 'file_downloader/service'
3
+
4
+ module FileDownloader
5
+ def self.download(url:, filepath:)
6
+ Service.new(url, filepath).execute
7
+ end
8
+ end
@@ -0,0 +1,64 @@
1
+ require 'file_downloader/status'
2
+ require 'logger'
3
+
4
+ module FileDownloader
5
+ class NotEofError < StandardError; end
6
+ class Service
7
+
8
+ def initialize(url, filepath, logger: nil)
9
+ @url = url
10
+ @filepath = filepath
11
+ @retry_count = 0
12
+ @logger = logger || Logger.new($stdout)
13
+ end
14
+
15
+ def execute
16
+ uri = URI.parse(url)
17
+ http = Net::HTTP.new(uri.host, uri.port)
18
+
19
+ content_length = fetch_content_length(http, uri)
20
+ download_file(http, uri, content_length)
21
+ filepath
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :url, :filepath, :logger
27
+
28
+ def fetch_content_length(http, uri)
29
+ http.use_ssl = true if uri.port == 443
30
+
31
+ res = http.head(uri.request_uri)
32
+ Status.check(res.code)
33
+ header = res.to_hash
34
+ header['content-length'][0]
35
+ end
36
+
37
+ def download_file(http, uri, content_length)
38
+ file = File.open(filepath, 'wb')
39
+
40
+ retry_on_error do
41
+ res = http.get(uri.request_uri, 'range' => "bytes=#{file.size}-#{content_length}") do |bytes|
42
+ file << bytes
43
+ end
44
+ Status.check(res.code)
45
+ raise NotEofError unless file.size == content_length.to_i
46
+ end
47
+ ensure
48
+ file&.close
49
+ end
50
+
51
+ def retry_on_error(times: 10)
52
+ @retry_count += 1
53
+ yield
54
+ rescue NotEofError
55
+ if @retry_count < times
56
+ logger.info "Connection closed: #{@retry_count} time retry"
57
+ retry
58
+ else
59
+ logger.error 'Connection closed'
60
+ raise
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,9 @@
1
+ class NotFoundError < StandardError; end
2
+
3
+ module Status
4
+ module_function
5
+
6
+ def check(code)
7
+ raise NotFoundError if code == '404'
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module FileDownloader
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_downloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sukechannnn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Complete downloading huge file even if connection interrupted few times.
112
+ email:
113
+ - wonderfulworld0315@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".rubocop_todo.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - file_downloader.gemspec
131
+ - lib/file_downloader.rb
132
+ - lib/file_downloader/service.rb
133
+ - lib/file_downloader/status.rb
134
+ - lib/file_downloader/version.rb
135
+ homepage: https://github.com/sukechannnn/file_downloader
136
+ licenses:
137
+ - MIT
138
+ metadata:
139
+ homepage_uri: https://github.com/sukechannnn/file_downloader
140
+ source_code_uri: https://github.com/sukechannnn/file_downloader
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubygems_version: 3.0.3
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Complete downloading huge file even if connection interrupted few times.
160
+ test_files: []