Class: NSICloudooo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nsicloudooo/client.rb,
lib/nsicloudooo/configuration.rb

Defined Under Namespace

Classes: Configuration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Note:

if you had used the ‘configure’ method, you can use it without parameters and those you provided before will be used (see Client#configure)

Initialize a client to a CloudoooManager node

Examples:

nsisam = NSISam::Client.new host: 'localhost', port: '8886', user: 'test', password: 'test'

Parameters:

  • options (Hash)

    used to connect to the desired CloudoooManager



23
24
25
26
27
28
29
# File 'lib/nsicloudooo/client.rb', line 23

def initialize(params = {})
  params = Configuration.settings.merge(params)
  @user = params[:user]
  @password = params[:password]
  @host = params[:host]
  @port = params[:port]
end

Class Method Details

.configure { ... } ⇒ Object

Pre-configure the NSICloudooo module with default params for the NSICloudooo::Client

Examples:

NSICloudooo::Client.configure do
  user     "why"
  password "chunky"
  host     "localhost"
  port     "8888"
end

Yields:



202
203
204
# File 'lib/nsicloudooo/client.rb', line 202

def self.configure(&block)
  Configuration.instance_eval(&block)
end

Instance Method Details

#done(key) ⇒ Hash

Verify if a document is already granulated

Examples:

nsicloudooo.done("some key")

Parameters:

  • key (String)

    of the desired document

Returns:

  • (Hash)

    response

    • “done” [String] true if the document was already granualted, otherwise, false

Raises:

  • NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSICloudooo::Errors::Client::KeyNotFoundError when an invalid key is provided



109
110
111
112
# File 'lib/nsicloudooo/client.rb', line 109

def done(key)
  request = prepare_request :GET, {:key => key}.to_json
  execute_request(request)
end

#extract_metadata(document_key, type, callback_url = nil, callback_verb = nil) ⇒ Hash

Enqueue a document to have its metadata extracted

Parameters:

  • key (String)

    of the desired document

  • type (String)

    of the desired document (‘tcc’ or ‘event’)

Returns:

  • (Hash)

    response

    • “doc_key” [String] the key to access the granulated document if the sam node it was stored

Raises:

  • NSICloudooo::Errors::Client::MissingParametersError when an invalid or incomplete set of parameters is provided

  • NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSICloudooo::Errors::Client::KeyNotFoundError when an invalid sam_uid is provided



166
167
168
169
170
171
# File 'lib/nsicloudooo/client.rb', line 166

def (document_key, type, callback_url=nil, callback_verb=nil)
  @request_data = {:sam_uid => document_key, :type => type, :metadata => true}
  insert_callback_data :callback => callback_url, :verb => callback_verb
  request = prepare_request :POST, @request_data.to_json
  execute_request(request)
end

#grains_keys_for(document_key) ⇒ Hash

Return the keys of the grains of a document

Examples:

nsicloudooo.grains_keys_for("some key")

Parameters:

  • key (String)

    of the desired document

Returns:

  • (Hash)

    response

    • “images” [String] keys to the images grains of the document

    • “files” [String] keys to the files grains of the document

Raises:

  • NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSICloudooo::Errors::Client::KeyNotFoundError when an invalid key is provided



129
130
131
132
# File 'lib/nsicloudooo/client.rb', line 129

def grains_keys_for(document_key)
  request = prepare_request :GET, {:doc_key => document_key}.to_json
  execute_request(request).select {|chave| ['images', 'files'].include? chave}
end

#granulate(options = {}) ⇒ Hash

Note:

the filename is very importante, the cloudooo node will convert the document based on its filename, if necessary

Note:

if provided both doc_link and file options, file will be ignored and the client will download the document instead

Send a document be granulated by a nsi.cloudooo node

Examples:

A simple granulation

require 'base64'
doc = Base64.encode64(File.new('document.odt', 'r').read)
response = nsicloudooo.granulate(:file => doc, :filename => 'document.odt')
nsicloudooo.done(response["doc_key"])
nsicloudooo.grains_keys_for(response["doc_key"])

Granulating from a SAM uid

doc = Base64.encode64(File.new('document.odt', 'r').read)
response = sam.store({:doc => doc})
doc_key = response["doc_key"]
response = nsicloudooo.granulate(:sam_uid => doc_key, :filename => 'document.odt')
nsicloudooo.done(response["doc_key"])
nsicloudooo.grains_keys_for(response["doc_key"])

Downloading and granulating from web

response = nsicloudooo.granulate(:doc_link => 'http://google.com/document.odt')
nsicloudooo.done(response["doc_key"])
nsicloudooo.grains_keys_for(response["doc_key"])

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')

Using a custom verb to the callback

doc = Base64.encode64(File.new('document.odt', 'r').read)
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")

Parameters:

  • options (Hash) (defaults to: {})

    used to send a document to be graulated

Options Hash (options):

  • file (String)

    the base64 encoded file to be granulated

  • sam_uid (String)

    the UID of a document at SAM

  • filename (String)

    the filename of the document

  • doc_link (String)

    link to the document that’ll be granulated

  • callback (String)

    a callback url to the file granulation

  • verb (String)

    the callback request verb, when not provided, nsi.cloudooo default to POST

Returns:

  • (Hash)

    response

    • “doc_key” [String] the key to access the granulated document if the sam node it was stored

Raises:

  • NSICloudooo::Errors::Client::MissingParametersError when an invalid or incomplete set of parameters is provided

  • NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSICloudooo::Errors::Client::KeyNotFoundError when an invalid sam_uid is provided



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nsicloudooo/client.rb', line 77

def granulate(options = {})
  @request_data = Hash.new
  if options[:doc_link]
    insert_download_data options
  elsif options[:sam_uid] && options[:filename]
    file_data = {:sam_uid => options[:sam_uid], :filename => options[:filename]}
    @request_data.merge! file_data
  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

#metadata_key_for(document_key) ⇒ String

Return the key of the metadata of a document

Examples:

nsicloudooo.("some key")

Parameters:

  • key (String)

    of the desired document

Returns:

  • (String)

    key of the desired document’s metadata or false if the metadata wasn’t extracted yet

Raises:

  • NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSICloudooo::Errors::Client::KeyNotFoundError when an invalid key is provided



186
187
188
189
# File 'lib/nsicloudooo/client.rb', line 186

def (document_key)
  request = prepare_request :GET, {:doc_key => document_key, :metadata => true}.to_json
  execute_request(request)["metadata_key"]
end

#thumbnail_key_for(document_key) ⇒ String

Return the key of the thumbnail of a document

Examples:

nsicloudooo.thumbnail_key_for("some key")

Parameters:

  • key (String)

    of the desired document

Returns:

  • (String)

    key of the desired document’s thumbnail

Raises:

  • NSICloudooo::Errors::Client::SAMConnectionError when cannot connect to the SAM node

  • NSICloudooo::Errors::Client::AuthenticationError when invalids user and/or password are provided

  • NSICloudooo::Errors::Client::KeyNotFoundError when an invalid key is provided



147
148
149
150
# File 'lib/nsicloudooo/client.rb', line 147

def thumbnail_key_for(document_key)
  request = prepare_request :GET, {:doc_key => document_key}.to_json
  execute_request(request)["thumbnail"]
end