Module: ServiceNow

Defined in:
lib/version.rb,
lib/em-service-now.rb

Defined Under Namespace

Classes: ConfigException, RuntimeException

Constant Summary collapse

Version =
VERSION = '0.3.1'

Instance Method Summary collapse

Instance Method Details

#em_service_now(type, query, records_array = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/em-service-now.rb', line 54

def em_service_now( type, query, records_array = nil, &block )

  @service_now_inline = false

  EM::run do

    service_now( type, query, records_array, &block )
    
  end
  
end

#service_now(type, query, records_array = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
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
102
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
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/em-service-now.rb', line 66

def service_now( type, query, records_array = nil, &block )
  
  @service_now_running = true
  
  url = nil
  
  table = query['table']
  
  case type
    
  when :getRecords, :getKeys, :getCSV
    
    return sn_query( type, query, &block )
    
  when :insert

    url = "#{@url}/#{table}.do?JSON&sysparm_action=insertMultiple"
    body = {

      'records' => records_array

    }
    
  when :update
    
    url = "#{@url}/#{table}.do?JSON" 
    url="#{url}&sysparm_action=update"
    url = sn_append_query( url, query )
    body = records_array[0]
    
  else
    
    raise ServiceNow::RuntimeException.new(
      
      "no such query type '#{type}'"
    
    )
    
  end
  
  json_body = JSON.dump(body) 
  
  @logger.debug "ServiceNow URL:#{url}" unless @logger.nil?
  
  http = EventMachine::HttpRequest.new( url ).post(
    
      sn_generate_POST_request( json_body )
    
  )
  
  http.errback do
    
    sn_on_error( url, http, &block )
    
  end
  
  http.callback do
    
    if http.response_header.status == 401
      
      #
      # Authentication failed 
      #
      ( url, json_body, &block )
              
    else
    
      sn_on_success( url, http, &block )
    
    end
    
  end
  
end

#service_now_init(config, logger = nil) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/em-service-now.rb', line 13

def service_now_init( config, logger = nil )
  
  @config = {}
  @config = config unless config.nil?
  
  @logger = logger
  
  @connect_timeout = 60
  @connect_timeout = @config[:connect_timeout] unless @config[:connect_timeout].nil?
  
  @timeout = 120
  @timeout = @config[:timeout] unless @config[:timeout].nil?
  
  @username = @config[:username]
  @password = @config[:password]
  @url = @config[:url]
  
  @service_now_running = false
  
  raise ConfigException.new(
  
    "#{self.class} requires :url: and :username: and :password: configured."
  
  ) if (

    @username.nil? or @username == '' or
    @password.nil? or @password == '' or
    @url.nil? or @url == ''
    
  )
  
  #
  # Assumes already running inside an EM reactor
  # If not, call em_service_now
  # 
  @service_now_inline = true
  
  sn_get_cookies
      
end