class DopamineKit
The main driver for the DopamineAPI
Public Class Methods
Creates a DopamineKit
object to communicate with the DopamineAPI Get your credentials from dashboard.usedopamine.com and enter them during initilization
Params:
appID
-
String - Uniquely identifies your app, get this from your [developer dashboard](dev.usedopamine.com).
developmentSecret
-
String - Secret key for development.
productionSecret
-
String - Secret key for production.
versionID
-
String - This is a unique identifier that you choose that marks this implementation as unique in our system. This could be something like 'summer2015Implementation' or 'ClinicalTrial4'. Your `versionID` is what we use to keep track of what users are exposed to what reinforcement and how to best optimize that.
inProduction
-
boolean - Indicates whether app is in production or development mode, when you're happy with how you're integrating Dopamine and ready to launch set this argument to `true`. This will activate optimized reinforcement and start your billing cycle. While set to `false` your app will receive dummy reinforcement, new users will not be registered with our system, and no billing occurs.
debugMode
-
boolean=false - Enables debug mode, where the sent and received data are printed. Can be updated with `enableDebugMode(bool)`.
# File lib/dopaminekit.rb, line 20 def initialize(appID, developmentSecret, productionSecret, versionID, inProduction, debugMode=false) @clientSDKVersion = '4.0.0' @persistentMetaData = Hash.new @debugMode = debugMode @credentials = { :appID => appID, :secret => inProduction ? productionSecret : developmentSecret, :versionID => versionID } end
Public Instance Methods
When debugMode is set to true, the raw sent and received data are printed. It is by default set to false.
Params:
enabled
-
boolean - true to enable, false to disable
# File lib/dopaminekit.rb, line 39 def enableDebugMode(enabled) @debugMode = enabled end
Used to send refresh calls to the DopamineAPI.
Params:
payload
-
Hash - Contains the keys :utc, :timezoneOffset, :primaryIdentity, and :actionID
Return:
response
-
String - a JSON string containing the key 'status', and possibly 'reinforcementCartridge' and 'expiresIn'
# File lib/dopaminekit.rb, line 78 def refresh(payload) return send(:refresh, payload) end
Used to send report calls to the DopamineAPI.
Params:
payload
-
Hash - Contains the keys :utc, :timezoneOffset, :primaryIdentity, and :actions
Return:
response
-
String - a JSON string containing the key 'status', and if 'status' != 200 also another key 'errors'
# File lib/dopaminekit.rb, line 65 def report(payload) return send(:report, payload) end
Used to send track calls to the DopamineAPI.
Params:
payload
-
Hash - Contains the keys :utc, :timezoneOffset, :primaryIdentity, and :actions
Return:
response
-
String - a JSON string containing the key 'status', and if 'status' != 200 also another key 'errors'
# File lib/dopaminekit.rb, line 52 def track(payload) return send(:track, payload) end
Private Instance Methods
Privately used function to get POST data with basic information and time
# File lib/dopaminekit.rb, line 93 def getBasePostData() postData = { :clientOS => "Ruby", :clientOSVersion => "#{RUBY_VERSION}", :clientSDKVersion => @clientSDKVersion, :UTC => Time.now.utc.to_i, :localTime => Time.now.utc.to_i, } return postData.merge(@credentials) end
Privately used function to send an HTTP POST
# File lib/dopaminekit.rb, line 107 def send(type, data) if !data.is_a? Hash puts "[DopamineKit] - Invalid data for api call:#{data} -- needs to be a Hash" return end postData = getBasePostData().merge(data) url = "https://staging-api.usedopamine.com/v4/app/" case type when :track url = url + "track/" when :report url = url + "report/" when :refresh url = url + "refresh/" else return end if(@debugMode) puts "Data to be sent:\n#{data}\n\n" end uri = URI.parse(url) https = Net::HTTP.new(uri.host, 443) https.use_ssl = true req = Net::HTTP::Post.new(uri.path, initheader={'Content-Type' => 'application/json'}) req.body = postData.to_json res = https.request(req) if(@debugMode) puts "Response:\n#{res.read_body}\n\n" end return res.read_body end