Module: Backblaze::B2
- Defined in:
- lib/backblaze/b2.rb,
lib/backblaze/b2/base.rb,
lib/backblaze/b2/file.rb,
lib/backblaze/b2/bucket.rb,
lib/backblaze/b2/file_version.rb
Defined Under Namespace
Classes: Base, Bucket, File, FileVersion
Class Attribute Summary collapse
-
.account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
.api_path ⇒ Object
readonly
Returns the value of attribute api_path.
-
.api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
.download_url ⇒ Object
readonly
Returns the value of attribute download_url.
-
.token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
- .credentials_file(filename, raise_errors: true, logging: false) ⇒ Object
-
.login(account_id:, application_key:, api_path: '/b2api/v1/') ⇒ void
Authenticates with the server to get the authorization data.
Class Attribute Details
.account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
8 9 10 |
# File 'lib/backblaze/b2.rb', line 8 def account_id @account_id end |
.api_path ⇒ Object (readonly)
Returns the value of attribute api_path.
8 9 10 |
# File 'lib/backblaze/b2.rb', line 8 def api_path @api_path end |
.api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
8 9 10 |
# File 'lib/backblaze/b2.rb', line 8 def api_url @api_url end |
.download_url ⇒ Object (readonly)
Returns the value of attribute download_url.
8 9 10 |
# File 'lib/backblaze/b2.rb', line 8 def download_url @download_url end |
.token ⇒ Object (readonly)
Returns the value of attribute token.
8 9 10 |
# File 'lib/backblaze/b2.rb', line 8 def token @token end |
Class Method Details
.credentials_file(filename, raise_errors: true, logging: false) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/backblaze/b2.rb', line 35 def credentials_file(filename, raise_errors: true, logging: false) opts = nil open(filename, 'r') do |f| if ::File.extname(filename) == '.json' require 'json' opts = JSON.load(f) else require 'psych' opts = Psych.load(f.read) end end parsed = {} [:application_key, :account_id, :api_path].each do |key| if opts[key.to_s].is_a? String parsed[key] = opts[key.to_s] end end if [:application_key, :account_id].inject(true) { |status, key| status && !parsed[key].nil? } puts "Attempting #{parsed[:account_id]}" if logging login(parsed) true else puts "Missing params" if logging false end rescue => e puts e if logging raise e if raise_errors false end |
.login(account_id:, application_key:, api_path: '/b2api/v1/') ⇒ void
This method returns an undefined value.
Authenticates with the server to get the authorization data. Raises an error if there is a problem
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/backblaze/b2.rb', line 17 def login(account_id:, application_key:, api_path: '/b2api/v1/') = { basic_auth: {username: account_id, password: application_key} } api_path = "/#{api_path}/".gsub(/\/+/, '/') response = HTTParty.get("https://api.backblaze.com#{api_path}b2_authorize_account", ) raise Backblaze::AuthError.new(response) unless response.code == 200 @account_id = response['accountId'] @token = response['authorizationToken'] @api_url = response['apiUrl'] @download_url = response['downloadUrl'] @api_path = api_path Backblaze::B2::Base.base_uri "#{@api_url}#{api_path}" Backblaze::B2::Base.headers 'Authorization' => @token, 'Content-Type' => 'application/json' end |