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



19
20
21
22
# File 'lib/vws.rb', line 19

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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vws.rb', line 73

def add_target(target_name, file_path, width, active_flag)
  date_timestamp = Time.now.httpdate 
  #for file uploads, read file contents data and Base 64 encode it:

  contents_encoded = Base64.encode64(File.open(file_path, 'rb').read)
  body_hash = { :name => target_name, 
                :width => width, #width of the target in scene units

                :image => contents_encoded, 
                :active_flag => active_flag }
  signature = self.build_signature('/targets', body_hash, 'POST', date_timestamp)
  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



24
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
# File 'lib/vws.rb', line 24

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";
    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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
# File 'lib/vws.rb', line 184

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)
          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_targetsObject

Calls the api end point for the list of targets associated with server access key and cloud database



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vws.rb', line 58

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)
  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



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

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)
  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



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

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)
  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



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vws.rb', line 119

def summary
  date_timestamp = Time.now.httpdate
  signature = self.build_signature('/summary', nil, 'GET', date_timestamp) 
  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



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vws.rb', line 149

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) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vws.rb', line 94

def update_target(target_id, target_name=nil, file_path=nil, width=nil, active_flag=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(File.open(file_path, 'rb').read)
  body_hash = { :name => target_name, 
                :width => width, #Width of the target in scene unit

                :image => contents_encoded, 
                :active_flag => active_flag }
  signature = self.build_signature(target_id_suburl, body_hash, 'PUT', date_timestamp)
  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