Class: TicktokCli::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/ticktok_cli/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Clock

Returns a new instance of Clock.



10
11
12
# File 'lib/ticktok_cli/cli.rb', line 10

def initialize(name)
  @name = name
end

Class Method Details

.named(name) ⇒ Object



14
15
16
# File 'lib/ticktok_cli/cli.rb', line 14

def self.named(name)
  Clock.new(name)
end

Instance Method Details

#consume(tick_channel, action) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ticktok_cli/cli.rb', line 36

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

#create_clockObject



28
29
30
31
32
33
34
# File 'lib/ticktok_cli/cli.rb', line 28

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

#invoke(action) ⇒ Object



23
24
25
26
# File 'lib/ticktok_cli/cli.rb', line 23

def invoke(action)
  channel = create_clock
  consume(channel, action)
end

#on(schedule) ⇒ Object



18
19
20
21
# File 'lib/ticktok_cli/cli.rb', line 18

def on(schedule)
  @schedule = schedule
  self
end