eep_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0f27b56c72f0be65e77fab3edee05c69a69f1c7
4
+ data.tar.gz: 4f1714fb51a69cda64e381126ef38d873dd4afd9
5
+ SHA512:
6
+ metadata.gz: ab6f685df7ddd049f674ea1c26f41e406c3e1e4a2980e3bdb78bde1f8389db3578d9e7208bc491819d9a0eb8165710dc6b0302e18b0a8c177803a6dc4d7d90e7
7
+ data.tar.gz: 03a8577eee00ab6588b481cbe33c3b8eef8919ad5be317bf1dc95cb7a00a595f8b2cfeb103f4d09af15f968658ca94aa99c05b7059f831a7adb057d08db3a1f8
data/lib/eep_client.rb ADDED
@@ -0,0 +1,104 @@
1
+ require 'time'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'json'
5
+ require_relative 'eep_client/const'
6
+ require_relative 'eep_client/response'
7
+
8
+ ## some constants
9
+ EVENT = 'event'
10
+ CLEAR = 'clear'
11
+ OK = 'ok'
12
+
13
+ # urls
14
+ BASE_URL = 'https://bender.lab.pdist.net'
15
+ BASE_RESOURCE = '/api/v1/'
16
+ EVENT_RESOURCE = BASE_RESOURCE + EVENT
17
+ CLEAR_RESOURCE = BASE_RESOURCE + CLEAR
18
+
19
+ # http config
20
+ POST = 'POST'
21
+ ACC_HDR = 'Accept'
22
+ CON_HDR = 'Content-type'
23
+ APP_JSON = 'application/json'
24
+
25
+ class EepClient
26
+ def initialize(api_token, options = { })
27
+ @api_token = api_token
28
+ @debug = options[:debug]
29
+
30
+ unless options[:no_send]
31
+ uri = URI(BASE_URL)
32
+ @http = Net::HTTP.new(uri.host, uri.port)
33
+ @http.use_ssl = true
34
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if options[:no_verify]
35
+ @http.set_debug_output($stdout) if @debug
36
+ end
37
+ end
38
+
39
+ def send_event(event)
40
+ json = format_event(event)
41
+ if @debug
42
+ puts "sending event json:"
43
+ puts json
44
+ end
45
+ if @http
46
+ headers = { ACC_HDR => APP_JSON, CON_HDR => APP_JSON }
47
+ res = @http.send_request(POST, EVENT_RESOURCE, json, headers).body
48
+ Response.new_instance(JSON.parse(res))
49
+ else
50
+ Response.mock_event_ok
51
+ end
52
+ end
53
+
54
+ def send_clear(clear)
55
+ json = format_clear(clear)
56
+ if @debug
57
+ puts "sending clear json:"
58
+ puts json
59
+ end
60
+ if @http
61
+ headers = { ACC_HDR => APP_JSON, CON_HDR => APP_JSON }
62
+ res = @http.send_request(POST, CLEAR_RESOURCE, json, headers).body
63
+ Response.new_instance(JSON.parse(res))
64
+ else
65
+ Response.mock_clear_ok
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def format_data(type, data)
72
+ packet = {
73
+ api_token: @api_token,
74
+ type => data
75
+ }
76
+
77
+ if @debug
78
+ JSON.pretty_generate(packet)
79
+ else
80
+ JSON.generate(packet)
81
+ end
82
+ end
83
+
84
+ def format_event(data)
85
+ # event time attrs can take strings (iso8601 format), ints (unix epoch) or Time objects
86
+ [:creation_time, :last_time].each do |name|
87
+ val = data[name]
88
+ if val
89
+ case
90
+ when val.is_a?(Fixnum)
91
+ data[name] = Time.at(val).iso8601
92
+ when val.is_a?(Time)
93
+ data[name] = val.iso8601
94
+ end
95
+ end
96
+ end
97
+ format_data(:event, data)
98
+ end
99
+
100
+ def format_clear(data)
101
+ h = { :local_instance_id => data[:local_instance_id], :source_location => data[:source_location] }
102
+ format_data(:clear, h)
103
+ end
104
+ end
@@ -0,0 +1,15 @@
1
+ class EepClient
2
+ module Const
3
+ # severities
4
+ SEV_INFO = 'info'
5
+ SEV_WARNING = 'warning'
6
+ SEV_ERROR = 'error'
7
+ SEV_CRITICAL = 'critical'
8
+
9
+ # priorities
10
+ PRI_NONE = 'none'
11
+ PRI_LOW = 'low'
12
+ PRI_MEDIUM = 'medium'
13
+ PRI_HIGH = 'high'
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ class EepClient
2
+ class Response
3
+ OK = 'ok'
4
+ MESSAGE = 'message'
5
+ STATUS = 'status'
6
+ ID = 'id'
7
+ MESSAGES = 'messages'
8
+
9
+ attr_reader :status
10
+
11
+ def initialize(options = { })
12
+ @status = options[STATUS]
13
+ @message = options[MESSAGE]
14
+ @id = options[ID]
15
+ @messages = options[MESSAGES]
16
+ end
17
+
18
+ def self.new_instance(data)
19
+ if data.has_key? OK
20
+ OkResponse.new(data[OK])
21
+ else
22
+ ErrorResponse.new(data)
23
+ end
24
+ end
25
+
26
+ def self.mock_event_ok
27
+ OkResponse.new({ MESSAGE => 'event received', STATUS => 'unclassified', ID => 999 })
28
+ end
29
+
30
+ def self.mock_clear_ok
31
+ OkResponse.new({ MESSAGE => 'event cleared', STATUS => 'unclassified', ID => 999 })
32
+ end
33
+ end
34
+
35
+ class OkResponse < Response
36
+ attr_reader :message, :id
37
+
38
+ def to_s
39
+ "#{self.class.name} status: #@status, message: #@message, id: #@id"
40
+ end
41
+ end
42
+
43
+ class ErrorResponse < Response
44
+ attr_reader :messages
45
+ def to_s
46
+ "#{self.class.name} status: #@status, err_messages: #{@messages.join(', ')}"
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eep_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Lewis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A client library for sending and clearing Events via the Event Enrichment
28
+ Platform (EEP) REST API
29
+ email: mike@eventenrichment.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/eep_client.rb
35
+ - lib/eep_client/const.rb
36
+ - lib/eep_client/response.rb
37
+ homepage: http://rubygems.org/gems/eep_client
38
+ licenses:
39
+ - Apache License, Version 2.0
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.0.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Event Enrichment Platform (EEP) REST API client
61
+ test_files: []