Class: Vws::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/vws.rb

Instance Method Summary collapse

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

Raises:

  • (ArgumentError)


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?
  date_timestamp = 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', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
  authorization_header = "VWS " + @accesskey + ":" +  signature
  begin
    RestClient.post(TARGETS_URL, body_hash.to_json,
                                :'Date' => date_timestamp,
                                :'Authorization' => authorization_header,
                                :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, timestamp)
  # 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" +
              timestamp   + "\n" +
              request_path

  return Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new,
                          @secretkey, toDigest))
end

#delete_target(target_id) ⇒ Object



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
          date_timestamp = 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', date_timestamp)
          raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
          authorization_header = "VWS " + @accesskey + ":" + signature
            begin
              RestClient.delete(target_id_url,
                                         :'Date' => date_timestamp,
                                         :'Authorization' => authorization_header)
            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

Raises:

  • (ArgumentError)


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)
  date_timestamp = 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', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
  authorization_header = "VWS " + @accesskey + ":" +  signature
  begin
    RestClient.get(target_id_url, :'Date' => date_timestamp,
                                  :'Authorization' => authorization_header)
  rescue => e
    e.response
  end
end

#list_targetsObject

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

Raises:

  • (ArgumentError)


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.
  date_timestamp = Time.now.httpdate #ruby provides this date format
                                     #with httpdate method
  signature = self.build_signature('/targets', nil, 'GET', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil

  authorization_header = "VWS " + @accesskey + ":" +  signature
  begin
    RestClient.get(TARGETS_URL, :'Date' => date_timestamp,
                                :'Authorization' => authorization_header)
  rescue => e
    e.response
  end
end

#retrieve_target(target_id) ⇒ Object

Raises:

  • (ArgumentError)


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)
  date_timestamp = 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', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
  authorization_header = "VWS " + @accesskey + ":" + signature
  begin
    RestClient.get(target_id_url, :'Date' => date_timestamp,
                                  :'Authorization' => authorization_header)
  rescue => e
    e.response
  end
end

#set_active_flag(target_id, active_flag) ⇒ Object

Raises:

  • (ArgumentError)


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)
  date_timestamp = 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', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
  authorization_header = "VWS " + @accesskey + ":" + signature
  begin
    RestClient.put(target_id_url, body_hash.to_json,
                                  :'Date' => date_timestamp,
                                  :'Authorization' => authorization_header,
                                  :content_type => 'application/json',
                                  :accept => :json)
  rescue => e
      e.response
  end
end

#summaryObject

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/vws.rb', line 131

def summary
  date_timestamp = Time.now.httpdate
  signature = self.build_signature('/summary', nil, 'GET', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
  authorization_header = "VWS " + @accesskey + ":" + signature
  begin
    RestClient.get(SUMMARY_URL, :'Date' => date_timestamp,
                                :'Authorization' => authorization_header)
  rescue => e
    e.response
  end
end

#target_summary(target_id) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vws.rb', line 163

def target_summary(target_id)
  date_timestamp = 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', date_timestamp)
  authorization_header = "VWS " + @accesskey + ":" + signature
  begin
    RestClient.get(target_id_url, :'Date' => date_timestamp,
                                  :'Authorization' => authorization_header)
  rescue => e
    e.response
  end
end

#update_target(target_id, target_name = nil, file_path = nil, width = nil, active_flag = nil, metadata = nil) ⇒ Object

Raises:

  • (ArgumentError)


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)
  date_timestamp = 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', date_timestamp)
  raise ArgumentError.new('Signature returned nil. Aborting...') if signature == nil
  authorization_header = "VWS " + @accesskey + ":" +  signature
  begin
    RestClient.put(target_id_url, body_hash.to_json,
                                :'Date' => date_timestamp,
                                :'Authorization' => authorization_header,
                                :content_type => 'application/json',
                                :accept => :json)
  rescue => e
    e.response
  end
end