Class: Blobs

Inherits:
ChinoBaseAPI show all
Defined in:
lib/chino_ruby.rb

Instance Method Summary collapse

Methods inherited from ChinoBaseAPI

#delete_resource, #get_resource, #initialize, #parse_response, #patch_resource, #post_resource, #post_resource_with_string_result, #put_resource, #return_uri

Methods inherited from CheckValues

#check_boolean, #check_int, #check_json, #check_string

Constructor Details

This class inherits a constructor from ChinoBaseAPI

Instance Method Details

#commit_upload(upload_id) ⇒ Object



1539
1540
1541
1542
1543
1544
1545
# File 'lib/chino_ruby.rb', line 1539

def commit_upload(upload_id)
    check_string(upload_id)
    data = {"upload_id": upload_id}.to_json
    blob = Blob.new
    blob.from_json(post_resource("/blobs/commit", data).to_json, true)
    blob
end

#delete_blob(blob_id, force) ⇒ Object



1576
1577
1578
1579
1580
# File 'lib/chino_ruby.rb', line 1576

def delete_blob(blob_id, force)
    check_string(blob_id)
    check_boolean(force)
    delete_resource("/blobs/#{blob_id}", force)
end

#get(blob_id, destination) ⇒ Object



1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
# File 'lib/chino_ruby.rb', line 1547

def get(blob_id, destination)
    check_string(blob_id)
    check_string(destination)
    uri = return_uri("/blobs/#{blob_id}")
    req = Net::HTTP::Get.new(uri.path)
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
        else
        req.basic_auth @customer_id, @customer_key
    end
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    blob = GetBlobResponse.new
    blob.blob_id = blob_id
    filename = res.header['Content-Disposition'].partition('=').last
    blob.filename = filename
    blob.path = destination
    path = File.join File.expand_path("..", File.dirname(__FILE__)), destination
    FileUtils.mkdir_p(path) unless File.exist?(path)
    File.open(File.join(path+filename), 'wb') { |file|
        file << res.body
        blob.md5 = (Digest::MD5.file file).hexdigest
        blob.sha1 = (Digest::SHA1.file file).hexdigest
        blob.size = file.size
    }
    blob
end

#init_upload(filename, document_id, field) ⇒ Object



1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/chino_ruby.rb', line 1509

def init_upload(filename, document_id, field)
    check_string(filename)
    check_string(document_id)
    check_string(field)
    data = {"file_name": filename, "document_id": document_id, "field": field}.to_json
    blob = InitBlobResponse.new
    blob.from_json(ActiveSupport::JSON.decode(post_resource("/blobs", data).to_json)['blob'].to_json)
    blob
end

#upload_blob(path, filename, document_id, field) ⇒ Object



1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
# File 'lib/chino_ruby.rb', line 1489

def upload_blob(path, filename, document_id, field)
    chunk_size = 1024*32
    check_string(path)
    check_string(document_id)
    check_string(field)
    check_string(filename)
    blob = InitBlobResponse.new
    blob = init_upload(filename, document_id, field)
    bytes = []
    offset = 0
    file_path = File.join File.expand_path("..", File.dirname(__FILE__)), path, filename
    File.open(file_path, 'rb') { |file|
        while (buffer = file.read(chunk_size)) do
            upload_chunk(blob.upload_id, buffer, offset)
            offset = offset+buffer.length
        end
        commit_upload(blob.upload_id)
    }
end

#upload_chunk(upload_id, bytes, offset) ⇒ Object



1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
# File 'lib/chino_ruby.rb', line 1519

def upload_chunk(upload_id, bytes, offset)
    uri = return_uri("/blobs/#{upload_id}")
    req = Net::HTTP::Put.new(uri)
    req.body = bytes
    req.add_field("length", bytes.length)
    req.add_field("offset", offset)
    req.add_field("Content-Type", "application/octet-stream")
    if @customer_id == "Bearer "
        req.add_field("Authorization", @customer_id+@customer_key)
        else
        req.basic_auth @customer_id, @customer_key
    end
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
        http.request(req)
    }
    blob = InitBlobResponse.new
    blob.from_json(parse_response(res)['data'].to_json, true)
    blob
end