Sha256: dea2ff360665f125ece4b870650273020335924fcfb28f5a640c432302d4fc54

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'rest-client'
require 'bunny'
require 'json'

module TicktokCli

  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 CLI < Command
    class_option :verbose, type: :boolean
    class_option :noop, type: :boolean

    desc "clock", "Create and listen to a new clock"
    long_desc Help.text(:clock)

    def clock(name = "you")
      Clock.named("kuku").on('every.5.seconds').invoke(lambda {
          |m| puts "kuku\tevery.5.seconds\t\t#{Time.new.inspect}"}
      )
    end

    desc "version", "prints version"

    def version
      puts TicktokCli::VERSION
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ticktok-cli-0.1.1 lib/ticktok_cli/cli.rb