http_downloader 0.9.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 +7 -0
- data/lib/http_downloader.rb +15 -0
- data/lib/http_downloader/download_request.rb +40 -0
- data/lib/http_downloader/download_response.rb +25 -0
- data/lib/http_downloader/http/request.rb +39 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3b6d8c681b5dc8be8e6852bcc6ba61a71ac0de67
|
4
|
+
data.tar.gz: 9ee34d7fc2b194402beb1e02df30a06e15895e39
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00b5e02ec67753e13ec10499325054516aa40cae9c0b044d6d5a2fb735024ff538c8acd28b4b2a64f9238a0d4cc70eba7421e5f65f19ac9ac8733c91296e4fcb
|
7
|
+
data.tar.gz: 348a6a0ea40d0b5039b335f1c508008fd2616e089eb6309b446a7830ae3dc668ce856a4655a71cc3152716c28fcde79386ea12667e2af906418ca187fa876917
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'openssl'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
require 'http_downloader/http/request'
|
6
|
+
require 'http_downloader/download_request'
|
7
|
+
require 'http_downloader/download_response'
|
8
|
+
|
9
|
+
module HttpDownloader
|
10
|
+
|
11
|
+
def self.download(url, headers = {}, max_redirects = 5, http_options = {})
|
12
|
+
DownloadRequest.new(url, headers, max_redirects, http_options).execute
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module HttpDownloader
|
2
|
+
class DownloadRequest
|
3
|
+
|
4
|
+
def initialize(url, headers = {}, max_redirects = 5, http_options)
|
5
|
+
@uri = URI.parse(url)
|
6
|
+
@headers = headers
|
7
|
+
@max_redirects = max_redirects
|
8
|
+
@http_options = http_options
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute
|
12
|
+
DownloadResponse.new(http_request(@uri))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def http_request(uri)
|
18
|
+
response = Http::Request.new(uri, @headers, @http_options).execute
|
19
|
+
case response
|
20
|
+
when Net::HTTPSuccess
|
21
|
+
response
|
22
|
+
when Net::HTTPRedirection
|
23
|
+
follow_redirect(response)
|
24
|
+
else
|
25
|
+
response.error!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def follow_redirect(response)
|
30
|
+
@max_redirects -= 1
|
31
|
+
http_request(uri_from_location_header(response))
|
32
|
+
end
|
33
|
+
|
34
|
+
def uri_from_location_header(response)
|
35
|
+
uri = URI.parse(response['location'])
|
36
|
+
uri.relative? ? URI.parse("#{response.uri.scheme}://#{response.uri.host}:#{response.uri.port}").merge(uri) : uri
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module HttpDownloader
|
2
|
+
class DownloadResponse
|
3
|
+
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
end
|
7
|
+
|
8
|
+
def read_body(content_types = [])
|
9
|
+
if allowed_content_type?(content_types)
|
10
|
+
block_given? ? @response.read_body { |chunk| yield(chunk) } : @response.read_body
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def allowed_content_type?(content_types)
|
17
|
+
content_types.empty? || @response['content-type'].starts_with_any?(content_types)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module HttpDownloader
|
2
|
+
module Http
|
3
|
+
class Request
|
4
|
+
HTTP = 'http'
|
5
|
+
HTTPS = 'https'
|
6
|
+
|
7
|
+
def initialize(uri, headers = {}, http_options = {})
|
8
|
+
@uri = uri
|
9
|
+
@headers = headers
|
10
|
+
@http_options = http_options
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
response = nil
|
15
|
+
byebug
|
16
|
+
Net::HTTP.start(@uri.host, @uri.port, http_options(@uri)) do |http|
|
17
|
+
request = Net::HTTP::Get.new(@uri, @headers)
|
18
|
+
http.request(request) do |http_response|
|
19
|
+
response = http_response
|
20
|
+
end
|
21
|
+
end
|
22
|
+
response
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def http_options(uri)
|
28
|
+
{ :open_timeout => 60,
|
29
|
+
:read_timout => 60,
|
30
|
+
:use_ssl => https?(uri) }.merge(@http_options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def https?(uri)
|
34
|
+
!!(uri.scheme && uri.scheme.downcase == HTTPS)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_downloader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc Marcet
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http_url
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
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: byebug
|
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
|
+
description: A simple and lightweight library to download content over HTTP. Supports
|
42
|
+
http redirects and chunked downloads.
|
43
|
+
email: marcmr87@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/http_downloader.rb
|
49
|
+
- lib/http_downloader/download_request.rb
|
50
|
+
- lib/http_downloader/download_response.rb
|
51
|
+
- lib/http_downloader/http/request.rb
|
52
|
+
homepage: https://github.com/marcmr87/http_downloader
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.9.3
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.2.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: A simple and lightweight library to download content over HTTP. Supports
|
76
|
+
http redirects and chunked downloads.
|
77
|
+
test_files: []
|