Module: Breinify

Defined in:
lib/Breinify.rb,
lib/Breinify/version.rb

Constant Summary collapse

VERSION =
"0.1.2"
@@logger =

logger

Logger.new(STDOUT)
@@defaultActivityEndpoint =

default endpoint of activity

'/activity'
@@defaultLookupEndpoint =

default endpoint of lookup

'/lookup'
@@defaultUrl =

default breinify url

'https://api.breinify.com'
@@defaultSecret =

default secret value

nil
@@defaultTimeout =

default timeout

6000

Class Method Summary collapse

Class Method Details

.activity(options = {}) ⇒ Object

Description

Sends an activity to the engine utilizing the API. The call is done asynchronously as a POST request. It is important that a valid API-key is configured prior to using this function.

Possible parameters are:

Example:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/Breinify.rb', line 118

def self.activity(options = {})

  if options == nil
    @@logger.debug 'Breinify activity: values are nil'
    return
  end

  begin

    # unix timestamp
    unixTimestamp = Time.now.getutc.to_i
    @@logger.debug 'Unix timestamp is: ' + unixTimestamp.to_s

    @@logger.debug 'activity values are: ' + options.to_s

    ## the following fields have to be added
    # apiKey
    # unixTimestamp
    # secret (if set)

    data = options
    data['apiKey'] = @@apiKey
    data['unixTimestamp'] = unixTimestamp

    signature = handleSignature(options, unixTimestamp)
    if signature != nil
      data['signature'] = signature
    end

    ## add the userAgent
    userAgent = retrieiveUserAgentInformation

    # for test purposes
    userAgent = 'BLABLA'

    # fetch previous values - if they exists
    begin
      additionalValues = options.fetch('user', {}).fetch('additional', {})
      if additionalValues.empty?

        userAgentHash = Hash.new
        userAgentHash['userAgent'] = userAgent

        userData = options.fetch('user', {})
        userData['additional'] = userAgentHash
      else
        additionalValues['userAgent'] = userAgent
      end
    rescue
      @@logger.debug 'Could not handle userAgent information'
    end

    # url to use with actvitiy endpoint
    fullUrl = @@url + @@activityEndpoint

    # retrieve all the options
    uri = URI(fullUrl)

    # Create the HTTP objects
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if uri.scheme == 'https'
    request = Net::HTTP::Post.new(uri.request_uri, {'accept': 'application/json'})
    request.body = data.to_json
    @@logger.debug 'JSON data is: ' + data.to_json.to_s

    # Send the request
    response = http.request(request)
    @@logger.debug 'response from call is: ' + response.to_s

  rescue Exception => e
    @@logger.debug 'Exception caught: ' + e.message
    @@logger.debug '  Backtrace is: ' + e.backtrace.inspect
    return
  end

end

.handleSignature(options, unixTimestamp) ⇒ Object

Description

Handles the signature…



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/Breinify.rb', line 214

def self.handleSignature(options, unixTimestamp)
  signature = nil
  if @@secret != nil

    activityData = options.fetch('activity', nil)
    activityType = activityData.fetch('type', nil)
    message = activityType + unixTimestamp.to_s + '1'
    hash = OpenSSL::HMAC.digest('sha256', @@secret, message)
    signature = Base64.encode64(hash).strip

    @@logger.debug 'Secret value is: ' + signature
  end
  signature
end

.retrieiveUserAgentInformationObject

Description



198
199
200
201
202
203
204
205
206
207
# File 'lib/Breinify.rb', line 198

def self.retrieiveUserAgentInformation
  begin
    userAgent = request.env['HTTP_USER_AGENT']
    @@logger.debug 'userAgent is: ' + userAgent
  rescue
    @@logger.debug 'Sorry, no userAgent can be detected'
    userAgent = nil
  end
  userAgent
end

.setConfig(options = {}) ⇒ Object

Description

sets the Breinify Configuration of the library for the properties supplied.

  • possible parameters are:

    apiKey: The API-key to be used (mandatory).
    url: The url of the API
    activityEndpoint: The end-point of the API to send activities.
    lookupEndpoint: The end-point of the API to retrieve lookup results.
    secret: The secret attached to the API-key
    timeout: The maximum amount of time in milliseconds an API-call should take.
             If the API does not response after this amount of time, the call is cancelled.
    

If no parameters are set the default parameters will be used.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/Breinify.rb', line 71

def self.setConfig(options = {})

  if options == nil
    @@logger.debug 'BreinifyConfig: values are nil'
    return
  end

  begin
    @@apiKey = options.fetch(:apiKey, '')
    @@logger.debug ('apiKey: ' + @@apiKey)

    @@url = options.fetch(:url, @@defaultUrl)
    @@logger.debug ('url: ' + @@url)

    @@activityEndpoint = options.fetch(:activityEndpoint, @@defaultActivityEndpoint)
    @@logger.debug ('ActivityEndpoint: ' + @@activityEndpoint)

    @@lookupEndpoint = options.fetch(:lookupEndpoint, @@defaultLookupEndpoint)
    @@logger.debug ('LookupEndpoint: ' + @@lookupEndpoint)

    @@secret = options.fetch(:secret, @@defaultSecret)
    @@logger.debug ('Secret: ' + @@secret)

    @@timeout = options.fetch(:timeout, @@defaultTimeout)
    @@logger.debug ('Timeout: ' + @@timeout.to_s)

  rescue Exception => e
    @@logger.debug 'Exception caught: ' + e.message
    @@logger.debug '  Backtrace is: ' + e.backtrace.inspect
    return
  end

end