Class: NSISam::Client

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

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Initialize a client to a SAM node hosted at a specific url

Examples:

nsisam = NSISam::Client.new user: 'username' password: 'pass',
                            host: 'localhost', port: '8888'

Parameters:

  • optional (Hash)

    hash with user, password, host and port of the SAM node



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

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

Instance Attribute Details

#expireObject

Returns the value of attribute expire.



13
14
15
# File 'lib/nsisam/client.rb', line 13

def expire
  @expire
end

Class Method Details

.configure { ... } ⇒ Object

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

Examples:

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

Yields:



173
174
175
# File 'lib/nsisam/client.rb', line 173

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

Instance Method Details

#delete(key) ⇒ Hash

Delete data at a given SAM key

Examples:

Deleting an existing key

nsisam.delete("some key")

Parameters:

  • key (Sring)

    of the value to delete

Returns:

  • (Hash)

    response

    • “deleted” [Boolean] true if the key was successfully deleted

Raises:



76
77
78
79
80
# File 'lib/nsisam/client.rb', line 76

def delete(key)
  request_data = {:key => key}.to_json
  request = prepare_request :DELETE, request_data
  Response.new(execute_request(request))
end


177
178
179
# File 'lib/nsisam/client.rb', line 177

def download_link_for_file(key)
  "http://#{@host}:#{@port}/file/#{key}"
end

#get(key, expected_checksum = nil) ⇒ Response

Recover data stored at a given SAM key

Examples:

nsisam.get("some key")

Parameters:

  • key (String)

    of the value to acess

Returns:

  • (Response)

    response object holding the file and some metadata

Raises:



92
93
94
95
96
97
98
# File 'lib/nsisam/client.rb', line 92

def get(key, expected_checksum=nil)
  request_data = {:key => key}.to_json
  request = prepare_request :GET, request_data
  response = execute_request(request)
  verify_checksum(response["data"], expected_checksum) unless expected_checksum.nil?
  Response.new(response)
end

#get_file(key, type = :file, expected_checksum = nil) ⇒ Response

Note:

Use of wrong “type” parameter can generate errors.

Recover a file stored at a given SAM key

Examples:

nsisam.get_file("some key")
nsisam.store_file("test", :doc) # stored at key 'test_key'
nsisam.get_file("test_key", :doc)

Parameters:

  • key (String)

    of the file to access

  • type (Symbol) (defaults to: :file)

    of the file to be recovered. Can be either :doc and :video.

Returns:

  • (Response)

    response object holding the file and some metadata

Raises:



115
116
117
118
119
120
121
122
123
# File 'lib/nsisam/client.rb', line 115

def get_file(key, type=:file, expected_checksum = nil)
  response = get(key, expected_checksum)
  response = Response.new(
    'key' => response.key,
    'checksum' => response.checksum,
    'filename' => response.data['filename'],
    'file' => Base64.decode64(response.data[type.to_s]),
    'deleted' => response.deleted?)
end

#store(data) ⇒ Hash

Store a given data in SAM

Examples:

nsisam.store("something")

Parameters:

  • data (String)

    the desired data to store

Returns:

  • (Hash)

    response with the data key and checksum

    • “key” [String] the key to access the stored data

    • “checksum” [String] the sha512 checksum of the stored data

Raises:



42
43
44
45
46
47
# File 'lib/nsisam/client.rb', line 42

def store(data)
  request_data = {:value => data}
  request_data[:expire] = @expire if @expire
  request = prepare_request :POST, request_data.to_json
  Response.new(execute_request(request))
end

#store_file(file_content, filename, type = :file) ⇒ Response

Store a file in SAM. If the file will be used by other NSI’s service you should pass an additional ‘type’ parameter.

Examples:

nsisam.store_file(File.read("foo.txt"))
nsisam.store_file(File.read("foo.txt"), :video)

Parameters:

  • file_content (Object)

    json serializable object

  • type (Symbol) (defaults to: :file)

    of the file to be stored. Can be either :doc and :video.

Returns:

  • (Response)

    object with access to the key and the sha512 checkum of the stored data

Raises:



61
62
63
# File 'lib/nsisam/client.rb', line 61

def store_file(file_content, filename, type=:file)
  store(type => Base64.encode64(file_content), :filename => filename)
end

#update(key, value) ⇒ Response

Update data stored at a given SAM key

Examples:

nsisam.update("my key", "my value")

Parameters:

  • key (String)

    of the data to update

  • data (String, Hash, Array)

    to be stored at the key

Returns:

  • (Response)

    response object holding the file and some metadata

Raises:



136
137
138
139
140
141
# File 'lib/nsisam/client.rb', line 136

def update(key, value)
  request_data = {:key => key, :value => value}
  request_data[:expire] = @expire if @expire
  request = prepare_request :PUT, request_data.to_json
  Response.new(execute_request(request))
end

#update_file(key, type = :file, new_content, filename) ⇒ Response

Update file stored at a given SAM key

Examples:

nsisam.update_file("my key", "my value")
nsisam.update_file("my key", "my value", :video)
nsisam.update_file("my key", "my value", :doc)

Parameters:

  • key (String)

    of the file to update

  • type (Symbol) (defaults to: :file)

    of the file to be recovered. Can be either :doc and :video.

  • new_content (String)

    content of the new file

Returns:

  • (Response)

    response object holding the file and some metadata

Raises:



157
158
159
160
# File 'lib/nsisam/client.rb', line 157

def update_file(key, type=:file, new_content, filename)
  encoded = Base64.encode64(new_content)
  update(key, type => encoded, filename: filename)
end