b2 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +6 -0
- data/b2.gemspec +28 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/b2.rb +18 -0
- data/lib/b2/authentication.rb +16 -0
- data/lib/b2/base.rb +18 -0
- data/lib/b2/bucket.rb +90 -0
- data/lib/b2/doc.rb +76 -0
- data/lib/b2/version.rb +3 -0
- data/lib/helpers/configuration.rb +22 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d28af996d66702191e695e912e898c0565cb8f5a
|
4
|
+
data.tar.gz: 9f13f2f09700c1c03754f74435f0467af99bbf24
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d88651620eab4977aae261c62bc5e71d161c0994da1c506c815320fa2d372956c5ca89e676a10a8f33a48b4a36e121d526b8774677213830648a094376ce2ae
|
7
|
+
data.tar.gz: df09e71cdd402b6d6269693af3d341c19d84969e1f41e74dab6e9d6d9346bbe09b13c9cfd823ba7787a66f1bcc8fcc01eb9b8bb9b353bf7aee77fbf9d259cf3c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Muhammad Bayu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# B2
|
2
|
+
|
3
|
+
Ruby Wrapper for Backblaze B2 Api
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'b2'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install b2
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Write your api key in config/initializers/b2.rb
|
24
|
+
```ruby
|
25
|
+
B2.configuration do |config|
|
26
|
+
config.application_key = YOUR-APPLICATION-KEY
|
27
|
+
config.account_id = YOUR-ACCOUNT-ID
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
## To-Do
|
32
|
+
* More doc for api call
|
33
|
+
* integrate with carrierwave
|
34
|
+
* Test Unit
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/metalkoholic/b2.
|
39
|
+
|
40
|
+
1. Fork it ( https://github.com/metalkoholic/b2/fork )
|
41
|
+
2. Create your feature branch (`git checkout -b new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin new-feature`)
|
44
|
+
5. Create a new Pull Request
|
45
|
+
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
50
|
+
|
data/Rakefile
ADDED
data/b2.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'b2/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "b2"
|
8
|
+
spec.version = B2::VERSION
|
9
|
+
spec.authors = ["Muhammad Bayu"]
|
10
|
+
spec.email = ["metalkoholic@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ruby wrapper for backblaze b2 API}
|
13
|
+
spec.homepage = "https://github.com/metalkoholic/b2"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "httparty", "~> 0.13"
|
27
|
+
spec.required_ruby_version = '>= 2.1.0'
|
28
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "b2"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/b2.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require 'helpers/configuration'
|
3
|
+
|
4
|
+
require "b2/version"
|
5
|
+
require "b2/authentication"
|
6
|
+
require "b2/base"
|
7
|
+
require "b2/bucket"
|
8
|
+
require "b2/doc"
|
9
|
+
|
10
|
+
BASE_URI = 'https://api.backblaze.com/b2api/v1/'
|
11
|
+
BASE_PATH = '/b2api/v1/'
|
12
|
+
|
13
|
+
module B2
|
14
|
+
extend Configuration
|
15
|
+
|
16
|
+
define_setting :account_id
|
17
|
+
define_setting :application_key
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module B2::Authentication
|
2
|
+
|
3
|
+
class << self
|
4
|
+
def authorize
|
5
|
+
options = {
|
6
|
+
basic_auth: {username: B2.account_id, password: B2.application_key}
|
7
|
+
}
|
8
|
+
|
9
|
+
response = HTTParty.get("#{BASE_URI}b2_authorize_account", options)
|
10
|
+
|
11
|
+
B2::Base.base_uri "#{response['apiUrl']}/b2api/v1/"
|
12
|
+
B2::Base.headers 'Authorization' => response['authorizationToken'], 'Content-Type' => 'application/json'
|
13
|
+
B2::Base.cookies.merge!(download_url: response['downloadUrl'])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/b2/base.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module B2
|
2
|
+
class Base
|
3
|
+
include HTTParty
|
4
|
+
attr_accessor :download_url
|
5
|
+
|
6
|
+
[:get, :head, :post, :put].each do |req|
|
7
|
+
define_method(req) do |path, options={}, &block|
|
8
|
+
self.class.send(req, path, options, &block)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
B2::Authentication.authorize if B2::Base.base_uri.nil?
|
14
|
+
@download_url = B2::Base.cookies[:download_url]
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/lib/b2/bucket.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
module B2
|
2
|
+
class Bucket < Base
|
3
|
+
attr_accessor :bucket_id, :account_id, :bucket_name, :bucket_type, :buckets, :upload_url, :authorization_token, :files
|
4
|
+
|
5
|
+
def create(bucket_name, bucket_type)
|
6
|
+
body = {
|
7
|
+
accountId: B2.account_id,
|
8
|
+
bucketName: bucket_name,
|
9
|
+
bucketType: bucket_type
|
10
|
+
}
|
11
|
+
response = post('/b2_create_bucket', body: body.to_json)
|
12
|
+
assign_return_value(response)
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete(bucket_id)
|
16
|
+
body = {
|
17
|
+
accountId: B2.account_id,
|
18
|
+
bucketId: bucket_id
|
19
|
+
}
|
20
|
+
|
21
|
+
response = post('/b2_delete_bucket', body: body.to_json)
|
22
|
+
assign_return_value(response)
|
23
|
+
end
|
24
|
+
|
25
|
+
def list
|
26
|
+
body = {accountId: B2.account_id}
|
27
|
+
|
28
|
+
response = post('/b2_list_buckets', body: body.to_json)
|
29
|
+
assign_return_value(response)
|
30
|
+
end
|
31
|
+
|
32
|
+
def update_bucket(bucket_id, bucket_type)
|
33
|
+
body = {
|
34
|
+
accountId: B2.account_id,
|
35
|
+
bucketId: bucket_id,
|
36
|
+
bucketType: bucket_type
|
37
|
+
}
|
38
|
+
|
39
|
+
response = post('/b2_update_bucket', body: body.to_json)
|
40
|
+
assign_return_value(response)
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_upload_url(bucket_id)
|
44
|
+
body = {
|
45
|
+
bucketId: bucket_id
|
46
|
+
}
|
47
|
+
|
48
|
+
response = post('/b2_get_upload_url', body: body.to_json)
|
49
|
+
assign_return_value(response)
|
50
|
+
end
|
51
|
+
|
52
|
+
def list_file_names(bucket_id, startFileName: nil, maxFileCount: nil)
|
53
|
+
body = {
|
54
|
+
bucketId: bucket_id
|
55
|
+
}
|
56
|
+
body.merge!(startFileName: startFileName) if startFileName
|
57
|
+
body.merge!(maxFileCount: maxFileCount) if maxFileCount
|
58
|
+
|
59
|
+
response = post('/b2_list_file_names', body: body.to_json)
|
60
|
+
assign_return_value(response)
|
61
|
+
end
|
62
|
+
|
63
|
+
def list_file_versions(bucket_id, startFileName: nil, startFileId: nil, maxFileCount: nil)
|
64
|
+
body = {
|
65
|
+
bucketId: bucket_id
|
66
|
+
}
|
67
|
+
body.merge!(startFileName: startFileName) if startFileName
|
68
|
+
body.merge!(maxFileCount: maxFileCount) if maxFileCount
|
69
|
+
body.merge!(startFileId: startFileId) if startFileId
|
70
|
+
|
71
|
+
response = post('/b2_list_file_versions', body: body.to_json)
|
72
|
+
assign_return_value(response)
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
def assign_return_value(response)
|
77
|
+
if response.code.eql?(200)
|
78
|
+
self.bucket_id = response["bucketId"]
|
79
|
+
self.account_id = response["accountId"]
|
80
|
+
self.bucket_name = response["bucketName"]
|
81
|
+
self.bucket_type = response["bucketType"]
|
82
|
+
self.buckets = response["buckets"]
|
83
|
+
self.upload_url = response["uploadUrl"]
|
84
|
+
self.authorization_token = response["authorizationToken"]
|
85
|
+
self.files = response["files"]
|
86
|
+
end
|
87
|
+
response
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/b2/doc.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module B2
|
2
|
+
class Doc < Base
|
3
|
+
attr_accessor :bucket_id, :account_id, :content_length, :content_sha1, :content_type, :file_id, :filename, :file_info
|
4
|
+
|
5
|
+
def create(upload_url, file, authorization_token, folder: nil, content_type: 'b2/x-auto', info: {})
|
6
|
+
if file.respond_to?(:read)
|
7
|
+
content_type = file.content_type
|
8
|
+
size = file.size.to_s
|
9
|
+
filename = folder.nil? ? ERB::Util.url_encode(file.original_filename) : "#{folder}/#{ERB::Util.url_encode(file.original_filename)}"
|
10
|
+
digest = Digest::SHA1.file(file.path).hexdigest
|
11
|
+
body = file.read
|
12
|
+
elsif file.is_a?(String)
|
13
|
+
size = File.size(file).to_s
|
14
|
+
filename = folder.nil? ? ERB::Util.url_encode(File.basename(file)) : "#{folder}/#{ERB::Util.url_encode(File.basename(file))}"
|
15
|
+
digest = Digest::SHA1.file(file).hexdigest
|
16
|
+
body = File.read(file)
|
17
|
+
else
|
18
|
+
raise ArgumentError.new('Unsuitable data type. Please read the docs.')
|
19
|
+
end
|
20
|
+
|
21
|
+
additional_headers = {}
|
22
|
+
info.first(10).each do |key, value|
|
23
|
+
additional_headers["X-Bz-Info-#{key}"] = value
|
24
|
+
end
|
25
|
+
|
26
|
+
headers = {
|
27
|
+
"Authorization" => authorization_token,
|
28
|
+
"Content-Type" => content_type,
|
29
|
+
"X-Bz-File-Name" => filename,
|
30
|
+
"Content-Length" => size,
|
31
|
+
"X-Bz-Content-Sha1" => digest
|
32
|
+
}
|
33
|
+
|
34
|
+
headers.merge!(additional_headers)
|
35
|
+
|
36
|
+
response = HTTParty.post(upload_url, body: body, headers: headers)
|
37
|
+
assign_return_value(response)
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete_file_version(filename, file_id)
|
41
|
+
body = {
|
42
|
+
fileName: filename,
|
43
|
+
fileId: file_id
|
44
|
+
}
|
45
|
+
response = post('/b2_delete_file_version', body: body.to_json)
|
46
|
+
assign_return_value(response)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_download_url(bucket_name, filename)
|
50
|
+
"#{self.download_url}/file/#{bucket_name}/#{filename}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def download_file_by_id(file_id)
|
54
|
+
get("#{self.download_url}/b2api/v1/b2_download_file_by_id?fileId=#{file_id}")
|
55
|
+
end
|
56
|
+
|
57
|
+
def download_file_by_name(bucket_name, filename)
|
58
|
+
get("#{self.download_url}/file/#{bucket_name}/#{filename}")
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def assign_return_value(response)
|
63
|
+
if response.code.eql?(200)
|
64
|
+
self.bucket_id = response["bucketId"]
|
65
|
+
self.account_id = response["accountId"]
|
66
|
+
self.content_length = response["contentLength"]
|
67
|
+
self.content_sha1 = response["contentSha1"]
|
68
|
+
self.content_type = response["contentType"]
|
69
|
+
self.file_id = response["fileId"]
|
70
|
+
self.file_info = response["fileInfo"]
|
71
|
+
self.filename = response["fileName"]
|
72
|
+
end
|
73
|
+
response
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/lib/b2/version.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Configuration
|
2
|
+
def configuration
|
3
|
+
yield self
|
4
|
+
end
|
5
|
+
|
6
|
+
def define_setting(name, default = nil)
|
7
|
+
class_variable_set("@@#{name}", default)
|
8
|
+
define_class_method "#{name}=" do |value|
|
9
|
+
class_variable_set("@@#{name}", value)
|
10
|
+
end
|
11
|
+
define_class_method name do
|
12
|
+
class_variable_get("@@#{name}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def define_class_method(name, &block)
|
18
|
+
(class << self; self; end).instance_eval do
|
19
|
+
define_method name, &block
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: b2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Muhammad Bayu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: httparty
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.13'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.13'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- metalkoholic@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- b2.gemspec
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/b2.rb
|
86
|
+
- lib/b2/authentication.rb
|
87
|
+
- lib/b2/base.rb
|
88
|
+
- lib/b2/bucket.rb
|
89
|
+
- lib/b2/doc.rb
|
90
|
+
- lib/b2/version.rb
|
91
|
+
- lib/helpers/configuration.rb
|
92
|
+
homepage: https://github.com/metalkoholic/b2
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 2.1.0
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: ruby wrapper for backblaze b2 API
|
116
|
+
test_files: []
|