Breinify 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/Breinify.rb +70 -36
- data/lib/Breinify/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c8821732d52cf7d678064177e737b4675fc4567
|
4
|
+
data.tar.gz: 76f2c0cdb6f247bcc4ecfde1984087e7ac4e44f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8499453201e5e40f0ff5409dbb6c380f2b8ae59e64204f17fd9c5b62b79bd4ca6f775d40a208893f22c7a0522e28f1bb081f54124eab0ecaaab1542bff64f83b
|
7
|
+
data.tar.gz: 4d0c707d1cedcdd2636af5a5706961c29f483a8882d03f5262747a5088cb13f2ba7bd3bd38ba29982f8cf91a7dfd4832d8562cd023450916f3256ab47052442f
|
data/lib/Breinify.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
# require "Breinify/version"
|
2
|
-
|
3
|
-
#require 'rubygems'
|
4
|
-
#require 'uri'
|
5
|
-
#require 'net/http'
|
6
|
-
|
7
1
|
require 'net/https'
|
8
2
|
require 'json'
|
9
3
|
require 'base64'
|
@@ -18,11 +12,6 @@ module Breinify
|
|
18
12
|
@@logger.sev_threshold = Logger::DEBUG
|
19
13
|
# @@logger = Logger.new('breinify.log', 'daily')
|
20
14
|
|
21
|
-
|
22
|
-
## question should I have a class ???
|
23
|
-
class BreinConfig
|
24
|
-
end
|
25
|
-
|
26
15
|
## default values
|
27
16
|
|
28
17
|
##
|
@@ -48,19 +37,19 @@ module Breinify
|
|
48
37
|
## module values
|
49
38
|
|
50
39
|
##
|
51
|
-
# url
|
40
|
+
# contains the url
|
52
41
|
@@url
|
53
42
|
|
54
43
|
##
|
55
|
-
# api key
|
44
|
+
# contains the api key
|
56
45
|
@@apiKey
|
57
46
|
|
58
47
|
##
|
59
|
-
# secret
|
48
|
+
# contains the secret
|
60
49
|
@@secret
|
61
50
|
|
62
51
|
##
|
63
|
-
# timeout (in ms)
|
52
|
+
# contains the timeout (in ms)
|
64
53
|
@@timeout
|
65
54
|
|
66
55
|
##
|
@@ -135,32 +124,12 @@ module Breinify
|
|
135
124
|
|
136
125
|
begin
|
137
126
|
|
138
|
-
# url to use with actvitiy endpoint
|
139
|
-
fullUrl = @@url + @@activityEndpoint
|
140
|
-
|
141
|
-
# retrieve all the options
|
142
|
-
uri = URI(fullUrl)
|
143
|
-
header = {'accept': 'application/json'}
|
144
|
-
|
145
127
|
# unix timestamp
|
146
128
|
unixTimestamp = Time.now.getutc.to_i
|
147
129
|
@@logger.debug 'Unix timestamp is: ' + unixTimestamp.to_s
|
148
130
|
|
149
|
-
signature = nil
|
150
|
-
if @@secret != nil
|
151
|
-
|
152
|
-
activityData = options.fetch('activity', nil)
|
153
|
-
activityType = activityData.fetch('type', nil)
|
154
|
-
message = activityType + unixTimestamp.to_s + '1'
|
155
|
-
hash = OpenSSL::HMAC.digest('sha256', @@secret, message)
|
156
|
-
signature = Base64.encode64(hash).strip
|
157
|
-
|
158
|
-
@@logger.debug 'Secret value is: ' + signature
|
159
|
-
end
|
160
|
-
|
161
131
|
@@logger.debug 'activity values are: ' + options.to_s
|
162
132
|
|
163
|
-
|
164
133
|
## the following fields have to be added
|
165
134
|
# apiKey
|
166
135
|
# unixTimestamp
|
@@ -170,14 +139,44 @@ module Breinify
|
|
170
139
|
data['apiKey'] = @@apiKey
|
171
140
|
data['unixTimestamp'] = unixTimestamp
|
172
141
|
|
142
|
+
signature = handleSignature(options, unixTimestamp)
|
173
143
|
if signature != nil
|
174
144
|
data['signature'] = signature
|
175
145
|
end
|
176
146
|
|
147
|
+
## add the userAgent
|
148
|
+
userAgent = retrieiveUserAgentInformation
|
149
|
+
|
150
|
+
# for test purposes
|
151
|
+
userAgent = 'BLABLA'
|
152
|
+
|
153
|
+
# fetch previous values - if they exists
|
154
|
+
begin
|
155
|
+
additionalValues = options.fetch('user', {}).fetch('additional', {})
|
156
|
+
if additionalValues.empty?
|
157
|
+
|
158
|
+
userAgentHash = Hash.new
|
159
|
+
userAgentHash['userAgent'] = userAgent
|
160
|
+
|
161
|
+
userData = options.fetch('user', {})
|
162
|
+
userData['additional'] = userAgentHash
|
163
|
+
else
|
164
|
+
additionalValues['userAgent'] = userAgent
|
165
|
+
end
|
166
|
+
rescue
|
167
|
+
@@logger.debug 'Could not handle userAgent information'
|
168
|
+
end
|
169
|
+
|
170
|
+
# url to use with actvitiy endpoint
|
171
|
+
fullUrl = @@url + @@activityEndpoint
|
172
|
+
|
173
|
+
# retrieve all the options
|
174
|
+
uri = URI(fullUrl)
|
175
|
+
|
177
176
|
# Create the HTTP objects
|
178
177
|
http = Net::HTTP.new(uri.host, uri.port)
|
179
178
|
http.use_ssl = true if uri.scheme == 'https'
|
180
|
-
request = Net::HTTP::Post.new(uri.request_uri,
|
179
|
+
request = Net::HTTP::Post.new(uri.request_uri, {'accept': 'application/json'})
|
181
180
|
request.body = data.to_json
|
182
181
|
@@logger.debug 'JSON data is: ' + data.to_json.to_s
|
183
182
|
|
@@ -193,5 +192,40 @@ module Breinify
|
|
193
192
|
|
194
193
|
end
|
195
194
|
|
195
|
+
##
|
196
|
+
# == Description
|
197
|
+
#
|
198
|
+
def self.retrieiveUserAgentInformation
|
199
|
+
begin
|
200
|
+
userAgent = request.env['HTTP_USER_AGENT']
|
201
|
+
@@logger.debug 'userAgent is: ' + userAgent
|
202
|
+
rescue
|
203
|
+
@@logger.debug 'Sorry, no userAgent can be detected'
|
204
|
+
userAgent = nil
|
205
|
+
end
|
206
|
+
userAgent
|
207
|
+
end
|
208
|
+
|
209
|
+
##
|
210
|
+
# == Description
|
211
|
+
#
|
212
|
+
# Handles the signature...
|
213
|
+
#
|
214
|
+
def self.handleSignature(options, unixTimestamp)
|
215
|
+
signature = nil
|
216
|
+
if @@secret != nil
|
217
|
+
|
218
|
+
activityData = options.fetch('activity', nil)
|
219
|
+
activityType = activityData.fetch('type', nil)
|
220
|
+
message = activityType + unixTimestamp.to_s + '1'
|
221
|
+
hash = OpenSSL::HMAC.digest('sha256', @@secret, message)
|
222
|
+
signature = Base64.encode64(hash).strip
|
223
|
+
|
224
|
+
@@logger.debug 'Secret value is: ' + signature
|
225
|
+
end
|
226
|
+
signature
|
227
|
+
end
|
228
|
+
|
229
|
+
|
196
230
|
end
|
197
231
|
|
data/lib/Breinify/version.rb
CHANGED