Skip to content

Instantly share code, notes, and snippets.

@dzoba
Created January 7, 2016 00:11
Show Gist options
  • Save dzoba/aa33b0c56de3c423b581 to your computer and use it in GitHub Desktop.
Save dzoba/aa33b0c56de3c423b581 to your computer and use it in GitHub Desktop.
Communicating With Segment.io Via Brightscript on the Roku
sub Main()
mport = CreateObject("roMessagePort")
roUrlTransfer = CreateObject("roUrlTransfer")
roUrlTransfer.AddHeader("Authorization", "Basic <BASE 64 ENCODED SEGMENT WRITE KEY>")
roUrlTransfer.AddHeader("Accept", "application/json")
roUrlTransfer.AddHeader("Content-Type", "application/json")
roUrlTransfer.EnablePeerVerification(false)
roUrlTransfer.EnableHostVerification(false)
roUrlTransfer.RetainBodyOnError(true)
roUrlTransfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
roUrlTransfer.InitClientCertificates()
roUrlTransfer.SetMessagePort(mport)
roUrlTransfer.SetUrl("https://api.segment.io/v1/track")
associativeArray = {
anonymousId: 1234567890
event: "Test Event 1234"
properties: {
field1: "this is field 1s value"
}
}
json = FormatJson(associativeArray)
json = strReplace(json, "anonymousid", "anonymousId")
print "Posting to " + roUrlTransfer.GetUrl() + ": " + json
if(roUrlTransfer.AsyncPostFromString(json))
while true
msg = wait(0, mport)
if type(msg) = "roUrlEvent"
print msg.GetResponseCode()
print msg.GetString()
end if
end while
end if
end sub
function strReplace(basestr As String, oldsub As String, newsub As String) As String
newstr = ""
i = 1
while i <= Len(basestr)
x = Instr(i, basestr, oldsub)
if x = 0 then
newstr = newstr + Mid(basestr, i)
exit while
endif
if x > i then
newstr = newstr + Mid(basestr, i, x-i)
i = x
endif
newstr = newstr + newsub
i = i + Len(oldsub)
end while
return newstr
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment