Class: B2::Connection
- Inherits:
-
Object
- Object
- B2::Connection
- Defined in:
- lib/b2/connection.rb
Instance Method Summary collapse
- #account_id ⇒ Object
- #authorization_token ⇒ Object
- #buckets ⇒ Object
- #download(bucket, key, to = nil) ⇒ Object
- #download_url ⇒ Object
- #get(path, body = nil, &block) ⇒ Object
- #get_download_url(bucket, filename, expires_in: 3_600, disposition: nil) ⇒ Object
-
#initialize(key_id, secret, pool: 5, timeout: 5) ⇒ Connection
constructor
A new instance of Connection.
- #lookup_bucket_id(name) ⇒ Object
- #post(path, body = nil, &block) ⇒ Object
- #send_request(request, body = nil, &block) ⇒ Object
- #with_connection ⇒ Object
Constructor Details
#initialize(key_id, secret, pool: 5, timeout: 5) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/b2/connection.rb', line 7 def initialize(key_id, secret, pool: 5, timeout: 5) @mutex = Mutex.new @availability = ConditionVariable.new @max = pool @timeout = timeout @free_pool = [] @used_pool = [] @key_id = key_id @key_secret = secret @buckets_cache = [] end |
Instance Method Details
#account_id ⇒ Object
21 22 23 24 25 |
# File 'lib/b2/connection.rb', line 21 def account_id return @account_id if !@account_id.nil? @account_id = with_connection { |conn| conn.account_id } end |
#authorization_token ⇒ Object
51 52 53 |
# File 'lib/b2/connection.rb', line 51 def with_connection { |conn| conn. } end |
#buckets ⇒ Object
63 64 65 66 67 |
# File 'lib/b2/connection.rb', line 63 def buckets post('/b2api/v2/b2_list_buckets', {accountId: account_id})['buckets'].map do |b| B2::Bucket.new(b, self) end end |
#download(bucket, key, to = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/b2/connection.rb', line 89 def download(bucket, key, to=nil) opened_file = (to && to.is_a?(String)) to = ::File.open(to, 'wb') if to.is_a?(String) digestor = Digest::SHA1.new data = "" uri = URI.parse(download_url) conn = Net::HTTP.new(uri.host, uri.port) conn.use_ssl = uri.scheme == 'https' req = Net::HTTP::Get.new("/file/#{bucket}/#{key}") req['Authorization'] = conn.start do |http| http.request(req) do |response| case response when Net::HTTPSuccess response.read_body do |chunk| digestor << chunk if to to << chunk elsif block_given? yield(chunk) else data << chunk end end if response['X-Bz-Content-Sha1'] != 'none' && digestor.hexdigest != response['X-Bz-Content-Sha1'] rase B2::FileIntegrityError.new("SHA1 Mismatch, expected: \"#{response['X-Bz-Content-Sha1']}\", actual: \"#{digestor.hexdigest}\"") end when Net::HTTPNotFound raise B2::NotFound.new(JSON.parse(response.body)['message']) else begin body = JSON.parse(response.body) if body['code'] == 'not_found' raise B2::NotFound(body['message']) else raise "#{body['code']} (#{body['message']})" end rescue raise response.body end end end end if opened_file to.close elsif to to.flush end !block_given? && to.nil? ? data : nil end |
#download_url ⇒ Object
59 60 61 |
# File 'lib/b2/connection.rb', line 59 def download_url with_connection { |conn| conn.download_url } end |
#get(path, body = nil, &block) ⇒ Object
144 145 146 147 148 |
# File 'lib/b2/connection.rb', line 144 def get(path, body=nil, &block) request = Net::HTTP::Get.new(path) send_request(request, body, &block) end |
#get_download_url(bucket, filename, expires_in: 3_600, disposition: nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/b2/connection.rb', line 77 def get_download_url(bucket, filename, expires_in: 3_600, disposition: nil) response = post("/b2api/v2/b2_get_download_authorization", { bucketId: lookup_bucket_id(bucket), fileNamePrefix: filename, validDurationInSeconds: expires_in, b2ContentDisposition: disposition }) url = download_url + '/file/' + bucket + '/' + filename + "?Authorization=" + response['authorizationToken'] url += "&b2ContentDisposition=#{CGI.escape(disposition)}" if disposition url end |
#lookup_bucket_id(name) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/b2/connection.rb', line 69 def lookup_bucket_id(name) bucket = @buckets_cache.find{ |b| b.name == name } return bucket.id if bucket @buckets_cache = buckets @buckets_cache.find{ |b| b.name == name }&.id end |
#post(path, body = nil, &block) ⇒ Object
150 151 152 153 154 |
# File 'lib/b2/connection.rb', line 150 def post(path, body=nil, &block) request = Net::HTTP::Post.new(path) send_request(request, body, &block) end |
#send_request(request, body = nil, &block) ⇒ Object
55 56 57 |
# File 'lib/b2/connection.rb', line 55 def send_request(request, body=nil, &block) with_connection { |conn| conn.send_request(request, body, &block) } end |
#with_connection ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/b2/connection.rb', line 27 def with_connection conn = @mutex.synchronize do cxn = if !@free_pool.empty? @free_pool.shift elsif @free_pool.size + @used_pool.size < @max B2::APIConnection.new(@key_id, @key_secret) else @availability.wait(@mutex, @timeout) @free_pool.shift || B2::APIConnection.new(@key_id, @key_secret) end @used_pool << cxn cxn end yield conn ensure @mutex.synchronize do @used_pool.delete(conn) @free_pool << conn if conn.active? @availability.signal() end end |