bestchange-api 0.3 → 0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04f8a40e380db40689c09a837ee11046dc2db89aa57b4cb35e7f7f2a1491a1c3
4
- data.tar.gz: 71e0dfc4d65b38a3ce17c5a680a559e43b2370683eb31b1cb865feef921fe81a
3
+ metadata.gz: b3e48dd22229d0446f26b047fd38fee4a9824be55e2981b7bcfeadb49ca4f044
4
+ data.tar.gz: a098a7884b4b9668974acabc2f73df5837ad54b671c40dec14dc147e23e6d1aa
5
5
  SHA512:
6
- metadata.gz: 431c8546f41f151ae5c087ca373759e4f535921a951171b023c6a52e553d94a7a71ceea0ce28162971f758e90c1c48077fe57f16d780d7a0df6d6558577d6b72
7
- data.tar.gz: f2804fcd70e28c4ff26bc19a70651fc84d47d31d938b0e40fe1a4cc8bd45c3b15a381d531b93241c2dec1974578f8996621e2b4c349007d9839cb094f2b82fc4
6
+ metadata.gz: 3d574c842366cc7429dc2f197e34767996d265eed053eae2a9f64f00bc5bbd1743bfde48da456e7501473b85acd467378c2223b766db9e11d509805d9994f075
7
+ data.tar.gz: bd27f374b87e3e18b74e41d952e33e5eb740748d9ae7344dd1895538e477dfd6c5c9cd4f931c8c827d6dd284aa85928c9a96c29426505e91784d50e0e268487d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change log
2
2
 
3
+ ## [0.5]
4
+ Add before_extract block
5
+
6
+ ## [0.4]
7
+ Do not delete already existed files
8
+
3
9
  ## [0.3]
4
10
  Fix configuration access
5
11
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bestchange-api (0.3)
4
+ bestchange-api (0.4)
5
5
  rubyzip (= 2.3.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -23,6 +23,8 @@ Or install it yourself as:
23
23
  Add your own configuration or use the default one
24
24
 
25
25
  ```ruby
26
+ require 'bestchange'
27
+
26
28
  Bestchange.configure do |config|
27
29
  config.dir = "path/to/your/custom/folder" # default is Dir.pwd
28
30
  config.timeout = 20 # Timeout for getting & parsing data, default is 40
@@ -7,36 +7,46 @@ require 'timeout'
7
7
  require 'zip'
8
8
 
9
9
  module Bestchange
10
- class Api
11
- def self.get_files(filenames)
12
- pathnames = filenames.map do |filename|
13
- Pathname.new(Bestchange.configuration.dir).join(filename)
10
+ module Api
11
+ extend self
12
+
13
+ def get_files(filenames, &before_extract)
14
+ Timeout.timeout(Bestchange.configuration.timeout) do
15
+ fetch_files(filenames, &before_extract)
14
16
  end
17
+ end
15
18
 
16
- Timeout.timeout(Bestchange.configuration.timeout) { fetch_data(pathnames) }
19
+ private
17
20
 
18
- pathnames.map do |pathname|
19
- File.open(pathname)
20
- end
21
+ def fetch_files(filenames, &before_extract)
22
+ response = make_request
23
+
24
+ archive = Tempfile.new(ARCHIVE_DATA_FILENAME, encoding: ARCHIVE_DATA_ENCODING)
25
+ archive.write(response)
26
+
27
+ extract_files('info.zip', filenames, &before_extract)
28
+ ensure
29
+ archive.close if defined?(archive)
21
30
  end
22
31
 
23
- def self.fetch_data(pathnames)
24
- response = Net::HTTP.get(URI(BASE_URI))
32
+ def make_request
33
+ Net::HTTP.get(URI(BASE_URI))
34
+ end
25
35
 
26
- temp_file = Tempfile.new(ARCHIVE_DATA_FILENAME, encoding: ARCHIVE_DATA_ENCODING)
27
- temp_file.write(response)
36
+ def extract_files(archive, filenames, &before_extract)
37
+ Zip::File.open(archive) do |zip_file|
38
+ filenames.map do |filename|
39
+ entry = zip_file.find_entry(filename)
28
40
 
29
- Zip::File.open(temp_file) do |zip_file|
30
- pathnames.each do |pathname|
31
- entry = zip_file.find_entry(pathname.basename)
41
+ pathname = Pathname.new(Bestchange.configuration.dir).join(filename)
32
42
 
33
- pathname.delete if pathname.exist?
43
+ before_extract&.call(filename, pathname)
34
44
 
35
45
  zip_file.extract(entry, pathname)
46
+
47
+ pathname.open
36
48
  end
37
49
  end
38
-
39
- temp_file.close
40
50
  end
41
51
  end
42
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bestchange
4
- VERSION = '0.3'
4
+ VERSION = '0.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bestchange-api
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Karpinovsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-05 00:00:00.000000000 Z
11
+ date: 2023-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip