Class: GranicusPlatformAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/granicus-platform-api/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(granicus_site = nil, username = nil, password = nil, options = {}) ⇒ Client

create a client



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/granicus-platform-api/client.rb', line 89

def initialize(granicus_site=nil, username=nil, password=nil, options={})
  # setup our private members
  @options             = options
  @impersonation_token = nil
  @connected           = false

  # configure savon
  Savon.configure do |config|
    config.log = false
  end
  HTTPI.log = false

  # connect if we have a site and credentials
  unless granicus_site.nil?
    self.site = granicus_site
  end

  unless username.nil? or password.nil?
    (username, password)
  end
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



6
7
8
# File 'lib/granicus-platform-api/client.rb', line 6

def connected
  @connected
end

Class Method Details

.classmapObject

classmap for generating proper attributes! hash within savon calls



47
48
49
# File 'lib/granicus-platform-api/client.rb', line 47

def self.classmap
  @@classmap
end

.classmap=(obj) ⇒ Object



51
52
53
# File 'lib/granicus-platform-api/client.rb', line 51

def self.classmap=(obj)
  @@classmap = obj
end

.typecastsObject

typecasts ripped from rubiii/nori, adapted for xsd types



502
503
504
# File 'lib/granicus-platform-api/client.rb', line 502

def self.typecasts
  @@typecasts
end

.typecasts=(obj) ⇒ Object



506
507
508
# File 'lib/granicus-platform-api/client.rb', line 506

def self.typecasts=(obj)
  @@typecasts = obj
end

.typegeneratorsObject

mappings between soap types and our complex types this area should be rewritten to auto-generate data sets properly and refactored to separate standard xsd types from custom types in class map



12
13
14
# File 'lib/granicus-platform-api/client.rb', line 12

def self.typegenerators
  @@typegenerators
end

.typegenerators=(obj) ⇒ Object



16
17
18
# File 'lib/granicus-platform-api/client.rb', line 16

def self.typegenerators=(obj)
  @@typegenerators = obj
end

Instance Method Details

#add_clip_meta_data(clip_id, meta_data) ⇒ Object

add metadata to a clip



286
287
288
289
290
# File 'lib/granicus-platform-api/client.rb', line 286

def (clip_id, )
  call_soap_method(             :add_clip_meta_data, '//ns5:AddClipMetaDataResponse/KeyTable', {
      'ClipID'   => clip_id,
      'MetaData' => })
end

#attribute_of(value) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/granicus-platform-api/client.rb', line 425

def attribute_of(value)
  case value.class.to_s
    when 'Array'
      return {"xsi:type" => 'SOAP-ENC:Array'} if value.count == 0
      xsd_type = self.class.classmap[value[0].class.to_s.split('::').last]
      if xsd_type.nil?
        puts "Couldn't get array xsd:type for #{value[0].class}"
        {"xsi:type" => 'SOAP-ENC:Array'}
      else
        {"xsi:type" => 'SOAP-ENC:Array', "SOAP-ENC:arrayType" => "#{xsd_type}[#{value.count}]"}
      end
    else
      xsd_type = self.class.classmap[value.class.to_s.split('::').last]
      if xsd_type.nil?
        puts "Couldn't get xsd:type for #{value.class}"
        nil
      else
        {"xsi:type" => xsd_type}
      end
  end
end

#build_meta_tree(list, parent_id = 0) ⇒ Object

translate metadata list returned by the get_event and get_clip meta data functions into a tree



490
491
492
493
494
495
496
497
498
499
# File 'lib/granicus-platform-api/client.rb', line 490

def build_meta_tree(list, parent_id=0)
  tree = []
  list.each do |item|
    if item.ParentID == parent_id
      item.Children = build_meta_tree list, item.ID
      tree << item
    end
  end
  tree
end

#call_soap_method(method, returnfilter, args = {}) ⇒ Object

private



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/granicus-platform-api/client.rb', line 366

def call_soap_method(method, returnfilter, args={})
  debug     = @options[:debug]
  @response = @client.request method do
    soap.namespaces['xmlns:granicus'] = "http://granicus.com/xsd"
    soap.namespaces['xmlns:SOAP-ENC'] = "http://schemas.xmlsoap.org/soap/encoding/"
    soap.body                         = prepare_hash args
    if debug
      puts soap.body
    end
  end

  doc = Nokogiri::XML(@response.to_xml) do |config|
    config.noblanks
  end
  if debug
    puts doc
  end
  response = handle_response(doc.xpath(returnfilter, doc.root.namespaces)[0])
  if debug
    puts response
  end
  response
end

#connect(granicus_site, username, password) ⇒ Object

connect up to a site



121
122
123
124
125
126
127
128
129
# File 'lib/granicus-platform-api/client.rb', line 121

def connect(granicus_site, username, password)
  logout if @connected

  # create the client
  self.site = granicus_site

  # call login
   username, password
end

#create_camera(camera) ⇒ Object

create a camera



180
181
182
# File 'lib/granicus-platform-api/client.rb', line 180

def create_camera(camera)
  call_soap_method(:create_camera, '//ns4:CreateCameraResponse/CameraID', {'CameraData' => camera})
end

#create_event(event) ⇒ Object

create an event



214
215
216
# File 'lib/granicus-platform-api/client.rb', line 214

def create_event(event)
  call_soap_method(:create_event, '//ns4:CreateEventResponse/EventID', {'EventData' => event})
end

#delete_camera(camera_id) ⇒ Object

delete the requested camera



199
200
201
# File 'lib/granicus-platform-api/client.rb', line 199

def delete_camera(camera_id)
  call_soap_method(:delete_camera, '//ns4:DeleteCameraResponse', {'CameraID' => camera_id})
end

#delete_event(event_id) ⇒ Object

delete the requested event



234
235
236
# File 'lib/granicus-platform-api/client.rb', line 234

def delete_event(event_id)
  call_soap_method(:delete_event, '//ns4:DeleteEventResponse', {'EventID' => event_id})
end

#fetch_attachment(meta_id) ⇒ Object

fetch an attachment



293
294
295
# File 'lib/granicus-platform-api/client.rb', line 293

def fetch_attachment(meta_id)
  call_soap_method(:fetch_attachment, '//ns5:FetchAttachmentResponse/Attachment', {'MetaDataID' => meta_id})
end

#get_camera(camera_id) ⇒ Object

return the requested camera



185
186
187
# File 'lib/granicus-platform-api/client.rb', line 185

def get_camera(camera_id)
  call_soap_method(:get_camera, '//ns5:GetCameraResponse/camera', {'CameraID' => camera_id})
end

#get_camerasObject

return all of the cameras



175
176
177
# File 'lib/granicus-platform-api/client.rb', line 175

def get_cameras
  call_soap_method(:get_cameras, '//ns5:GetCamerasResponse/cameras')
end

#get_clip(clip_id) ⇒ Object

return the requested clip



335
336
337
# File 'lib/granicus-platform-api/client.rb', line 335

def get_clip(clip_id)
  call_soap_method(:get_clip, '//ns5:GetClipResponse/clip', {'ClipID' => clip_id})
end

#get_clip_by_uid(clip_uid) ⇒ Object

return the requested clip



345
346
347
# File 'lib/granicus-platform-api/client.rb', line 345

def get_clip_by_uid(clip_uid)
  call_soap_method(:get_clip_by_uid, '//ns5:GetClipByUIDResponse/clip', {'ClipUID' => clip_uid})
end

#get_clip_meta_data(clip_id) ⇒ Object

return all of the clip meta data



271
272
273
# File 'lib/granicus-platform-api/client.rb', line 271

def (clip_id)
  build_meta_tree call_soap_method(:get_clip_meta_data, '//ns5:GetClipMetaDataResponse/metadata', {'ClipID' => clip_id})
end

#get_clips(folder_id) ⇒ Object

return all of the clips



325
326
327
# File 'lib/granicus-platform-api/client.rb', line 325

def get_clips(folder_id)
  call_soap_method(:get_clips, '//ns5:GetClipsResponse/clips', {'FolderID' => folder_id})
end

#get_clips_by_foreign_id(foreign_id) ⇒ Object

return all of the clips with matching foreign id



330
331
332
# File 'lib/granicus-platform-api/client.rb', line 330

def get_clips_by_foreign_id(foreign_id)
  call_soap_method(:get_clips_by_foreign_id, '//ns5:GetClipsByForeignIDResponse/clips', {'ForeignID' => foreign_id})
end

#get_comments_by_agenda_item_uid(uid) ⇒ Object



315
316
317
# File 'lib/granicus-platform-api/client.rb', line 315

def get_comments_by_agenda_item_uid(uid)
  call_soap_method(:get_comments_by_agenda_item_uid, '//ns5:GetCommentsByAgendaItemUIDResponse/Comments', {'AgendaItemUID' => uid})
end

#get_comments_by_event_id(event_id) ⇒ Object



307
308
309
# File 'lib/granicus-platform-api/client.rb', line 307

def get_comments_by_event_id(event_id)
  call_soap_method(:get_comments_by_event_id, '//ns5:GetCommentsByEventIDResponse/Comments', {'EventID' => event_id})
end

#get_comments_by_event_uid(uid) ⇒ Object



311
312
313
# File 'lib/granicus-platform-api/client.rb', line 311

def get_comments_by_event_uid(uid)
  call_soap_method(:get_comments_by_event_uid, '//ns5:GetCommentsByEventUIDResponse/Comments', {'EventUID' => uid})
end

#get_current_user_logonObject

return the current logged on user name



164
165
166
# File 'lib/granicus-platform-api/client.rb', line 164

def get_current_user_logon
  call_soap_method(:get_current_user_logon, '//ns4:GetCurrentUserLogonResponse/Logon')
end

#get_event(event_id) ⇒ Object

return the requested event



219
220
221
# File 'lib/granicus-platform-api/client.rb', line 219

def get_event(event_id)
  call_soap_method(:get_event, '//ns5:GetEventResponse/event', {'EventID' => event_id})
end

#get_event_by_uid(event_uid) ⇒ Object

return the requested event by uid



224
225
226
# File 'lib/granicus-platform-api/client.rb', line 224

def get_event_by_uid(event_uid)
  call_soap_method(:get_event_by_uid, '//ns5:GetEventByUIDResponse/event', {'EventUID' => event_uid})
end

#get_event_meta_data(event_id) ⇒ Object

return all of the event meta data



247
248
249
# File 'lib/granicus-platform-api/client.rb', line 247

def (event_id)
  build_meta_tree call_soap_method(:get_event_meta_data, '//ns5:GetEventMetaDataResponse/metadata', {'EventID' => event_id})
end

#get_event_meta_data_by_uid(event_uid) ⇒ Object

return all of the event meta data by UID



252
253
254
# File 'lib/granicus-platform-api/client.rb', line 252

def (event_uid)
  build_meta_tree call_soap_method(:get_event_meta_data_by_uid, '//ns5:GetEventMetaDataByUIDResponse/metadata', {'EventUID' => event_uid})
end

#get_event_video_location(event_id) ⇒ Object



238
239
240
# File 'lib/granicus-platform-api/client.rb', line 238

def get_event_video_location(event_id)
  call_soap_method(:get_event_video_location, '//ns4:GetEventVideoLocationResponse/url', {'EventID' => event_id})
end

#get_event_video_location_by_uid(event_uid) ⇒ Object



242
243
244
# File 'lib/granicus-platform-api/client.rb', line 242

def get_event_video_location_by_uid(event_uid)
  call_soap_method(:get_event_video_location_by_uid, '//ns4:GetEventVideoLocationByUIDResponse/url', {'EventUID' => event_uid})
end

#get_eventsObject

return all of the events



204
205
206
# File 'lib/granicus-platform-api/client.rb', line 204

def get_events
  call_soap_method(:get_events, '//ns5:GetEventsResponse/events')
end

#get_events_by_foreign_id(foreign_id) ⇒ Object

return all of the events with matching foreign id



209
210
211
# File 'lib/granicus-platform-api/client.rb', line 209

def get_events_by_foreign_id(foreign_id)
  call_soap_method(:get_events_by_foreign_id, '//ns5:GetEventsByForeignIDResponse/events', {'ForeignID' => foreign_id})
end

#get_foldersObject

return all of the folders



320
321
322
# File 'lib/granicus-platform-api/client.rb', line 320

def get_folders
  call_soap_method(:get_folders, '//ns5:GetFoldersResponse/folders')
end

#get_meta_data(meta_id) ⇒ Object

get meta data by id



298
299
300
# File 'lib/granicus-platform-api/client.rb', line 298

def (meta_id)
  call_soap_method(:get_meta_data, '//ns5:GetMetaDataResponse/MetaData', {'MetaDataID' => meta_id})
end

#get_server(server_id) ⇒ Object

return the requested server



355
356
357
# File 'lib/granicus-platform-api/client.rb', line 355

def get_server(server_id)
  call_soap_method(:get_server, '//ns5:GetServerResponse/server', {'ServerID' => server_id})
end

#get_serversObject

get servers



350
351
352
# File 'lib/granicus-platform-api/client.rb', line 350

def get_servers
  call_soap_method(:get_servers, '//ns5:GetServersResponse/servers')
end

#get_settingsObject

get settings



360
361
362
# File 'lib/granicus-platform-api/client.rb', line 360

def get_settings
  call_soap_method(:get_settings, '//ns5:GetSettingsResponse/settings')
end

#handle_response(node) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/granicus-platform-api/client.rb', line 447

def handle_response(node)
  if node.is_a? Nokogiri::XML::NodeSet or node.is_a? Array then
    return node.map { |el| handle_response el }
  end
  return node.to_s unless node['type']
  typespace, type = node['type'].split(':')
  case typespace
    when 'xsd'
      proc = self.class.typecasts[type]
      unless proc.nil?
        proc.call(node.children[0].to_s)
      else
        puts "Unknown xsd:type: #{type}"
        node.children[0].to_s
      end
    when 'SOAP-ENC'
      if type == 'Array' then
        node.children.map { |element| handle_response element }
      else
        puts "Unknown SOAP-ENC:type: #{type}"
        node.to_s
      end
    else
      # we have a custom type, attempt to generate it. if that fails use a hash
      proc  = self.class.typegenerators[type]
      value = {}
      unless proc.nil?
        value = proc.call
      else
        puts "Unknown custom type: #{type}"
      end
      node.children.each do |value_node|
        begin
          value[value_node.name] = handle_response value_node
        rescue
          # should log warning message here, but need to implement logging
        end
      end
      value
  end
end

#impersonate(token) ⇒ Object

impersonate a user



146
147
148
149
# File 'lib/granicus-platform-api/client.rb', line 146

def impersonate(token)
  @impersonation_token           = token
  @client.http.headers["Cookie"] = "SESS1=#{token}; path=/"
end

#impersonation_tokenObject



151
152
153
# File 'lib/granicus-platform-api/client.rb', line 151

def impersonation_token
  @impersonation_token
end

#import_clip_meta_data(clip_id, meta_data, clear_existing = true, as_tree = true) ⇒ Object

import metadata for a clip ImportClipMetaData Method (ClipID, MetaData, ClearExisting, AsTree)



277
278
279
280
281
282
283
# File 'lib/granicus-platform-api/client.rb', line 277

def (clip_id, , clear_existing=true, as_tree=true)
  call_soap_method(:import_clip_meta_data, '//ns5:ImportClipMetaDataResponse/KeyTable', {
      'ClipID'        => clip_id,
      'MetaData'      => ,
      'ClearExisting' => clear_existing,
      'AsTree'        => as_tree})
end

#import_event_meta_data(event_id, meta_data, clear_existing = true, as_tree = true) ⇒ Object

import metadata for an event



257
258
259
260
261
262
263
# File 'lib/granicus-platform-api/client.rb', line 257

def (event_id, , clear_existing=true, as_tree=true)
  call_soap_method(:import_event_meta_data, '//ns5:ImportEventMetaDataResponse/KeyTable', {
      'EventID'       => event_id,
      'MetaData'      => ,
      'ClearExisting' => clear_existing,
      'AsTree'        => as_tree})
end

#login(username, password) ⇒ Object

login



156
157
158
159
160
161
# File 'lib/granicus-platform-api/client.rb', line 156

def (username, password)
  logout if @connected
  call_soap_method(:login, '//ns4:LoginResponse/return', {'Username' => username, 'Password' => password})
  @impersonation_token = @response.http.headers['Set-Cookie'].gsub(/SESS1=(.*); path=\//, '\\1')
  @connected           = true
end

#logoutObject

logout



169
170
171
172
# File 'lib/granicus-platform-api/client.rb', line 169

def logout
  call_soap_method(:logout, '//ns4:LogoutResponse')
  @connected = false
end

#optionsObject

options



112
113
114
# File 'lib/granicus-platform-api/client.rb', line 112

def options
  @options
end

#options=(value) ⇒ Object



116
117
118
# File 'lib/granicus-platform-api/client.rb', line 116

def options=(value)
  @options = value
end

#prepare_array(array) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/granicus-platform-api/client.rb', line 409

def prepare_array(array)
  return {"item" => array} if array.count == 0
  new_array = []
  array.each do |item|
    case item.class.to_s
      when /GranicusPlatformAPI::/, 'Hash'
        new_array << prepare_hash(item)
      when 'Array'
        new_array << prepare_array(item)
      else
        new_array << item
    end
  end
  {"item" => new_array, :attributes! => {"item" => attribute_of(array[0])}}
end

#prepare_hash(hash = {}) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/granicus-platform-api/client.rb', line 390

def prepare_hash(hash={})
  attributes = {}
  new_hash   = {}
  hash.each do |key, value|
    case value.class.to_s
      when /GranicusPlatformAPI::/, 'Hash'
        new_hash[key] = prepare_hash value
      when 'Array'
        new_hash[key] = prepare_array value
      when 'File'
        new_hash[key] = Base64.encode64(value.read().force_encoding('BINARY'))
      else
        new_hash[key] = value
    end
    attributes[key] = attribute_of value
  end
  new_hash.merge({:attributes! => attributes})
end

#savonObject



189
190
191
# File 'lib/granicus-platform-api/client.rb', line 189

def savon
  @client
end

#set_event_agenda_url(event_id, url) ⇒ Object

set the event agenda url



266
267
268
# File 'lib/granicus-platform-api/client.rb', line 266

def set_event_agenda_url(event_id, url)
  call_soap_method(:set_event_agenda_url, '//ns4:SetEventAgendaURLResponse', {'EventID' => event_id, 'URL' => url})
end

#siteObject

site property



132
133
134
# File 'lib/granicus-platform-api/client.rb', line 132

def site
  return @granicus_site
end

#site=(value) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/granicus-platform-api/client.rb', line 136

def site=(value)
  @granicus_site = value
  @client        = Savon::Client.new do |wsdl, http|
    wsdl.document = File.expand_path("../granicus-platform-api.xml", __FILE__)
    wsdl.endpoint = "http://#{value}/SDK/User/index.php"
    http.proxy = @options[:proxy] if not @options[:proxy].nil?
  end
end

#update_camera(camera) ⇒ Object

update a camera



194
195
196
# File 'lib/granicus-platform-api/client.rb', line 194

def update_camera(camera)
  call_soap_method(:update_camera, '//ns4:UpdateCameraResponse', {'camera' => camera})
end

#update_clip(clip) ⇒ Object

update a clip



340
341
342
# File 'lib/granicus-platform-api/client.rb', line 340

def update_clip(clip)
  call_soap_method(:update_clip, '//ns4:UpdateClipResponse', {'clip' => clip})
end

#update_event(event) ⇒ Object

update an event



229
230
231
# File 'lib/granicus-platform-api/client.rb', line 229

def update_event(event)
  call_soap_method(:update_event, '//ns4:UpdateEventResponse', {'event' => event})
end

#update_meta_data(meta_data) ⇒ Object

update metadata



303
304
305
# File 'lib/granicus-platform-api/client.rb', line 303

def ()
  call_soap_method(:update_meta_data, '//ns4:UpdateMetaDataResponse', {'MetaData' => })
end