kns_endpoint 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/endpoint.rb +67 -14
- metadata +7 -7
data/lib/endpoint.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
|
-
require '
|
1
|
+
require 'net/https'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
|
2
5
|
module Kynetx
|
3
6
|
|
4
7
|
class Endpoint
|
8
|
+
attr_accessor :session
|
5
9
|
|
6
10
|
@@events = {}
|
7
11
|
@@directives = {}
|
12
|
+
@@use_single_session = true;
|
8
13
|
|
9
14
|
def initialize
|
10
15
|
end
|
11
16
|
|
12
17
|
|
13
18
|
def self.event(e, params={}, &block)
|
14
|
-
|
15
19
|
@@events[e] = { :params => params }
|
16
|
-
|
17
20
|
@@events[e][:block] = block_given? ? block : lambda { |p| p }
|
18
|
-
|
19
|
-
|
20
21
|
end
|
21
22
|
|
22
23
|
def self.directive(d, &block)
|
@@ -25,12 +26,11 @@ module Kynetx
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def self.ruleset(r); @@ruleset = r end
|
28
|
-
|
29
29
|
def self.domain(d); @@domain = d end
|
30
|
+
def use_single_session; @@use_single_session end
|
31
|
+
def use_single_session=(uss); @@use_single_session = uss end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
def signal(e, params)
|
33
|
+
def signal(e, params={})
|
34
34
|
run_event(e, params)
|
35
35
|
end
|
36
36
|
|
@@ -41,15 +41,52 @@ module Kynetx
|
|
41
41
|
|
42
42
|
# setup the parameters and call the block
|
43
43
|
|
44
|
-
@@events
|
44
|
+
if @@events.keys.include? e
|
45
|
+
@@events[e][:block].call(params)
|
46
|
+
else
|
47
|
+
raise "Undefined event #{e.to_s}"
|
48
|
+
end
|
49
|
+
|
45
50
|
|
46
51
|
# run the event
|
47
|
-
|
48
|
-
|
52
|
+
|
53
|
+
kns_json = {"directives" => []}
|
54
|
+
|
55
|
+
begin
|
56
|
+
api_call = "https://cs.kobj.net/blue/event/#{@@domain.to_s}/#{e.to_s}/#{@@ruleset.to_s}"
|
57
|
+
uri = URI.parse(api_call)
|
58
|
+
http_session = Net::HTTP.new(uri.host, uri.port)
|
59
|
+
http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
60
|
+
http_session.use_ssl = true
|
61
|
+
|
62
|
+
headers = {
|
63
|
+
'Host'=> uri.host,
|
64
|
+
}
|
65
|
+
|
66
|
+
headers["Cookie"] = "SESSION_ID=#{@session}" if @session && @@use_single_session
|
67
|
+
|
68
|
+
timeout(30) do
|
69
|
+
http_session.start { |http|
|
70
|
+
req = Net::HTTP::Post.new(uri.path)
|
71
|
+
headers.each{|key, val| req.add_field(key, val)}
|
72
|
+
resp, data = http.request(req, params.to_url_params)
|
73
|
+
@session = parse_cookie(resp, 'SESSION_ID')
|
74
|
+
|
75
|
+
raise "Unexpected response from KNS (HTTP Error: #{resp.code} - #{resp.message})" unless resp.code == '200'
|
76
|
+
begin
|
77
|
+
kns_json = JSON.parse(data)
|
78
|
+
rescue
|
79
|
+
raise "Unexpected response from KNS (#{data})"
|
80
|
+
end
|
81
|
+
}
|
82
|
+
end
|
83
|
+
rescue Exception => e
|
84
|
+
raise "Unable to connect to KNS. (#{e.message})"
|
85
|
+
end
|
49
86
|
|
50
87
|
# execute the returned directives
|
51
88
|
directive_output = []
|
52
|
-
|
89
|
+
kns_json["directives"].each do |d|
|
53
90
|
o = run_directive(d["name"].to_sym, symbolize_keys(d["options"])) if @@directives.keys.include?(d["name"].to_sym)
|
54
91
|
directive_output.push o
|
55
92
|
end
|
@@ -82,11 +119,27 @@ module Kynetx
|
|
82
119
|
result
|
83
120
|
}
|
84
121
|
end
|
122
|
+
|
123
|
+
def parse_cookie(resp_hash, cookie)
|
124
|
+
cookie_str = resp_hash['set-cookie']
|
125
|
+
cookies = {}
|
126
|
+
cookie_str.split(";").map{|e| k,v = e.split('='); cookies[k] = v}
|
127
|
+
return cookies[cookie]
|
128
|
+
end
|
85
129
|
|
130
|
+
end
|
86
131
|
|
132
|
+
end
|
87
133
|
|
134
|
+
class Hash
|
135
|
+
def to_url_params
|
136
|
+
elements = []
|
137
|
+
self.each_pair do |k,v|
|
138
|
+
elements << "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}"
|
139
|
+
end
|
140
|
+
elements.join('&')
|
88
141
|
end
|
89
142
|
|
90
|
-
|
91
143
|
end
|
92
144
|
|
145
|
+
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Farmer
|
@@ -14,21 +14,21 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-09-01 00:00:00 -06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: json
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
|
-
-
|
29
|
-
- 5
|
28
|
+
- 1
|
30
29
|
- 2
|
31
|
-
|
30
|
+
- 4
|
31
|
+
version: 1.2.4
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
34
|
description: " Build a Kynetx Endpoint in Ruby. Adds the ability to raise, or signal, events on the KNS Platform with\n a simple DSL to your existing or new ruby scripts or applications.\n"
|