lib/nsicloudooo/client.rb in nsicloudooo-0.2.6 vs lib/nsicloudooo/client.rb in nsicloudooo-0.2.7
- old
+ new
@@ -25,18 +25,24 @@
# @option options [String] verb the callback request verb, when not provided, nsi.cloudooo default to POST
#
# @example A simple granulation
# require 'base64'
# doc = Base64.encode64(File.new('document.odt', 'r').read)
- # nsicloudooo.granulate(:file => doc, :filename => 'document.odt')
+ # response = nsicloudooo.granulate(:file => doc, :filename => 'document.odt')
+ # nsicloudooo.done(response["doc_key"])
+ # nsicloudooo.grains_keys_for(response["doc_key"])
# @example Granulating from a SAM uid
# doc = Base64.encode64(File.new('document.odt', 'r').read)
# response = sam.store({:doc => doc})
# doc_key = response["doc_key"]
- # nsicloudooo.granulate(:sam_uid => doc_key, :filename => 'document.odt')
+ # response = nsicloudooo.granulate(:sam_uid => doc_key, :filename => 'document.odt')
+ # nsicloudooo.done(response["doc_key"])
+ # nsicloudooo.grains_keys_for(response["doc_key"])
# @example Downloading and granulating from web
- # nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt')
+ # response = nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt')
+ # nsicloudooo.done(response["doc_key"])
+ # nsicloudooo.grains_keys_for(response["doc_key"])
# @example Sending a callback url
# doc = Base64.encode64(File.new('document.odt', 'r').read)
# nsicloudooo.granulate(:file => doc, :filename => 'document.odt', :callback => 'http://google.com')
# nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt', :callback => 'http://google.com')
# @example Using a custom verb to the callback
@@ -44,52 +50,68 @@
# nsicloudooo.granulate(:file => doc, :filename => 'document.odt', :callback => 'http://google.com', :verb => "PUT")
# nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt', :callback => 'http://google.com', :verb => "PUT")
#
# @return [Hash] response
# * "doc_key" [String] the key to access the granulated document if the sam node it was stored
+ #
+ # @raise NSICloudooo::Errors::Client::MissingParametersError when an invalid or incomplete set of parameters is provided
+ # @raise NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node
+ # @raise NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided
+ # @raise NSICloudooo::Errors::Client::KeyNotFoundError when an invalid sam_uid is provided
+ #
def granulate(options = {})
@request_data = Hash.new
if options[:doc_link]
insert_download_data options
- elsif options[:sam_uid]
+ elsif options[:sam_uid] && options[:filename]
file_data = {:sam_uid => options[:sam_uid], :filename => options[:filename]}
@request_data.merge! file_data
- else
+ elsif options[:file] && options[:filename]
file_data = {:doc => options[:file], :filename => options[:filename]}
@request_data.merge! file_data
+ else
+ raise NSICloudooo::Errors::Client::MissingParametersError
end
insert_callback_data options
request = prepare_request :POST, @request_data.to_json
execute_request(request)
end
# Verify if a document is already granulated
#
- # @raise NSICloudooo::Errors::Client:KeyNotFoundError when an invalid document key is provided
#
# @param [String] key of the desired document
# @return [Hash] response
# * "done" [String] true if the document was already granualted, otherwise, false
#
# @example
# nsicloudooo.done("some key")
+ #
+ # @raise NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node
+ # @raise NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided
+ # @raise NSICloudooo::Errors::Client::KeyNotFoundError when an invalid key is provided
+ #
def done(key)
request = prepare_request :GET, {:key => key}.to_json
execute_request(request)
end
# Return the keys of the grains of a document
#
- # @raise NSICloudooo::Errors::Client:KeyNotFoundError when an invalid document key is provided
#
# @param [String] key of the desired document
# @return [Hash] response
# * "images" [String] keys to the images grains of the document
# * "files" [String] keys to the files grains of the document
#
# @example
# nsicloudooo.grains_keys_for("some key")
+ #
+ # @raise NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node
+ # @raise NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided
+ # @raise NSICloudooo::Errors::Client::KeyNotFoundError when an invalid key is provided
+ #
def grains_keys_for(document_key)
request = prepare_request :GET, {:doc_key => document_key}.to_json
execute_request(request)
end
@@ -116,9 +138,14 @@
def execute_request(request)
response = Net::HTTP.start @url, @port do |http|
http.request(request)
end
raise NSICloudooo::Errors::Client::KeyNotFoundError if response.code == "404"
+ raise NSICloudooo::Errors::Client::MalformedRequestError if response.code == "400"
+ raise NSICloudooo::Errors::Client::AuthenticationError if response.code == "401"
+ if response.code == "500" and response.body.include?("SAM")
+ raise NSICloudooo::Errors::Client::SAMConnectionError
+ end
JSON.parse(response.body)
end
end
end