bestchange-api 1.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: fefd2ae756cf40bfa5313e56c56bc473c0677dfc13873c5dea2a527ce901f551
4
- data.tar.gz: 1c031422cbff5f5f610c750b9dd1fdbcbfa0fbf6c578aa7d15efabb1282f1d1b
3
+ metadata.gz: 5dbecec983d7bc4299795c4ef489c5c63c6e90c33ef70144c8521bd2420fd067
4
+ data.tar.gz: 2e2196a4c4c458d42ec13dd530e423db8b0a9016636be8a1b074a5bfc771a395
5
5
  SHA512:
6
- metadata.gz: 2626dd57b231e3e8450b29805bbb58de6994842546c9b2b2dde3e6a3aef0e064a5f0d2c7eff5a98840afc3122b38afdf2ea6439195409292543b3e386ea8c13c
7
- data.tar.gz: b0bcf4bff9cce0413c69fdca33e90f0fd7c9a7949dc91633e561f5d6b2d18ad6ff958c5cc2463ca86b8109781a56249f00cf7a1d14dcda334053a30e44dc2649
6
+ metadata.gz: 61a22cb80ad3c8f6abec0da2dcca59f3a5829b0be7211b14e45e432196d6fc1c581b80420351d9dad1b2cff154a1755a67a3b92355be4d182f63096a6cd58bda
7
+ data.tar.gz: b837ec968c2959340e7363f7ea3c6f40f1c47e2b991f729df190fe67dc2d0d27fa6394b1f2933581a1055f810c7b1a81f00afbf6bd1b9d0587faff2436686e8a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change log
2
2
 
3
+ ## [1.1]
4
+ Fix tempfile location
5
+
6
+ ## [1.0]
7
+ Add new Api interface, introduce Zip objects
8
+
3
9
  ## [0.7]
4
10
  Minor improvements
5
11
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bestchange-api (0.7)
4
+ bestchange-api (1.0)
5
5
  rubyzip (= 2.3.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  # bestchange-api
2
3
 
3
4
  bestchange-api is a simple library for getting data from Bestchange aggregator
@@ -21,31 +22,61 @@ Or install it yourself as:
21
22
  ## Usage
22
23
  [Bestchange api description](https://github.com/karpinovsky/bestchange-api/blob/master/API_DOC.txt)
23
24
 
24
- Add your own configuration or use the default one
25
+ ### info.zip
25
26
 
26
27
  ```ruby
27
28
  require 'bestchange'
28
29
 
29
- Bestchange.configure do |config|
30
- config.dir = "path/to/your/custom/folder" # default is Dir.pwd
31
- config.timeout = 20 # Timeout for getting & parsing data, default is 40
32
- end
30
+ api_client = Bestchange::Api.new
31
+ response = api_client.request # this will call api.bestchange.ru/info.zip by default
32
+ archive = Bestchange::ZipBuilder.new(response.body).call
33
+ tempfile = Bestchange::ZipExtractor.new(archive).call('bm_rates.dat')
33
34
  ```
34
35
 
35
- Fetch data
36
+ **NOTE**: Do not forget to close and unlink files at the end
36
37
  ```ruby
37
- Bestchange::Api.get_files(['bm_rates.dat']) # => [File]
38
+ archive.close
39
+ tempfile.close
38
40
  ```
39
- ### &before_extract
40
- You can also pass a block that will be called before `Zip::Entry#extract`. It might be helpful if you need to remove existed files or change files destination for example
41
+
42
+ ### Using blocks
43
+ The `Bestchange::ZipBuilder` and `Bestchange::ZipExtractor` classes support using blocks to ensure that tempfiles are closed automatically. This is useful when you want to avoid manual file management and prevent possible leaks
41
44
  ```ruby
42
- before_extract = ->(_zip_entry, pathname) do
43
- pathname.delete if pathname.exist?
45
+ response = Bestchange::Api.new.request
46
+
47
+ Bestchange::ZipBuilder.new(response.body).call do |archive|
48
+ # Work with archive here
49
+ # archive will be closed automatically when the block finishes
50
+ Bestchange::ZipExtractor.new(archive).call('bm_rates.dat') do |bm_rates|
51
+ # Work with bm_rates here
52
+ # bm_rates will be closed automatically when the block finishes
53
+ end
44
54
  end
55
+ ```
56
+
57
+ ### Custom connection
58
+ You can pass your own http connection object while initializing an api client. This can be useful if you want to set custom timeouts, for example
59
+ ```ruby
60
+ require 'bestchange'
45
61
 
46
- Bestchange::Api.get_files(['bm_rates.dat'], &before_extract)
62
+ uri = URI(Bestchange::Api::BASE_URI)
63
+ conn = Net::HTTP.new(uri.host, uri.port)
64
+ conn.open_timeout = 5
65
+ conn.read_timeout = 10
66
+ api_client = Bestchange::Api.new(conn)
47
67
  ```
48
68
 
69
+ ### Custom request
70
+ You can provide your own http request object to the `#request` method
71
+ ```ruby
72
+ require 'bestchange'
73
+
74
+ request = Net::HTTP::Head.new(URI(Bestchange::Api::BASE_URI))
75
+ response = api_client.request(request)
76
+ created_at = response['Date']
77
+ ```
78
+
79
+
49
80
  ## License
50
81
 
51
82
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -17,11 +17,7 @@ module Bestchange
17
17
  attr_reader :conn
18
18
 
19
19
  def request(request = default_request)
20
- response = conn.request(request)
21
-
22
- return response unless block_given?
23
-
24
- yield response
20
+ conn.request(request)
25
21
  end
26
22
 
27
23
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bestchange
4
- VERSION = '1.0'
4
+ VERSION = '1.1'
5
5
  end
@@ -12,7 +12,16 @@ module Bestchange
12
12
  zip_file = Tempfile.new('', encoding: ARCHIVE_DATA_ENCODING)
13
13
  zip_file.write(@response_body)
14
14
  zip_file.rewind
15
- zip_file
15
+
16
+ if block_given?
17
+ begin
18
+ yield zip_file
19
+ ensure
20
+ zip_file.close
21
+ end
22
+ else
23
+ zip_file
24
+ end
16
25
  end
17
26
  end
18
27
  end
@@ -9,14 +9,23 @@ module Bestchange
9
9
  end
10
10
 
11
11
  def call(filename)
12
+ tempfile = Tempfile.new(filename)
13
+
12
14
  Zip::File.open(@zip_file) do |zip_file|
13
15
  entry = zip_file.find_entry(filename)
14
16
 
15
- tempfile = Tempfile.new(filename)
16
-
17
17
  entry.extract(tempfile) { true }
18
+ end
19
+
20
+ tempfile.rewind
18
21
 
19
- tempfile.rewind
22
+ if block_given?
23
+ begin
24
+ yield tempfile
25
+ ensure
26
+ tempfile.close
27
+ end
28
+ else
20
29
  tempfile
21
30
  end
22
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bestchange-api
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Karpinovsky