class Breinify::BreinActivity
Description¶ ↑
Sends an activity to the engine utilizing the API. The call is done as a POST request. It is important that a valid API-key is configured prior to using this function.
Attributes
http[RW]
init_done[RW]
request[RW]
Public Class Methods
instance()
click to toggle source
returns the BreinActivity
instance
# File lib/Breinify.rb, line 183 def self.instance return @@instance end
Private Class Methods
new()
click to toggle source
Create an instance of BreinConfig
# File lib/Breinify.rb, line 140 def initialize @brein_config = BreinConfig.instance @init_done = false end
Public Instance Methods
get_user_agent()
click to toggle source
handle_signature(options, unix_timestamp)
click to toggle source
Description¶ ↑
This method will crypt the signature.
# File lib/Breinify.rb, line 280 def handle_signature(options, unix_timestamp) signature = nil if @brein_config.secret != nil activity_data = options.fetch('activity', nil) activity_type = activity_data.fetch('type', nil) message = activity_type + unix_timestamp.to_s + '1' hash = OpenSSL::HMAC.digest('sha256', @brein_config.secret, message) signature = Base64.encode64(hash).strip end signature end
init_rest()
click to toggle source
Initializes the HTTP context
# File lib/Breinify.rb, line 147 def init_rest # if the initialization has already been done then go back if @init_done return end # url to use with activity endpoint full_url = @brein_config.url + @brein_config.activity_endpoint # retrieve all the options uri = URI(full_url) # Create the HTTP objects @http = Net::HTTP.new(uri.host, uri.port) @http.open_timeout = @brein_config.timeout @http.use_ssl = true if uri.scheme == 'https' # request itself @request = Net::HTTP::Post.new(uri.request_uri, {'accept': 'application/json'}) # indicates that the initializing for HTTP instance variables has been done @init_done = true end
send_activity(options = {})
click to toggle source
Sends an activity to the engine.
# File lib/Breinify.rb, line 190 def send_activity(options = {}) if options == nil $log.debug 'Breinify activity: values are nil' return end begin # unix timestamp unix_timestamp = Time.now.getutc.to_i $log.debug 'Unix timestamp is: ' + unix_timestamp.to_s $log.debug 'activity values are: ' + options.to_s ## the following fields will be added (apiKey, unixTimestamp, secret [if set]) data = options data['apiKey'] = @brein_config.api_key data['unixTimestamp'] = unix_timestamp # handles the secret / signature signature = handle_signature(options, unix_timestamp) if signature != nil data['signature'] = signature end ## retrieve the userAgent and set it if available user_agent = get_user_agent # fetch previous values - if they exists begin additional_values = options.fetch('user', {}).fetch('additional', {}) if additional_values.empty? user_agent_hash = Hash.new user_agent_hash['userAgent'] = user_agent user_data = options.fetch('user', {}) user_data['additional'] = user_agent_hash else additional_values['userAgent'] = user_agent end rescue $log.debug 'Could not handle userAgent information' end ## check if category has been set, otherwise add default from BreinConfig category_value = options.fetch('activity', {}).fetch('category', {}) if category_value.empty? default_category = @brein_config.category category_data = options.fetch('activity', {}) category_data['category'] = default_category end # prepare the body and send the request init_rest @request.body = data.to_json $log.debug 'JSON data request is: ' + @request.body.to_json.to_s # Send the request response = http.request(@request) $log.debug 'response from call is: ' + response.to_s rescue Exception => e $log.debug 'Exception caught: ' + e.message $log.debug ' Backtrace is: ' + e.backtrace.inspect return end end