Class: NSISam::Client
- Inherits:
-
Object
- Object
- NSISam::Client
- Defined in:
- lib/nsisam/client.rb,
lib/nsisam/configuration.rb
Defined Under Namespace
Classes: Configuration
Instance Attribute Summary collapse
-
#expire ⇒ Object
Returns the value of attribute expire.
Class Method Summary collapse
-
.configure { ... } ⇒ Object
Pre-configure the NSISam module with default params for the NSISam::Client.
Instance Method Summary collapse
-
#delete(key) ⇒ Hash
Delete data at a given SAM key.
- #download_link_for_file(key) ⇒ Object
-
#get(key, expected_checksum = nil) ⇒ Response
Recover data stored at a given SAM key.
-
#get_file(key, type = :file, expected_checksum = nil) ⇒ Response
Recover a file stored at a given SAM key.
-
#initialize(params = {}) ⇒ Client
constructor
Initialize a client to a SAM node hosted at a specific url.
-
#store(data) ⇒ Hash
Store a given data in SAM.
-
#store_file(file_content, filename, type = :file) ⇒ Response
Store a file in SAM.
-
#update(key, value) ⇒ Response
Update data stored at a given SAM key.
-
#update_file(key, type = :file, new_content, filename) ⇒ Response
Update file stored at a given SAM key.
Constructor Details
#initialize(params = {}) ⇒ Client
Initialize a client to a SAM node hosted at a specific url
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
#expire ⇒ Object
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
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
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 |
#download_link_for_file(key) ⇒ Object
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
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
Use of wrong “type” parameter can generate errors.
Recover a file stored at a given SAM key
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
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.
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
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
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 |