Class: RsServiceNow::Record

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

Direct Known Subclasses

Ci, Company

Instance Method Summary collapse

Constructor Details

#initialize(user, password, instance) ⇒ Record

Returns a new instance of Record.



7
8
9
10
11
# File 'lib/rs_service_now/record.rb', line 7

def initialize user, password, instance
  @user = user
  @password = password
  @url = "https://#{instance}.service-now.com"
end

Instance Method Details

#_export(table, encoded_query, max_export) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rs_service_now/record.rb', line 34

def _export table, encoded_query, max_export
  client = setup_client table
  get_keys_result = get_keys(client, encoded_query)
  num_entries = get_keys_result[:count].to_i
  keys = get_keys_result[:sys_id].split(/,/)
  index = 0
  export = []

  while index < num_entries
    # Use a URL to retrieve an XML export as documented at http://wiki.servicenow.com/index.php?title=Exporting_Data

    xml = open("#{@url}/#{table}.do?XML&" +
                       "sysparm_order_by=sys_id&" +
                       "sysparm_query=#{CGI.escape(encoded_query + '^')}" +
                       "sys_id#{CGI.escape('>=') + keys[index]}&" +
                       "sysparm_record_count=#{max_export}",
                   :http_basic_authentication => [@user, @password]
      ).read

    records = Hash.from_xml(xml)["xml"][table]
    if records.is_a? Array
      export.push *records
    else
      export.push records
    end

    index = index + max_export
  end

  export
end

#_request(table, encoded_query) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rs_service_now/record.rb', line 13

def _request table, encoded_query
  client = setup_client table
  num_entries = get_keys(client, encoded_query)[:count].to_i
  index = 0
  result = []

  while index < num_entries
    response = client.call(:get_records, :message => {:__encoded_query => encoded_query, :__first_row => index, :__last_row => (index + 250)})
    index = index + 250

    records = response.hash[:envelope][:body][:get_records_response][:get_records_result]
    if records.is_a? Array
      result.push *records
    else
      result.push records
    end
  end

  result
end

#get_keys(client, encoded_query) ⇒ Object



65
66
67
68
# File 'lib/rs_service_now/record.rb', line 65

def get_keys client, encoded_query
  response = client.call(:get_keys, :message => {:__encoded_query => encoded_query, :__order_by => "sys_id"})
  response.hash[:envelope][:body][:get_keys_response]
end

#setup_client(table) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/rs_service_now/record.rb', line 70

def setup_client table
  Savon.client do |globals|
    globals.wsdl "#{@url}/#{table}.do?WSDL"
    globals.basic_auth [@user, @password]
    globals.convert_request_keys_to :none
    globals.namespace_identifier :u
  end
end