bestchange-api 0.6 → 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: f8da60c9f91d2fac5ff9899af64181c5715b668baa92dd5789dfcf508a92a286
4
- data.tar.gz: c8259d19fd314094146ad62d60507ca4eb3279af5d481105fb6e57aac929dd57
3
+ metadata.gz: fefd2ae756cf40bfa5313e56c56bc473c0677dfc13873c5dea2a527ce901f551
4
+ data.tar.gz: 1c031422cbff5f5f610c750b9dd1fdbcbfa0fbf6c578aa7d15efabb1282f1d1b
5
5
  SHA512:
6
- metadata.gz: 3fc1081e2b6e436b0fc1659dae92a3c48396d51ee562310d6a0433979a576e173c7025fa19aed0b3941a020e889a26f4beef94b13c68af6cad27ef43bcee3b50
7
- data.tar.gz: 5c891ca4e54319a4a542472daea41fcf00d349f5c4e130854e9650259029c04f31a1ca375ccb9d2fab1156dd1c114235e166db240fca0eae8726bfc58601dcdb
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,5 +1,8 @@
1
1
  # Change log
2
2
 
3
+ ## [0.7]
4
+ Minor improvements
5
+
3
6
  ## [0.6]
4
7
  Pass zip entry via before_extract block
5
8
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bestchange-api (0.5)
4
+ bestchange-api (0.7)
5
5
  rubyzip (= 2.3.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  # bestchange-api
3
2
 
4
3
  bestchange-api is a simple library for getting data from Bestchange aggregator
@@ -7,46 +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
20
-
21
- def fetch_files(filenames, &before_extract)
22
- response = make_request
17
+ attr_reader :conn
23
18
 
24
- archive = Tempfile.new(ARCHIVE_DATA_FILENAME, encoding: ARCHIVE_DATA_ENCODING)
25
- archive.write(response)
19
+ def request(request = default_request)
20
+ response = conn.request(request)
26
21
 
27
- extract_files('info.zip', filenames, &before_extract)
28
- ensure
29
- archive.close if defined?(archive)
30
- end
22
+ return response unless block_given?
31
23
 
32
- def make_request
33
- Net::HTTP.get(URI(BASE_URI))
24
+ yield response
34
25
  end
35
26
 
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)
40
-
41
- pathname = Pathname.new(Bestchange.configuration.dir).join(filename)
42
-
43
- before_extract&.call(entry, pathname)
27
+ private
44
28
 
45
- zip_file.extract(entry, pathname)
29
+ def default_conn
30
+ Net::HTTP.new(URI(BASE_URI).host, URI(BASE_URI).port)
31
+ end
46
32
 
47
- pathname.open
48
- end
49
- end
33
+ def default_request
34
+ Net::HTTP::Get.new(URI(BASE_URI))
50
35
  end
51
36
  end
52
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bestchange
4
- VERSION = '0.6'
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.6'
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