Riminder 0.0.1
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.
- checksums.yaml +7 -0
- data/lib/filter.rb +22 -0
- data/lib/profile.rb +198 -0
- data/lib/requtils.rb +46 -0
- data/lib/restClientW.rb +87 -0
- data/lib/riminder.rb +31 -0
- data/lib/riminderException.rb +41 -0
- data/lib/source.rb +21 -0
- data/lib/webhook.rb +131 -0
- metadata +115 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 55cba8505a6a13db413342faa7d420a68ddc960766c10a168a5f416abd8a2d90
|
|
4
|
+
data.tar.gz: b415846f1217e0e85faa5e1050984787e6a07c2d18e94d8a6edab4f5f83341d5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 332943114ba425aaa881c27dd3c22c301840a8fb3917d1ac969d688a0590429f0962b789df8b973606be8adc299d165c07d4bc14711d8d36de6030cbc6f3b467
|
|
7
|
+
data.tar.gz: befb6ed0bc6e1c6138d51b18eccb45ecb7c95b8473a50f74557c21b8027a3d242e84a2a0d7dab38fc8c58e40137d3d1f19dc9553aa7fab4705ad26b03e9c5bdd
|
data/lib/filter.rb
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require_relative 'requtils.rb'
|
|
2
|
+
|
|
3
|
+
class Filter
|
|
4
|
+
@clientw
|
|
5
|
+
|
|
6
|
+
def initialize(clientw)
|
|
7
|
+
@clientw = clientw
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def list()
|
|
11
|
+
resp = @clientw.get("filters")
|
|
12
|
+
return resp['data']
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def get(options)
|
|
16
|
+
query = {}
|
|
17
|
+
query = ReqUtils.add_if_not_blank(query, 'filter_id', options['filter_id'])
|
|
18
|
+
query = ReqUtils.add_if_not_blank(query, 'filter_reference', options['filter_reference'])
|
|
19
|
+
resp = @clientw.get("filter", query)
|
|
20
|
+
return resp['data']
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/profile.rb
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require_relative 'riminderException.rb'
|
|
3
|
+
|
|
4
|
+
class Profile
|
|
5
|
+
@clientw
|
|
6
|
+
attr_reader :document
|
|
7
|
+
attr_reader :parsing
|
|
8
|
+
attr_reader :scoring
|
|
9
|
+
attr_reader :json
|
|
10
|
+
attr_reader :stage
|
|
11
|
+
attr_reader :rating
|
|
12
|
+
DEFAULT_DATE_START = '1347209668'
|
|
13
|
+
|
|
14
|
+
def initialize(clientw)
|
|
15
|
+
@clientw = clientw
|
|
16
|
+
|
|
17
|
+
@document = Documents.new(@clientw)
|
|
18
|
+
@parsing = Parsing.new(@clientw)
|
|
19
|
+
@scoring = Scoring.new(@clientw)
|
|
20
|
+
@json = Json.new(@clientw)
|
|
21
|
+
@stage = Stage.new(@clientw)
|
|
22
|
+
@rating = Rating.new(@clientw)
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def add(options)
|
|
27
|
+
if (!options.key?('filepath'))
|
|
28
|
+
raise RiminderArgumentException.new('A filepath should be given')
|
|
29
|
+
end
|
|
30
|
+
file_path = options['filepath']
|
|
31
|
+
payload = {
|
|
32
|
+
'source_id' => options['source_id']
|
|
33
|
+
}
|
|
34
|
+
payload = ReqUtils.add_if_not_blank(payload, 'profile_reference', options['profile_reference'])
|
|
35
|
+
payload = ReqUtils.add_if_not_blank(payload, 'timestamp_reception', options['timestamp_reception'])
|
|
36
|
+
payload = ReqUtils.add_if_not_blank(payload, 'training_metadata', ReqUtils.validateTrainingMetadata(options['training_metadata']))
|
|
37
|
+
resp = @clientw.postfile('profile', file_path, payload)
|
|
38
|
+
return resp["data"]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def get(options)
|
|
43
|
+
query = {
|
|
44
|
+
"source_id" => options['source_id']
|
|
45
|
+
}
|
|
46
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_id', options['profile_id'])
|
|
47
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_reference', options['profile_reference'])
|
|
48
|
+
resp = @clientw.get("profile", query)
|
|
49
|
+
return resp["data"]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def list(options)
|
|
53
|
+
if (!options['source_ids'].nil? && !options['source_ids'].kind_of?(Array))
|
|
54
|
+
raise RiminderArgumentException.new('options should contains a the source_ids field a an Array')
|
|
55
|
+
end
|
|
56
|
+
query = {
|
|
57
|
+
'source_ids' => options['source_ids'].to_json,
|
|
58
|
+
'date_start' => DEFAULT_DATE_START,
|
|
59
|
+
'date_end' => Time.now.to_i,
|
|
60
|
+
'page' => 1,
|
|
61
|
+
'sort_by' => 'ranking'
|
|
62
|
+
}
|
|
63
|
+
query = ReqUtils.add_if_not_blank(query, 'seniority', options['seniority'])
|
|
64
|
+
query = ReqUtils.add_if_not_blank(query, 'filter_id', options['filter_id'])
|
|
65
|
+
query = ReqUtils.add_if_not_blank(query, 'filter_reference', options['filter_reference'])
|
|
66
|
+
query = ReqUtils.add_if_not_blank(query, 'stage', options['stage'])
|
|
67
|
+
query = ReqUtils.add_if_not_blank(query, 'rating', options['rating'])
|
|
68
|
+
query = ReqUtils.add_if_not_blank(query, 'date_start', options['date_start'])
|
|
69
|
+
query = ReqUtils.add_if_not_blank(query, 'date_end', options['date_end'])
|
|
70
|
+
query = ReqUtils.add_if_not_blank(query, 'page', options['page'])
|
|
71
|
+
query = ReqUtils.add_if_not_blank(query, 'limit', options['limit'])
|
|
72
|
+
query = ReqUtils.add_if_not_blank(query, 'sort_by', options['sort_by'])
|
|
73
|
+
query = ReqUtils.add_if_not_blank(query, 'order_by', options['order_by'])
|
|
74
|
+
|
|
75
|
+
resp = @clientw.get('profiles', query)
|
|
76
|
+
return resp["data"]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class Documents
|
|
80
|
+
@clientw
|
|
81
|
+
def initialize(clientw)
|
|
82
|
+
@clientw = clientw
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def list(options)
|
|
86
|
+
query = {
|
|
87
|
+
"source_id" => options['source_id']
|
|
88
|
+
}
|
|
89
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_id', options['profile_id'])
|
|
90
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_reference', options['profile_reference'])
|
|
91
|
+
resp = @clientw.get("profile/documents", query)
|
|
92
|
+
return resp["data"]
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
class Parsing
|
|
97
|
+
@clientw
|
|
98
|
+
def initialize(clientw)
|
|
99
|
+
@clientw = clientw
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def get(options)
|
|
103
|
+
query = {
|
|
104
|
+
"source_id" => options['source_id']
|
|
105
|
+
}
|
|
106
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_id', options['profile_id'])
|
|
107
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_reference', options['profile_reference'])
|
|
108
|
+
resp = @clientw.get("profile/parsing", query)
|
|
109
|
+
return resp["data"]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
class Scoring
|
|
114
|
+
@clientw
|
|
115
|
+
def initialize(clientw)
|
|
116
|
+
@clientw = clientw
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def list(options)
|
|
120
|
+
query = {
|
|
121
|
+
"source_id" => options['source_id']
|
|
122
|
+
}
|
|
123
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_id', options['profile_id'])
|
|
124
|
+
query = ReqUtils.add_if_not_blank(query, 'profile_reference', options['profile_reference'])
|
|
125
|
+
resp = @clientw.get("profile/scoring", query)
|
|
126
|
+
return resp['data']
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
class Json
|
|
131
|
+
@clientw
|
|
132
|
+
def initialize(clientw)
|
|
133
|
+
@clientw = clientw
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def check(options)
|
|
137
|
+
payload = {
|
|
138
|
+
"profile_json" => options['profile_json']
|
|
139
|
+
}
|
|
140
|
+
payload = ReqUtils.add_if_not_blank(payload, 'training_metadata', ReqUtils.validateTrainingMetadata(options['training_metadata']))
|
|
141
|
+
resp = @clientw.post('profile/json/check', payload)
|
|
142
|
+
return resp['data']
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def add(options)
|
|
146
|
+
payload = {
|
|
147
|
+
"source_id" => options['source_id'],
|
|
148
|
+
"profile_json" => options['profile_json']
|
|
149
|
+
}
|
|
150
|
+
payload = ReqUtils.add_if_not_blank(payload, 'profile_reference', options['profile_reference'])
|
|
151
|
+
payload = ReqUtils.add_if_not_blank(payload, 'timestamp_reception', options['timestamp_reception'])
|
|
152
|
+
payload = ReqUtils.add_if_not_blank(payload, 'training_metadata', ReqUtils.validateTrainingMetadata(options['training_metadata']))
|
|
153
|
+
resp = @clientw.post('profile/json', payload)
|
|
154
|
+
return resp['data']
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
class Stage
|
|
159
|
+
@clientw
|
|
160
|
+
def initialize(clientw)
|
|
161
|
+
@clientw = clientw
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def set(options)
|
|
165
|
+
payload = {
|
|
166
|
+
"source_id" => options['source_id'],
|
|
167
|
+
"stage" => options["stage"]
|
|
168
|
+
}
|
|
169
|
+
payload = ReqUtils.add_if_not_blank(payload, 'profile_id', options['profile_id'])
|
|
170
|
+
payload = ReqUtils.add_if_not_blank(payload, 'profile_reference', options['profile_reference'])
|
|
171
|
+
payload = ReqUtils.add_if_not_blank(payload, 'filter_id', options['filter_id'])
|
|
172
|
+
payload = ReqUtils.add_if_not_blank(payload, 'filter_reference', options['filter_reference'])
|
|
173
|
+
resp = @clientw.patch("profile/stage", payload)
|
|
174
|
+
return resp['data']
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
class Rating
|
|
179
|
+
@clientw
|
|
180
|
+
def initialize(clientw)
|
|
181
|
+
@clientw = clientw
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def set(options)
|
|
185
|
+
payload = {
|
|
186
|
+
"source_id" => options['source_id'],
|
|
187
|
+
"rating" => options["rating"]
|
|
188
|
+
}
|
|
189
|
+
payload = ReqUtils.add_if_not_blank(payload, 'profile_id', options['profile_id'])
|
|
190
|
+
payload = ReqUtils.add_if_not_blank(payload, 'profile_reference', options['profile_reference'])
|
|
191
|
+
payload = ReqUtils.add_if_not_blank(payload, 'filter_id', options['filter_id'])
|
|
192
|
+
payload = ReqUtils.add_if_not_blank(payload, 'filter_reference', options['filter_reference'])
|
|
193
|
+
resp = @clientw.patch("profile/rating", payload)
|
|
194
|
+
return resp['data']
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
end
|
data/lib/requtils.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
require 'active_support/core_ext'
|
|
3
|
+
|
|
4
|
+
require_relative 'riminderException.rb'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ReqUtils
|
|
9
|
+
def self.add_if_not_blank(to_fill, key, elem)
|
|
10
|
+
if (!elem.nil? && !elem.blank?)
|
|
11
|
+
to_fill[key] = elem
|
|
12
|
+
end
|
|
13
|
+
return to_fill
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.assertIDRef(prefix, options)
|
|
17
|
+
ref = prefix + '_id'
|
|
18
|
+
id = prefix + '_reference'
|
|
19
|
+
if (!options.key?(ref) && !options.key?(id))
|
|
20
|
+
raise RiminderArgumentException("'%s' or '%s' should exist in options"[ref, id])
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.validateTrainingMetadata(to_validate)
|
|
25
|
+
if (to_validate.kind_of?(NilClass) || to_validate.nil?)
|
|
26
|
+
return to_validate
|
|
27
|
+
end
|
|
28
|
+
# True if have to be filled els otherwise
|
|
29
|
+
mandatory_keys = {'filter_reference' => true, 'stage' => false, 'stage_timestamp' => false, 'rating' => false, 'rating_timestamp' => false}
|
|
30
|
+
if (!to_validate.kind_of?(Array))
|
|
31
|
+
raise RiminderArgumentException.new('Training metadatas should be an array.')
|
|
32
|
+
end
|
|
33
|
+
mandatory_keys.each_pair{|key, shouldbefilled|
|
|
34
|
+
to_validate.each {|to_validate_elem|
|
|
35
|
+
if (!to_validate_elem.key?(key))
|
|
36
|
+
raise RiminderArgumentException.new('All training metadata should contain %s' [key])
|
|
37
|
+
end
|
|
38
|
+
if (shouldbefilled && to_validate_elem[key].blank?)
|
|
39
|
+
raise RiminderArgumentException.new('All training metadata should contain %s not nil or empty' [key])
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return to_validate
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/restClientW.rb
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require 'rest-client'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
require_relative 'riminderException.rb'
|
|
5
|
+
|
|
6
|
+
class RestClientW
|
|
7
|
+
|
|
8
|
+
@base_url
|
|
9
|
+
@base_headers
|
|
10
|
+
def initialize(base_url, headers)
|
|
11
|
+
@base_headers = headers
|
|
12
|
+
@base_url = base_url
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def fill_header_params_for_get(params)
|
|
16
|
+
headrs = {}
|
|
17
|
+
headrs["params"] = params
|
|
18
|
+
@base_headers.each_pair {|key, value| headrs[key] = value}
|
|
19
|
+
return headrs
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def get(path, params={})
|
|
23
|
+
url = @base_url + path
|
|
24
|
+
headrs = fill_header_params_for_get(params)
|
|
25
|
+
|
|
26
|
+
resp = nil
|
|
27
|
+
begin
|
|
28
|
+
resp = RestClient::Request.execute(:method => :get, :url => url, :headers => headrs)
|
|
29
|
+
rescue RestClient::ExceptionWithResponse => exp
|
|
30
|
+
raise RiminderResponseException.new("Invalid Response: ", exp)
|
|
31
|
+
rescue RestClient::Exception => exp
|
|
32
|
+
raise RiminderTransfertException.new("Error during transfert: ", exp)
|
|
33
|
+
end
|
|
34
|
+
return JSON.parse(resp)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def post(path, payload={})
|
|
38
|
+
url = @base_url + path
|
|
39
|
+
|
|
40
|
+
json_payload = payload.to_json
|
|
41
|
+
begin
|
|
42
|
+
resp = RestClient.post url, json_payload, @base_headers
|
|
43
|
+
rescue RestClient::ExceptionWithResponse => exp
|
|
44
|
+
raise RiminderResponseException.new("Invalid Response: ", exp)
|
|
45
|
+
rescue RestClient::Exception => exp
|
|
46
|
+
raise RiminderTransfertException.new("Error during transfert: ", exp)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
return JSON.parse(resp)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def fill_postfile_multipart(multipart, payload)
|
|
53
|
+
payload.each_pair {|key, value| multipart[key] = value }
|
|
54
|
+
return multipart
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def postfile(path, file_path, payload={})
|
|
58
|
+
url = @base_url + path
|
|
59
|
+
|
|
60
|
+
multipart = {:file => File.new(file_path, 'rb'), :multipart => true}
|
|
61
|
+
multipart = fill_postfile_multipart(multipart, payload)
|
|
62
|
+
|
|
63
|
+
begin
|
|
64
|
+
resp = RestClient.post url, multipart, @base_headers
|
|
65
|
+
rescue RestClient::ExceptionWithResponse => exp
|
|
66
|
+
raise RiminderResponseException.new("Invalid Response: ", exp)
|
|
67
|
+
rescue RestClient::Exception => exp
|
|
68
|
+
raise RiminderTransfertException.new("Error during transfert: ", exp)
|
|
69
|
+
end
|
|
70
|
+
return JSON.parse(resp)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def patch(path, payload={})
|
|
74
|
+
url = @base_url + path
|
|
75
|
+
|
|
76
|
+
json_payload = payload.to_json
|
|
77
|
+
begin
|
|
78
|
+
resp = RestClient.patch url, json_payload, @base_headers
|
|
79
|
+
rescue RestClient::ExceptionWithResponse => exp
|
|
80
|
+
raise RiminderResponseException.new("Invalid Response: ", exp)
|
|
81
|
+
rescue RestClient::Exception => exp
|
|
82
|
+
raise RiminderTransfertException.new("Error during transfert: ", exp)
|
|
83
|
+
end
|
|
84
|
+
return JSON.parse(resp)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
end
|
data/lib/riminder.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require_relative 'restClientW.rb'
|
|
2
|
+
require_relative 'source.rb'
|
|
3
|
+
require_relative 'filter.rb'
|
|
4
|
+
require_relative 'profile.rb'
|
|
5
|
+
require_relative 'webhook.rb'
|
|
6
|
+
|
|
7
|
+
class Riminder
|
|
8
|
+
@api_key
|
|
9
|
+
@webhook_key
|
|
10
|
+
@base_url
|
|
11
|
+
@clientw
|
|
12
|
+
attr_reader :source
|
|
13
|
+
attr_reader :filter
|
|
14
|
+
attr_reader :profile
|
|
15
|
+
attr_reader :webhooks
|
|
16
|
+
|
|
17
|
+
def initialize(api_key, webhook_key=nil, base_url='https://www.riminder.net/sf/public/api/v1.0/')
|
|
18
|
+
@api_key = api_key
|
|
19
|
+
@webhook_key = webhook_key
|
|
20
|
+
@base_url = base_url
|
|
21
|
+
headrs = {'X-API-KEY' => api_key}
|
|
22
|
+
@clientw = RestClientW.new(base_url, headrs)
|
|
23
|
+
|
|
24
|
+
@source = Source.new(@clientw)
|
|
25
|
+
@filter = Filter.new(@clientw)
|
|
26
|
+
@profile = Profile.new(@clientw)
|
|
27
|
+
@webhooks = Webhooks.new(@clientw, webhook_key)
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class RiminderException < StandardError
|
|
2
|
+
def initialize(msg)
|
|
3
|
+
super(msg)
|
|
4
|
+
end
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class RiminderArgumentException < RiminderException
|
|
8
|
+
def initialize(msg)
|
|
9
|
+
super(msg)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class RiminderWebhookException < RiminderException
|
|
14
|
+
def initialize(msg)
|
|
15
|
+
super(msg)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class RiminderTransfertException < RiminderException
|
|
20
|
+
def initialize(msg, exp)
|
|
21
|
+
msg = msg + exp.to_s + ' => ' + exp.backtrace.to_s
|
|
22
|
+
super(msg)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class RiminderResponseException < RiminderException
|
|
27
|
+
attr_reader :response_body
|
|
28
|
+
attr_reader :status_code
|
|
29
|
+
|
|
30
|
+
def initialize(msg, exp)
|
|
31
|
+
response_body = exp.http_body
|
|
32
|
+
if (response_body.length >= 201)
|
|
33
|
+
response_body = response_body.str[0, 200] + '...'
|
|
34
|
+
end
|
|
35
|
+
msg = msg + exp.to_s + " => " + response_body
|
|
36
|
+
|
|
37
|
+
@response_body = exp.http_body
|
|
38
|
+
@status_code = exp.http_code
|
|
39
|
+
super(msg)
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/source.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
class Source
|
|
3
|
+
@clientw
|
|
4
|
+
|
|
5
|
+
def initialize(clientw)
|
|
6
|
+
@clientw = clientw
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def list()
|
|
10
|
+
resp = @clientw.get("sources")
|
|
11
|
+
return resp['data']
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def get(source_id)
|
|
15
|
+
query = {
|
|
16
|
+
"source_id" => source_id
|
|
17
|
+
}
|
|
18
|
+
resp = @clientw.get("source", query)
|
|
19
|
+
return resp['data']
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/webhook.rb
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
require_relative 'riminderException.rb'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Webhooks
|
|
5
|
+
|
|
6
|
+
class EventNames
|
|
7
|
+
EVENT_PROFILE_PARSE_SUCCESS = 'profile.parse.success'
|
|
8
|
+
EVENT_PROFILE_PARSE_ERROR = 'profile.parse.error'
|
|
9
|
+
EVENT_PROFILE_SCORE_SUCCESS = 'profile.score.success'
|
|
10
|
+
EVENT_PROFILE_SCORE_ERROR = 'profile.score.error'
|
|
11
|
+
EVENT_FILTER_TRAIN_SUCCESS = 'filter.train.success'
|
|
12
|
+
EVENT_FILTER_TRAIN_ERROR = 'filter.train.error'
|
|
13
|
+
EVENT_FILTER_TRAIN_START = 'filter.train.start'
|
|
14
|
+
EVENT_FILTER_SCORE_SUCCESS = 'filter.score.success'
|
|
15
|
+
EVENT_FILTER_SCORE_ERROR = 'filter.score.error'
|
|
16
|
+
EVENT_FILTER_SCORE_START = 'filter.score.start'
|
|
17
|
+
ACTION_STAGE_SUCCESS = 'action.stage.success'
|
|
18
|
+
ACTION_STAGE_ERROR = 'action.stage.error'
|
|
19
|
+
ACTION_RATING_SUCCESS = 'action.rating.success'
|
|
20
|
+
ACTION_RATING_ERROR = 'action.rating.error'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
SIGNATURE_HEADER = 'HTTP-RIMINDER-SIGNATURE'
|
|
24
|
+
|
|
25
|
+
@clientw
|
|
26
|
+
@webhook_key
|
|
27
|
+
@handlers
|
|
28
|
+
def initialize(clientw, webhook_key)
|
|
29
|
+
@clientw = clientw
|
|
30
|
+
@webhook_key = webhook_key
|
|
31
|
+
@handlers = {}
|
|
32
|
+
|
|
33
|
+
@handlers[EventNames::EVENT_PROFILE_PARSE_SUCCESS] = nil
|
|
34
|
+
@handlers[EventNames::EVENT_PROFILE_PARSE_ERROR] = nil
|
|
35
|
+
@handlers[EventNames::EVENT_PROFILE_SCORE_SUCCESS] = nil
|
|
36
|
+
@handlers[EventNames::EVENT_PROFILE_SCORE_ERROR] = nil
|
|
37
|
+
@handlers[EventNames::EVENT_FILTER_TRAIN_SUCCESS] = nil
|
|
38
|
+
@handlers[EventNames::EVENT_FILTER_TRAIN_ERROR] = nil
|
|
39
|
+
@handlers[EventNames::EVENT_FILTER_TRAIN_START] = nil
|
|
40
|
+
@handlers[EventNames::EVENT_FILTER_SCORE_SUCCESS] = nil
|
|
41
|
+
@handlers[EventNames::EVENT_FILTER_SCORE_ERROR] = nil
|
|
42
|
+
@handlers[EventNames::EVENT_FILTER_SCORE_START] = nil
|
|
43
|
+
@handlers[EventNames::ACTION_STAGE_SUCCESS] = nil
|
|
44
|
+
@handlers[EventNames::ACTION_STAGE_ERROR] = nil
|
|
45
|
+
@handlers[EventNames::ACTION_RATING_SUCCESS] = nil
|
|
46
|
+
@handlers[EventNames::ACTION_RATING_ERROR] = nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def check
|
|
50
|
+
resp = @clientw.post('webhook/check')
|
|
51
|
+
return resp["data"]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def setHandler(eventName, callback)
|
|
55
|
+
if (!@handlers.key?(eventName))
|
|
56
|
+
raise RiminderArgumentException("%s is not a valid event." [eventName])
|
|
57
|
+
end
|
|
58
|
+
@handlers[eventName] = callback
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def removeHandler(eventName)
|
|
62
|
+
if (!@handlers.key?(eventName))
|
|
63
|
+
raise RiminderArgumentException("%s is not a valid event." [eventName])
|
|
64
|
+
end
|
|
65
|
+
@handlers[eventName] = nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def isHandlerPresent(eventName)
|
|
69
|
+
if (!@handlers.key?(eventName))
|
|
70
|
+
raise RiminderArgumentException("%s is not a valid event." [eventName])
|
|
71
|
+
end
|
|
72
|
+
return @handlers[eventName].nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def getEncodedHeader(receivedMessage)
|
|
76
|
+
if (receivedMessage.is_a?(Hash))
|
|
77
|
+
if (receivedMessage.key?(SIGNATURE_HEADER))
|
|
78
|
+
return receivedMessage[SIGNATURE_HEADER]
|
|
79
|
+
end
|
|
80
|
+
raise RiminderArgumentException.new("Webhhook request header should contain %s key." [SIGNATURE_HEADER])
|
|
81
|
+
end
|
|
82
|
+
return receivedMessage
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def customstrstr(inp, to_replace, by)
|
|
86
|
+
res = ""
|
|
87
|
+
inp.each_char {|c|
|
|
88
|
+
tmpc = c
|
|
89
|
+
tr_idx = to_replace.index(tmpc)
|
|
90
|
+
if (!tr_idx.nil? && by.length < tr_idx)
|
|
91
|
+
tmpc = by[tr_idx]
|
|
92
|
+
end
|
|
93
|
+
res = res + tmpc
|
|
94
|
+
}
|
|
95
|
+
return res
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def base64urlDecode(inp)
|
|
99
|
+
return Base64.decode64(customstrstr(inp, "-_", "+/"))
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def isSignatureValid(payload, sign)
|
|
103
|
+
expectedsign = OpenSSL::HMAC.digest("SHA256", @webhook_key, payload)
|
|
104
|
+
return expectedsign == sign
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def handle(receivedMessage)
|
|
108
|
+
if (@webhook_key.blank?)
|
|
109
|
+
raise RiminderWebhookException.new("No webhook secret key provided")
|
|
110
|
+
end
|
|
111
|
+
encodedMessage = getEncodedHeader(receivedMessage)
|
|
112
|
+
tmp = encodedMessage.split('.', 2)
|
|
113
|
+
sign = base64urlDecode(tmp[0])
|
|
114
|
+
json_payload = base64urlDecode(tmp[1])
|
|
115
|
+
|
|
116
|
+
if (!isSignatureValid(json_payload, sign))
|
|
117
|
+
raise RiminderWebhookException.new("Invalid signature")
|
|
118
|
+
end
|
|
119
|
+
payload = JSON.parse(json_payload)
|
|
120
|
+
|
|
121
|
+
payloadtype = payload['type']
|
|
122
|
+
if (payloadtype.nil? || !@handlers.key?(payloadtype))
|
|
123
|
+
raise RiminderWebhookException.new("Null or absent webhook type.")
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
callback = @handlers[payloadtype]
|
|
127
|
+
if (!callback.nil?)
|
|
128
|
+
callback.call(payloadtype, payload)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: Riminder
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gotte Alexandre
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-09-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rest-client
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 2.0.2
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - "~>"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '2.0'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 2.0.2
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: activesupport
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '5.0'
|
|
40
|
+
type: :runtime
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '5.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: rubocop
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 0.58.2
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 0.58.2
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: rspec
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3.5'
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '3.5'
|
|
75
|
+
description: A api client for riminder, permit to access and manipulate some Riminder's
|
|
76
|
+
services without the web interface.
|
|
77
|
+
email: contact@riminder.net
|
|
78
|
+
executables: []
|
|
79
|
+
extensions: []
|
|
80
|
+
extra_rdoc_files: []
|
|
81
|
+
files:
|
|
82
|
+
- lib/filter.rb
|
|
83
|
+
- lib/profile.rb
|
|
84
|
+
- lib/requtils.rb
|
|
85
|
+
- lib/restClientW.rb
|
|
86
|
+
- lib/riminder.rb
|
|
87
|
+
- lib/riminderException.rb
|
|
88
|
+
- lib/source.rb
|
|
89
|
+
- lib/webhook.rb
|
|
90
|
+
homepage: https://riminder.net
|
|
91
|
+
licenses:
|
|
92
|
+
- MIT
|
|
93
|
+
metadata:
|
|
94
|
+
source_code_uri: https://github.com/Riminder/ruby-riminder-api
|
|
95
|
+
post_install_message:
|
|
96
|
+
rdoc_options: []
|
|
97
|
+
require_paths:
|
|
98
|
+
- lib
|
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '0'
|
|
109
|
+
requirements: []
|
|
110
|
+
rubyforge_project:
|
|
111
|
+
rubygems_version: 2.7.6
|
|
112
|
+
signing_key:
|
|
113
|
+
specification_version: 4
|
|
114
|
+
summary: A ruby api client for Riminder api.
|
|
115
|
+
test_files: []
|