Class: SDEE::Poller

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Poller

Returns a new instance of Poller.



9
10
11
12
13
14
15
# File 'lib/sdee/poller.rb', line 9

def initialize(options = {})
  @host = options[:host]
  @path = '/cgi-bin/sdee-server'
  @proto = 'https://'

  @creds = Base64.encode64("#{options[:user]}:#{options[:pass]}")
end

Instance Method Details

#get_eventsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sdee/poller.rb', line 44

def get_events
  puts "Please login first" unless @subscription_id

  params = {
    action: 'get',
    confirm: 'yes',
    timeout: 1,
    maxNbrofEvents: 20,
    subscriptionId: @subscription_id,
    sessionId: @session_id
  }

  res = request params
  doc = Nokogiri::XML(res.body)

  xml_alerts = doc.xpath("//sd:evIdsAlert")
  hash_alerts = xml_alerts.collect {|x| Alert.new(x.to_s).to_hash }.uniq

  hash_alerts.each {|h| puts h.to_json }

  hash_alerts
end

#loginObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sdee/poller.rb', line 17

def 
  params = {
    action: 'open',
    events: 'evIdsAlert',
    force: 'yes'
  }

  response = request params
  doc = Nokogiri::XML(response.body)

  @session_id = doc.xpath('//env:Header').first.
    xpath('//sd:oobInfo').first.
    xpath('//sd:sessionId').first.text

  @subscription_id = doc.xpath('//env:Body').first.
    xpath('//sd:subscriptionId').first.text

  response
end

#poll_events(sleep_time = 5) ⇒ Object



37
38
39
40
41
42
# File 'lib/sdee/poller.rb', line 37

def poll_events(sleep_time=5)
  while true do
    get_events
    sleep sleep_time
  end
end

#request(params) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sdee/poller.rb', line 67

def request(params)
  http = Net::HTTP.new(@host, 443)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.ssl_version = :SSLv3

  uri = URI(@proto + @host + @path)
  uri.query = URI.encode_www_form(params)

  req = Net::HTTP::Get.new(uri)
  req['Authorization'] = "BASIC #{@creds}"

  http.request(req)
end