Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tether Notification Script
require 'builder'
require 'net/http'
require 'optparse'
DATE_FMT = '%Y-%m-%dT%H:%M:%SZ'
class SecureOriginsTetherNotification
def initialize(devid1, devid2)
@devid1 = devid1
@devid2 = devid2
end
def gen
x = Builder::XmlMarkup.new(indent: 2)
x.instruct!
x.TetherNotification{
x.DeviceID @devid1
x.TetherTagID @devid2
x.TetherDateTime now()
}
end
def now
Time.now.utc.strftime(DATE_FMT)
end
end
class Stdout
def put(str)
puts str
end
end
class HttpOut
def initialize(uri)
@uri = URI(uri)
end
def put(str)
req = Net::HTTP::Post.new(@uri.path)
req.body = str
Net::HTTP.start(@uri.host, @uri.port) do |http|
http.request(req)
end
end
end
class CmdLineApp
def start
parse_opts
select_out
select_devids
msg = SecureOriginsTetherNotification.new(@devid1, @devid2)
@output.put(msg.gen)
end
private
def select_out
if @options[:post]
@output = HttpOut.new(@options[:post])
else
@output = Stdout.new
end
end
def select_devids
@devid1 = @options[:devid1] || '26219'
@devid2 = @options[:devid2] || '26219'
end
def parse_opts
@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby script/simulator.rb [options]"
opts.on("-p", "--post [URI]", "Post to url") do |uri|
@options[:post] = uri
end
opts.on("-a", "--devid1 [D1]", "Device ID to use") do |d1|
@options[:devid1] = d1
end
opts.on("-b", "--devid2 [D2]", "Device ID to use") do |d2|
@options[:devid2] = d2
end
end.parse!
end
end
app = CmdLineApp.new
app.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.