Class: Vws::Api
- Inherits:
-
Object
- Object
- Vws::Api
- Defined in:
- lib/vws.rb
Instance Method Summary collapse
- #add_target(target_name, file_path, width, active_flag, metadata = nil) ⇒ Object
- #build_signature(request_path, body_hash, http_verb, timestamp) ⇒ Object
-
#delete_target(target_id) ⇒ Object
Delete a target in a cloud database developer.vuforia.com/library/articles/Solution/How-To-Delete-a-Target-Using-the-VWS-API.
-
#initialize(accesskey = nil, secretkey = nil) ⇒ Api
constructor
A new instance of Api.
- #list_duplicates(target_id) ⇒ Object
-
#list_targets ⇒ Object
Calls the api end point for the list of targets associated with server access key and cloud database developer.vuforia.com/library/articles/Solution/How-To-Get-a-Target-List-for-a-Cloud-Database-Using-the-VWS-API.
-
#retrieve_target(target_id) ⇒ Object
Retrieve a target from the cloud database developer.vuforia.com/resources/dev-guide/retrieving-target-cloud-database.
- #set_active_flag(target_id, active_flag) ⇒ Object
-
#summary ⇒ Object
Database Summary Report developer.vuforia.com/resources/dev-guide/database-summary-report.
-
#target_summary(target_id) ⇒ Object
Target Summary Report developer.vuforia.com/resources/dev-guide/target-summary-report.
- #update_target(target_id, target_name = nil, file_path = nil, width = nil, active_flag = nil, metadata = nil) ⇒ Object
Constructor Details
#initialize(accesskey = nil, secretkey = nil) ⇒ Api
Returns a new instance of Api.
20 21 22 23 |
# File 'lib/vws.rb', line 20 def initialize(accesskey=nil, secretkey=nil) @accesskey = accesskey || ENV['VWS_ACCESSKEY'] @secretkey = secretkey || ENV['VWS_SECRETKEY'] end |
Instance Method Details
#add_target(target_name, file_path, width, active_flag, metadata = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vws.rb', line 77 def add_target(target_name, file_path, width, active_flag, =nil) raise "file path is required" if file_path.nil? raise "target name is required" if target_name.nil? = Time.now.httpdate #for file uploads, read file contents data and Base 64 encode it: contents_encoded = Base64.encode64(open(file_path) { |io| io.read }) = Base64.encode64(.to_s) body_hash = { :name => target_name, :width => width, #width of the target in scene units :image => contents_encoded, :active_flag => active_flag, :application_metadata => } signature = self.build_signature('/targets', body_hash, 'POST', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.post(TARGETS_URL, body_hash.to_json, :'Date' => , :'Authorization' => , :content_type => 'application/json', :accept => :json) rescue => e e.response end end |
#build_signature(request_path, body_hash, http_verb, timestamp) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vws.rb', line 25 def build_signature(request_path, body_hash, http_verb, ) # request_path signifies the suburi you call after you subtract the # BASE_URL; that is, if you call https://vws/vuforia.com/targets, # the request_path is '/targets' contentType = "" hexDigest = "d41d8cd98f00b204e9800998ecf8427e" # Hex digest of an empty # string. We use it to # signify empty body if http_verb == "GET" || http_verb == "DELETE" # Do nothing since we have already set contentType and hexDigest elsif http_verb == "POST" || http_verb == "PUT" contentType = "application/json"; # the request should have a request body, so create an md5 hash of that # json body data hexDigest = Digest::MD5.hexdigest(body_hash.to_json) else puts "Invalid request method for signature method: " + http_verb return nil end toDigest = http_verb + "\n" + hexDigest + "\n" + contentType + "\n" + + "\n" + request_path return Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, @secretkey, toDigest)) end |
#delete_target(target_id) ⇒ Object
Delete a target in a cloud database developer.vuforia.com/library/articles/Solution/How-To-Delete-a-Target-Using-the-VWS-API
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/vws.rb', line 198 def delete_target(target_id) # In order to delete the target, we have to set it to non-active. # Therefore,first retrieve target info and act accordingly to target info # returned target_data = JSON.parse(retrieve_target(target_id)) # have to JSON.parse # retrieve_target's results since they're in string format target_result_code = target_data["result_code"] if target_result_code != "AuthenticationFailure" if target_result_code != "UnknownTarget" target_active_flag = target_data["target_record"]["active_flag"] target_status = target_data["status"] if target_result_code == "Success" if target_active_flag == true && target_status == "success" return {:result_code => "TargetActive"}.to_json elsif target_active_flag == false && target_status == "success" # if we reached this point, the target is fine, inactive and # ready to be deleted = Time.now.httpdate target_id_url = TARGETS_URL + '/' + target_id target_id_suburl = '/targets' + '/' + target_id signature = self.build_signature(target_id_suburl, nil, 'DELETE', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.delete(target_id_url, :'Date' => , :'Authorization' => ) rescue => e e.response end else return {:result_code => "#{target_status}"}.to_json end end else return {:result_code => "UnknownTarget"}.to_json end else return {:result_code => "AuthenticationFailure"}.to_json end end |
#list_duplicates(target_id) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/vws.rb', line 240 def list_duplicates(target_id) = Time.now.httpdate target_id_url = DUPLICATES_URL + '/' + target_id target_id_suburl = '/duplicates' + '/' + target_id signature = self.build_signature(target_id_suburl, nil, 'GET', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.get(target_id_url, :'Date' => , :'Authorization' => ) rescue => e e.response end end |
#list_targets ⇒ Object
Calls the api end point for the list of targets associated with server access key and cloud database developer.vuforia.com/library/articles/Solution/How-To-Get-a-Target-List-for-a-Cloud-Database-Using-the-VWS-API
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/vws.rb', line 60 def list_targets #Date is the current date per RFC 2616, section 3.3.1, #rfc1123-date format, e.g.: Sun, 22 Apr #2012 08:49:37 GMT. = Time.now.httpdate #ruby provides this date format #with httpdate method signature = self.build_signature('/targets', nil, 'GET', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.get(TARGETS_URL, :'Date' => , :'Authorization' => ) rescue => e e.response end end |
#retrieve_target(target_id) ⇒ Object
Retrieve a target from the cloud database developer.vuforia.com/resources/dev-guide/retrieving-target-cloud-database
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/vws.rb', line 146 def retrieve_target(target_id) = Time.now.httpdate target_id_url = TARGETS_URL + '/' + target_id target_id_suburl = '/targets' + '/' + target_id signature = self.build_signature(target_id_suburl, nil, 'GET', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.get(target_id_url, :'Date' => , :'Authorization' => ) rescue => e e.response end end |
#set_active_flag(target_id, active_flag) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/vws.rb', line 177 def set_active_flag(target_id, active_flag) = Time.now.httpdate target_id_url = TARGETS_URL + '/' + target_id target_id_suburl = '/targets' + '/' + target_id body_hash = {:active_flag => active_flag} signature = self.build_signature(target_id_suburl, body_hash, 'PUT', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.put(target_id_url, body_hash.to_json, :'Date' => , :'Authorization' => , :content_type => 'application/json', :accept => :json) rescue => e e.response end end |
#summary ⇒ Object
Database Summary Report developer.vuforia.com/resources/dev-guide/database-summary-report
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/vws.rb', line 131 def summary = Time.now.httpdate signature = self.build_signature('/summary', nil, 'GET', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.get(SUMMARY_URL, :'Date' => , :'Authorization' => ) rescue => e e.response end end |
#target_summary(target_id) ⇒ Object
Target Summary Report developer.vuforia.com/resources/dev-guide/target-summary-report
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/vws.rb', line 163 def target_summary(target_id) = Time.now.httpdate target_id_url = SUMMARY_URL + '/' + target_id target_id_suburl = '/summary' + '/' + target_id signature = self.build_signature(target_id_suburl, nil, 'GET', ) = "VWS " + @accesskey + ":" + signature begin RestClient.get(target_id_url, :'Date' => , :'Authorization' => ) rescue => e e.response end end |
#update_target(target_id, target_name = nil, file_path = nil, width = nil, active_flag = nil, metadata = nil) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/vws.rb', line 103 def update_target(target_id, target_name=nil, file_path=nil, width=nil, active_flag=nil, =nil) = Time.now.httpdate target_id_url = TARGETS_URL + '/' + target_id target_id_suburl = '/targets' + '/' + target_id #for file uploads, read file contents data and Base 64 encode it: contents_encoded = Base64.encode64(open(file_path) { |io| io.read }) = Base64.encode64(.to_s) body_hash = { :name => target_name, :width => width, #Width of the target in scene unit :image => contents_encoded, :active_flag => active_flag, :application_metadata => } signature = self.build_signature(target_id_suburl, body_hash, 'PUT', ) raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil = "VWS " + @accesskey + ":" + signature begin RestClient.put(target_id_url, body_hash.to_json, :'Date' => , :'Authorization' => , :content_type => 'application/json', :accept => :json) rescue => e e.response end end |