bestchange-api 0.7 → 1.0

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: a4519573ffcd91610bf68a30544ebd17c00506ab8c052b5412b74ff348f07d8a
4
- data.tar.gz: c6e1e36c2ed0d7c35a374e00576c8c5de2f433e86edb986a0ceaf5e17a04e167
3
+ metadata.gz: fefd2ae756cf40bfa5313e56c56bc473c0677dfc13873c5dea2a527ce901f551
4
+ data.tar.gz: 1c031422cbff5f5f610c750b9dd1fdbcbfa0fbf6c578aa7d15efabb1282f1d1b
5
5
  SHA512:
6
- metadata.gz: 8677e25eb2333d11e437e0b06eb64bcc122994b12fba0b81693e10dee37a6f31ce71d47a3f124929dafd865d72280db7e37df2c3109c7f0c4d2231c7b7d89c84
7
- data.tar.gz: 5459ab729ad2777e2981b8ba1b55fd37a88a8720d85ddbf18167dcb2587c095b0c5ba6e85f1a2583cad53446545ad569e77fcbc53491f71134cc53d867285e02
6
+ metadata.gz: 2626dd57b231e3e8450b29805bbb58de6994842546c9b2b2dde3e6a3aef0e064a5f0d2c7eff5a98840afc3122b38afdf2ea6439195409292543b3e386ea8c13c
7
+ data.tar.gz: b0bcf4bff9cce0413c69fdca33e90f0fd7c9a7949dc91633e561f5d6b2d18ad6ff958c5cc2463ca86b8109781a56249f00cf7a1d14dcda334053a30e44dc2649
data/.rubocop.yml CHANGED
@@ -1,3 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1.3
1
3
  Style/Documentation:
2
4
  Enabled: false
3
5
  Metrics/MethodLength:
data/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Change log
2
2
 
3
3
  ## [0.7]
4
- Small improvements
4
+ Minor improvements
5
5
 
6
6
  ## [0.6]
7
7
  Pass zip entry via before_extract block
@@ -7,49 +7,31 @@ require 'timeout'
7
7
  require 'zip'
8
8
 
9
9
  module Bestchange
10
- module Api
11
- extend self
10
+ class Api
11
+ BASE_URI = 'http://api.bestchange.ru/info.zip'
12
12
 
13
- def get_files(filenames, &before_extract)
14
- Timeout.timeout(Bestchange.configuration.timeout) do
15
- fetch_files(filenames, &before_extract)
16
- end
13
+ def initialize(conn = default_conn)
14
+ @conn = conn
17
15
  end
18
16
 
19
- private
17
+ attr_reader :conn
20
18
 
21
- def fetch_files(filenames, &before_extract)
22
- response = make_request
23
- archive = make_archive(response)
24
- extract_files(archive, filenames, &before_extract)
25
- ensure
26
- archive.close if defined?(archive)
27
- end
19
+ def request(request = default_request)
20
+ response = conn.request(request)
28
21
 
29
- def make_request
30
- Net::HTTP.get(URI(BASE_URI))
31
- end
22
+ return response unless block_given?
32
23
 
33
- def make_archive(response)
34
- archive = Tempfile.new(ARCHIVE_DATA_FILENAME, encoding: ARCHIVE_DATA_ENCODING)
35
- archive.write(response)
36
- archive
24
+ yield response
37
25
  end
38
26
 
39
- def extract_files(archive, filenames, &before_extract)
40
- Zip::File.open(archive) do |zip_file|
41
- filenames.map do |filename|
42
- entry = zip_file.find_entry(filename)
43
-
44
- pathname = Pathname.new(Bestchange.configuration.dir).join(filename)
45
-
46
- before_extract&.call(entry, pathname)
27
+ private
47
28
 
48
- zip_file.extract(entry, pathname)
29
+ def default_conn
30
+ Net::HTTP.new(URI(BASE_URI).host, URI(BASE_URI).port)
31
+ end
49
32
 
50
- pathname.open
51
- end
52
- end
33
+ def default_request
34
+ Net::HTTP::Get.new(URI(BASE_URI))
53
35
  end
54
36
  end
55
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bestchange
4
- VERSION = '0.7'
4
+ VERSION = '1.0'
5
5
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bestchange
4
+ class ZipBuilder
5
+ ARCHIVE_DATA_ENCODING = 'ASCII-8BIT'
6
+
7
+ def initialize(response_body)
8
+ @response_body = response_body
9
+ end
10
+
11
+ def call
12
+ zip_file = Tempfile.new('', encoding: ARCHIVE_DATA_ENCODING)
13
+ zip_file.write(@response_body)
14
+ zip_file.rewind
15
+ zip_file
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zip'
4
+
5
+ module Bestchange
6
+ class ZipExtractor
7
+ def initialize(zip_file)
8
+ @zip_file = zip_file
9
+ end
10
+
11
+ def call(filename)
12
+ Zip::File.open(@zip_file) do |zip_file|
13
+ entry = zip_file.find_entry(filename)
14
+
15
+ tempfile = Tempfile.new(filename)
16
+
17
+ entry.extract(tempfile) { true }
18
+
19
+ tempfile.rewind
20
+ tempfile
21
+ end
22
+ end
23
+ end
24
+ end
data/lib/bestchange.rb CHANGED
@@ -1,24 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'bestchange/api'
4
- require_relative 'bestchange/configuration'
4
+ require_relative 'bestchange/zip_builder'
5
+ require_relative 'bestchange/zip_extractor'
5
6
 
6
7
  module Bestchange
7
- ARCHIVE_DATA_FILENAME = 'info.zip'
8
- ARCHIVE_DATA_ENCODING = 'ASCII-8BIT'
9
-
10
- BASE_URI = "http://api.bestchange.ru/#{ARCHIVE_DATA_FILENAME}".freeze
11
-
12
- DIRECTORY = Dir.pwd
13
- TIMEOUT = 40
14
-
15
- def self.configuration
16
- @configuration ||= Configuration.new
17
- end
18
-
19
- def self.configure
20
- return configuration unless block_given?
21
-
22
- yield(configuration)
23
- end
24
8
  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.7'
4
+ version: '1.0'
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-17 00:00:00.000000000 Z
11
+ date: 2023-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -89,8 +89,9 @@ files:
89
89
  - bin/setup
90
90
  - lib/bestchange.rb
91
91
  - lib/bestchange/api.rb
92
- - lib/bestchange/configuration.rb
93
92
  - lib/bestchange/version.rb
93
+ - lib/bestchange/zip_builder.rb
94
+ - lib/bestchange/zip_extractor.rb
94
95
  homepage: https://github.com/karpinovsky/bestchange-api
95
96
  licenses:
96
97
  - MIT
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bestchange
4
- class Configuration
5
- def dir
6
- @dir || Bestchange::DIRECTORY
7
- end
8
-
9
- def timeout
10
- @timeout || Bestchange::TIMEOUT
11
- end
12
-
13
- attr_writer :dir, :timeout
14
- end
15
- end