chakin-rb 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: 101e6f71bf9445e30473598577f9a34d769ef86b
4
- data.tar.gz: c7c00f11e8fda927e65f89320a50358b9412ed25
3
+ metadata.gz: b84417581a3b637fd8983910da40f5b606854c11
4
+ data.tar.gz: 34be171f2ae48889ff6a8656bfe8a125a4a647d7
5
5
  SHA512:
6
- metadata.gz: 3e3f3a0d0450c78d76173c6e934a94909b6b8c25286bf4124600a42f82f0c35672c2b01fb72dc57e4a625bbb327fb078ebc47c1e8952945a3e2c8870e53e238c
7
- data.tar.gz: bf4d328fb293a305fe28a21c9ec5f67fc3cad769adbb1c589f68d4705c5fafa837e6a998a7af4dcd0d6a354617bd73b1f00509bf6e4dce742a71109322c04f38
6
+ metadata.gz: 89ea491cc2ff13af0389e2f01d044ba2369b4bafa8e9e548c586d4494234cf16d083e13ea034c3ab4f75d1f9fac5acc376f769e4c32f1424301dc76e958b2e4c
7
+ data.tar.gz: b82efb6ccbd6c7aafb7fc638e9f7fbd959c64621854b512995b7ba2127a1d230859c9ef5e482f05e9b8ac299468bccd15fcbd80a2b7e9125bdbc0818fcf566aa
data/.gitignore CHANGED
@@ -9,5 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
-
12
+ *.gem
13
13
  Gemfile.lock
@@ -6,6 +6,12 @@ require 'net/http'
6
6
  require 'fileutils'
7
7
 
8
8
  module Chakin
9
+ class HttpRedirect < StandardError
10
+ attr_reader :new_url
11
+ def initialize(new_url)
12
+ @new_url = new_url
13
+ end
14
+ end
9
15
  class Error < StandardError; end
10
16
  module Downloader
11
17
  def load_datasets(path = File.join(__dir__, 'datasets.csv'))
@@ -26,33 +32,47 @@ module Chakin
26
32
  url = row['URL']
27
33
  raise 'The word vector you specified was not found. Please specify correct name.' if url.nil?
28
34
 
29
- progressbar = ProgressBar.create
35
+
30
36
  file_name = url.split('/')[-1]
31
37
 
32
38
  FileUtils.mkdir_p(save_dir) unless File.exist?(save_dir)
33
39
 
34
40
  save_path = File.join(save_dir, file_name)
41
+ begin
42
+ download_file(save_path, url)
43
+ rescue Chakin::HttpRedirect => e
44
+ download_file(save_path, e.new_url)
45
+ end
46
+ save_path
47
+ end
48
+
49
+ def download_file(save_path, url)
50
+ progressbar = ProgressBar.create
35
51
 
36
52
  f = File.open(save_path, 'wb')
37
53
  begin
38
54
  my_uri = URI.parse(url)
55
+ http = Net::HTTP.new(my_uri.host, my_uri.port)
39
56
 
40
- Net::HTTP.start(my_uri.host) do |http|
41
- http.request_get(my_uri.path) do |resp|
42
- total_size = resp.content_length
43
- progressbar.total = total_size
57
+ if my_uri.instance_of?(URI::HTTPS)
58
+ http.use_ssl = true
59
+ end
44
60
 
45
- resp.read_body do |segment|
46
- progressbar.progress += segment.size
47
- f.write(segment)
48
- end
61
+ http.request_get(my_uri.path) do |resp|
62
+ total_size = resp.content_length
63
+ progressbar.total = total_size
64
+
65
+ if resp.code == "302"
66
+ raise HttpRedirect.new(resp.header['Location'])
67
+ end
68
+ resp.read_body do |segment|
69
+ progressbar.progress += segment.size
70
+ f.write(segment)
49
71
  end
50
72
  end
51
73
  ensure
52
74
  f.close
53
75
  end
54
-
55
- save_path
56
76
  end
57
77
 
58
78
  def search(lang = '')
@@ -1,3 +1,3 @@
1
1
  module Chakin
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chakin-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo