pulse-downloader 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82910e4a2a00c0519958083ae3380241bb4c0cd3166d513d311c338227e4184f
4
- data.tar.gz: c7642a1d110693f56ce933a466312fe851647ede790cba74a18c8157137c2a7e
3
+ metadata.gz: bee880729f0af4203d680eb78a1c3f0375137f14b4647e707ae6d4445e0cdd21
4
+ data.tar.gz: 504e7020e0af6291d7b39ba06f03fd40b23baae1c39263a5e0906982fdb38212
5
5
  SHA512:
6
- metadata.gz: 1ec8ec9d18dabd67e9c7e01abf5718206512e9169b2cc38c4ec2654b1ae289d7955945995ea55fda637c7a6363f8f053baa681a6ba463c1b6f3a2519bf1b714c
7
- data.tar.gz: 37dc7aa8612c40f696c6902e3e791811ac801106a1c45243ba5597f73bbecd7198b077d8d37340a7e010c995252c092ea3f8a869498cac0844762d5d72412ab3
6
+ metadata.gz: e32fb830552f65170cdf26a2a13789cb058843df064ef2da2f393ad91cb8e5b06c741885bd7d6d665021bb2eb17ef7157ade02cd03b9218bfe4f753a798100d9
7
+ data.tar.gz: 39186157cbe4cff6c91e7a9d9b2424af1720563f18c9ca4ceb438e1bbe854de743b1677525cc57495393a080b72bfcd2e5aeceb78f37bd0f0efb5bcdfd0a755e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pulse-downloader (0.1.0)
4
+ pulse-downloader (0.1.1)
5
5
  active_attr (~> 0.15)
6
6
  httparty (~> 0.18)
7
7
  nokogiri (~> 1.10.9)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Pulse::Downloader
2
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/pulse/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
3
+ This is a library to download a specific group of files linked to on an html page.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,19 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ```ruby
24
+ require 'pulse/downloader'
25
+
26
+ client = Pulse::Downloader::Client.new(
27
+ url: '',
28
+ file_type: 'zip',
29
+ save_data: true,
30
+ save_path: '',
31
+ read_from_save_path: false,
32
+ verify_ssl: true,
33
+ report_time: false
34
+ )
35
+ ```
26
36
 
27
37
  ## Development
28
38
 
@@ -34,7 +44,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
34
44
 
35
45
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pulse-downloader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/pulse-downloader/blob/master/CODE_OF_CONDUCT.md).
36
46
 
37
-
38
47
  ## License
39
48
 
40
49
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -4,14 +4,27 @@ module Pulse
4
4
  include ::Pulse::Downloader::WebPageParser
5
5
  include ::Pulse::Downloader::FileDownloader
6
6
 
7
- attr_reader :path, :file_type, :save_data, :save_path, :read_from_save_path
8
-
9
- def initialize(path:, file_type:, save_data: false, save_path: '', read_from_save_path: false)
10
- @path = path
7
+ attr_reader :url,
8
+ :file_type,
9
+ :save_data,
10
+ :save_path,
11
+ :read_from_save_path,
12
+ :verify_ssl,
13
+ :report_time,
14
+ :start_time,
15
+ :end_time
16
+
17
+ # TODO: Validation
18
+ # TODO: Retry
19
+ # TODO: DNS
20
+ def initialize(url:, file_type:, save_data: false, save_path: '', read_from_save_path: false, verify_ssl: true, report_time: false)
21
+ @url = url
11
22
  @file_type = file_type
12
23
  @save_data = save_data
13
24
  @save_path = save_path
14
25
  @read_from_save_path = read_from_save_path
26
+ @verify_ssl = verify_ssl
27
+ @report_time = report_time
15
28
  end
16
29
 
17
30
  def call!
@@ -33,11 +46,11 @@ module Pulse
33
46
  private
34
47
 
35
48
  def get_micro_second_time
36
- (Time.now.to_f * 1000000).to_i
49
+ (Time.now.to_f * 1000).to_i
37
50
  end
38
51
 
39
- def compute_filename(file_path)
40
- file_path.scan(/[\/]\S+/).last
52
+ def print_time
53
+ puts "Request time: #{end_time - start_time} ms."
41
54
  end
42
55
  end
43
56
  end
@@ -1,19 +1,22 @@
1
1
  module Pulse
2
2
  module Downloader
3
3
  module FileDownloader
4
- # save_path are defined in client.rb
4
+ # save_path and verify_ssl are defined in client.rb
5
5
  def download(file_path)
6
6
  raise "save_path is undefined" if save_data && save_path == ''
7
7
 
8
- start_time = get_micro_second_time
8
+ @start_time = get_micro_second_time
9
9
 
10
- file_data = HTTParty.get(file_path)
10
+ file_data = HTTParty.get(compute_file_link(file_path), verify: verify_ssl)
11
11
 
12
- # TODO: Use the time
13
- end_time = get_micro_second_time
12
+ @end_time = get_micro_second_time
13
+
14
+ if report_time
15
+ print_time
16
+ end
14
17
 
15
18
  if save_data
16
- File.open("#{save_path}/#{compute_filename(file_path)}", 'wb') do |file|
19
+ File.open(compute_save_path(file_path), 'wb') do |file|
17
20
  file.write(file_data.body)
18
21
  end
19
22
  end
@@ -21,9 +24,33 @@ module Pulse
21
24
  file_data
22
25
  end
23
26
 
27
+ def fetch_save_paths
28
+ fetch_file_paths.map do |file_path|
29
+ "#{save_path}/#{compute_filename(file_path)}"
30
+ end
31
+ end
32
+
24
33
  def compute_hash_of(data)
25
34
  { data: data }.hash
26
35
  end
36
+
37
+ private
38
+
39
+ def compute_save_path(url)
40
+ "#{save_path}/#{compute_filename(url)}"
41
+ end
42
+
43
+ def compute_filename(file_path)
44
+ file_path.scan(/[\/]\S+/).last
45
+ end
46
+
47
+ def compute_file_link(file_path)
48
+ if file_path[0] == '/'
49
+ "#{url}/#{file_path}"
50
+ else
51
+ file_path
52
+ end
53
+ end
27
54
  end
28
55
  end
29
56
  end
@@ -1,5 +1,5 @@
1
1
  module Pulse
2
2
  module Downloader
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -2,24 +2,30 @@ module Pulse
2
2
  module Downloader
3
3
  module WebPageParser
4
4
  def fetch_file_paths
5
- start_time = get_micro_second_time
5
+ @start_time = get_micro_second_time
6
6
 
7
- response = HTTParty.get(@path)
7
+ response = HTTParty.get(url, verify: verify_ssl)
8
8
 
9
- # TODO: Use the time
10
- end_time = get_micro_second_time
11
- extract_file_urls(response, start_time, end_time)
9
+ @end_time = get_micro_second_time
10
+
11
+ if report_time
12
+ print_time
13
+ end
14
+
15
+ extract_file_urls(response)
12
16
  end
13
17
 
14
18
  private
15
19
 
16
- def extract_file_urls(response, start_time, end_time)
17
- parse_html(response)
20
+ def extract_file_urls(response)
21
+ return [] if response.body.nil? || response.body.empty?
22
+
23
+ parse_html(response.body)
18
24
  .css('a')
19
25
  .to_a
20
26
  .map { |link| link['href'] }
21
27
  .compact
22
- .select { |link| link.include? @file_type }
28
+ .select { |link| link.include? file_type }
23
29
  end
24
30
 
25
31
  def parse_html(raw_html)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pulse-downloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - trex22