Sha256: cddcc02b4327b7b1760c6ab8f21f8ca87418a605edeac3f65ee52e56bb4f9f8e
Contents?: true
Size: 1.48 KB
Versions: 1
Compression:
Stored size: 1.48 KB
Contents
require 'thor' require 'bunny' require 'rest-client' require 'json' module Ticktok class Clock def initialize(name) @name = name end def self.named(name) Clock.new(name) end def on(schedule) @schedule = schedule self end def invoke(action) channel = create_clock consume(channel, action) end def create_clock resp = RestClient.post("https://ticktok-io-dev.herokuapp.com/api/v1/clocks?access_token=#{ENV["ACCESS_TOKEN"]}", {name: @name, schedule: @schedule}.to_json, {content_type: :json}) raise "Failed to create a clock" unless resp.code == 201 JSON.parse(resp.body)['channel'] end def consume(tick_channel, action) connection = Bunny.new(tick_channel['details']['uri']) connection.start ch = connection.create_channel queue = ch.queue(tick_channel['details']['queue'], :passive => true) begin queue.subscribe(block: true) do |delivery_info, _properties, body| action.call(body) end rescue Interrupt => _ ch.close connection.close end end end class TickListener end class TicktokCLI < Thor desc "clock", "Create and listen to a new clock" def clock(schedule) Clock.named("kuku").on('every.5.seconds').invoke(lambda { |m| puts "kuku\tevery.5.seconds\t\t#{Time.new.inspect}"} ) end end TicktokCLI.start(ARGV) end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ticktok-cli-0.1.0 | lib/app.rb |