b2-client 1.0.0 → 1.0.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: c46280fcd18ce9df082fc3edfcddf837786817a52d70cd0e485584027807e4b8
4
- data.tar.gz: 58e53bc7bec6095f7e4a541a8e6ec4fa28994cabe933b9f397fc9c0d820b9470
3
+ metadata.gz: fd33ba10f4089d70caa9a3c857517300cc428a1589660bff8a99f87779bff547
4
+ data.tar.gz: 8a641aeaa454fb3313ef25b2eef2b8120b31874dbc0fb6afb9209bf5cfcabc66
5
5
  SHA512:
6
- metadata.gz: 9139847cc212553bc2d214711c1ae79db7065c0e7a456c62ae15e2908b25f6dafa2effaa72df915d3f6afb72a62f07c8b1c194bcaeabf665808000f472fa94e0
7
- data.tar.gz: 6c093acb1482a0bd61c949f68d02f11782b5c4bd5fdf816d7686074c3a30499f8e95318ffe77d011a1d7b6d500c085c2de070bc88680b3efbdbd38f1f76ee0fe
6
+ metadata.gz: 16103376d13143f6606848a41f2aa4fafa31afec786b4b86dfc1c71e823cdacd692b4aa4b514ae7a5bdde52925ad3cb21fe57cbe6a241d0b375b5aa7cbdfef6f
7
+ data.tar.gz: df8e92a10236f6b0f7ae5b303cf262d8d30ad2d605398445b6b47fe65d038b206832e4286ccd234eefb97b82d01e9144df1ad7bcbef1bf819873f827f40217b7
data/lib/b2.rb CHANGED
@@ -94,21 +94,10 @@ class B2
94
94
  @connection.download_url + '/file/' + bucket + '/' + filename + "?Authorization=" + response['authorizationToken']
95
95
  end
96
96
 
97
- def download(bucket, filename, &block)
98
- digestor = Digest::SHA1.new
99
- data = ""
100
-
101
- @connection.get("/file/#{bucket}/#{filename}") do |response|
102
- response.read_body do |chunk|
103
- digestor << chunk
104
- block.nil? ? (data << chunk) : block(chunk)
105
- end
106
- if digestor.hexdigest != resp['X-Bz-Content-Sha1']
107
- raise 'file error'
108
- end
109
- end
110
- block.nil? ? data : nil
97
+ def download(bucket, key, to=nil, &block)
98
+ @connection.download(bucket, key, to, &block)
111
99
  end
100
+
112
101
 
113
102
  def download_to_file(bucket, key, filename)
114
103
  file = File.open(filename, 'w')
@@ -68,33 +68,7 @@ class B2
68
68
  end
69
69
 
70
70
  def download(key, to=nil, &block)
71
- to = File.open(to, 'w') if to.is_a?(String)
72
- data = ""
73
- digestor = Digest::SHA1.new
74
-
75
- uri = URI.parse(@connection.download_url)
76
- conn = Net::HTTP.new(uri.host, uri.port)
77
- conn.use_ssl = uri.scheme == 'https'
78
-
79
- conn.get("/file/#{@name}/#{key}") do |response|
80
-
81
- response.read_body do |chunk|
82
- digestor << chunk
83
- if to
84
- to << chunk
85
- elsif block
86
- block(chunk)
87
- else
88
- data << chunk
89
- end
90
- end
91
-
92
- if digestor.hexdigest != resp['X-Bz-Content-Sha1']
93
- raise 'file error'
94
- end
95
-
96
- end
97
- block.nil? && to.nil? ? data : nil
71
+ @connection.download(@name, key, to, &block)
98
72
  end
99
73
 
100
74
  def delete!(key)
@@ -83,6 +83,44 @@ class B2
83
83
  return_value
84
84
  end
85
85
 
86
+ def download(bucket, key, to=nil, &block)
87
+ to = ::File.open(to, 'wb') if to.is_a?(String)
88
+ digestor = Digest::SHA1.new
89
+ data = ""
90
+
91
+ uri = URI.parse(@download_url)
92
+ conn = Net::HTTP.new(uri.host, uri.port)
93
+ conn.use_ssl = uri.scheme == 'https'
94
+
95
+ req = Net::HTTP::Get.new("/file/#{bucket}/#{key}")
96
+ req['Authorization'] = authorization_token
97
+ conn.start do |http|
98
+ http.request(req) do |response|
99
+ case response
100
+ when Net::HTTPSuccess
101
+ response.read_body do |chunk|
102
+ digestor << chunk
103
+ if to
104
+ to << chunk
105
+ elsif block
106
+ block(chunk)
107
+ else
108
+ data << chunk
109
+ end
110
+ end
111
+
112
+ if digestor.hexdigest != response['X-Bz-Content-Sha1']
113
+ raise 'file error'
114
+ end
115
+ else
116
+ raise response.body
117
+ end
118
+ end
119
+ end
120
+
121
+ block.nil? && to.nil? ? data : nil
122
+ end
123
+
86
124
  def get(path, body=nil, &block)
87
125
  request = Net::HTTP::Get.new(path)
88
126
 
@@ -6,10 +6,10 @@ class B2
6
6
  @data = data
7
7
  @sha_appended = false
8
8
  @digestor = Digest::SHA1.new
9
- @size = if data.is_a?(::File)
10
- data.size + 40
11
- elsif data.is_a?(String)
9
+ @size = if data.is_a?(String)
12
10
  data.bytesize + 40
11
+ else
12
+ data.size + 40
13
13
  end
14
14
  end
15
15
 
@@ -1,3 +1,3 @@
1
1
  module B2
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: b2-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-12 00:00:00.000000000 Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake