lib/nsisam/client.rb in nsisam-0.3.0 vs lib/nsisam/client.rb in nsisam-0.3.1
- old
+ new
@@ -23,10 +23,13 @@
#
# @param [String] data the desired data to store
# @return [Hash] response with the data key and checksum
# * "key" [String] the key to access the stored data
# * "checksum" [String] the sha1 checksum of the stored data
+ #
+ # @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
+ #
# @example
# nsisam.store("something")
def store(data)
request_data = {:value => data}.to_json
request = prepare_request :PUT, request_data
@@ -36,11 +39,14 @@
# Delete data at a given SAM key
#
# @param [Sring] key of the value to delete
# @return [Hash] response
# * "deleted" [Boolean] true if the key was successfully deleted
- # @raise [NSISam::Errors::Client::KeyNotFoundError] When the key doesn't exists
+ #
+ # @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
+ # @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
+ #
# @example Deleting an existing key
# nsisam.delete("some key")
def delete(key)
request_data = {:key => key}.to_json
request = prepare_request :DELETE, request_data
@@ -52,11 +58,14 @@
# @param [String] key of the value to acess
# @return [Hash] response
# * "from_user" [String] the user who stored the value
# * "date" [String] the date when the value was stored
# * "data" [String, Hash, Array] the data stored at that key
- # @raise [NSISam::Errors::Client::KeyNotFoundError] When the key doesn't exists
+ #
+ # @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
+ # @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
+ #
# @example
# nsisam.get("some key")
def get(key, expected_checksum=nil)
request_data = {:key => key}.to_json
request = prepare_request :GET, request_data
@@ -70,11 +79,14 @@
# @param [String] key of the data to update
# @param [String, Hash, Array] data to be stored at the key
# @return [Hash] response
# * "key" [String] just to value key again
# * "checksum" [String] the new sha1 checksum of the key's data
- # @raise [NSISam::Errors::Client::KeyNotFoundError] When the key doesn't exists
+ #
+ # @raise [NSISam::Errors::Client::KeyNotFoundError] when the key doesn't exists
+ # @raise [NSISam::Errors::Client::AuthenticationError] when user and password doesn't match
+ #
# @example
# nsisam.update("my key", "my value")
def update(key, value)
request_data = {:key => key, :value => value}.to_json
request = prepare_request :POST, request_data
@@ -94,9 +106,11 @@
def execute_request(request)
response = Net::HTTP.start @url, @port do |http|
http.request(request)
end
raise NSISam::Errors::Client::KeyNotFoundError if response.code == "404"
+ raise NSISam::Errors::Client::MalformedRequestError if response.code == "400"
+ raise NSISam::Errors::Client::AuthenticationError if response.code == "401"
JSON.parse(response.body)
end
def verify_checksum(data, expected_checksum)
sha1_checksum = Digest::SHA1.hexdigest(data)