lib/Breinify.rb in Breinify-0.1.1 vs lib/Breinify.rb in Breinify-0.1.2

- old
+ new

@@ -1,11 +1,5 @@ -# require "Breinify/version" - -#require 'rubygems' -#require 'uri' -#require 'net/http' - require 'net/https' require 'json' require 'base64' require 'logger' @@ -16,15 +10,10 @@ # logger @@logger = Logger.new(STDOUT) @@logger.sev_threshold = Logger::DEBUG # @@logger = Logger.new('breinify.log', 'daily') - - ## question should I have a class ??? - class BreinConfig - end - ## default values ## # default endpoint of activity @@defaultActivityEndpoint = '/activity' @@ -46,23 +35,23 @@ @@defaultTimeout = 6000 ## module values ## - # url + # contains the url @@url ## - # api key + # contains the api key @@apiKey ## - # secret + # contains the secret @@secret ## - # timeout (in ms) + # contains the timeout (in ms) @@timeout ## # == Description # @@ -133,53 +122,63 @@ return end begin - # url to use with actvitiy endpoint - fullUrl = @@url + @@activityEndpoint - - # retrieve all the options - uri = URI(fullUrl) - header = {'accept': 'application/json'} - # unix timestamp unixTimestamp = Time.now.getutc.to_i @@logger.debug 'Unix timestamp is: ' + unixTimestamp.to_s - 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 - @@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, header) + 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) @@ -190,8 +189,43 @@ @@logger.debug ' Backtrace is: ' + e.backtrace.inspect return end end + + ## + # == Description + # + 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 + + ## + # == Description + # + # Handles the signature... + # + 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 + end