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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +43 -12
- data/lib/bestchange/api.rb +1 -5
- data/lib/bestchange/version.rb +1 -1
- data/lib/bestchange/zip_builder.rb +10 -1
- data/lib/bestchange/zip_extractor.rb +12 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dbecec983d7bc4299795c4ef489c5c63c6e90c33ef70144c8521bd2420fd067
|
4
|
+
data.tar.gz: 2e2196a4c4c458d42ec13dd530e423db8b0a9016636be8a1b074a5bfc771a395
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61a22cb80ad3c8f6abec0da2dcca59f3a5829b0be7211b14e45e432196d6fc1c581b80420351d9dad1b2cff154a1755a67a3b92355be4d182f63096a6cd58bda
|
7
|
+
data.tar.gz: b837ec968c2959340e7363f7ea3c6f40f1c47e2b991f729df190fe67dc2d0d27fa6394b1f2933581a1055f810c7b1a81f00afbf6bd1b9d0587faff2436686e8a
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|
-
|
25
|
+
### info.zip
|
25
26
|
|
26
27
|
```ruby
|
27
28
|
require 'bestchange'
|
28
29
|
|
29
|
-
Bestchange.
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
36
|
+
**NOTE**: Do not forget to close and unlink files at the end
|
36
37
|
```ruby
|
37
|
-
|
38
|
+
archive.close
|
39
|
+
tempfile.close
|
38
40
|
```
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
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
|
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).
|
data/lib/bestchange/api.rb
CHANGED
data/lib/bestchange/version.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|