summaryrefslogtreecommitdiff
path: root/bin/att
blob: fe8db05c77914fb6256a5c6ae2d89b7c7e54f92c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby

require 'thor'
require 'net/http'
require 'json'

class MockScheduler
  def method_missing(*args)
    yield
  end
end

def send_event(id, data)
  req = Net::HTTP::Post.new("/widgets/#{id}")
  req["content-type"] = "application/json"
  req.body = JSON.unparse(data.merge(:auth_token => AllTheThings::CLI.auth_token))
  res = Net::HTTP.new('localhost', 3000).start { |http| http.request(req) }
  puts "Data Sent to #{id}: #{data}"
end

SCHEDULER = MockScheduler.new

module AllTheThings

  class CLI < Thor
    include Thor::Actions

    class << self
      attr_accessor :auth_token
    end

    attr_accessor :name

    def self.source_root
      File.expand_path('../../templates', __FILE__)
    end

    desc "install PROJECT_NAME", "Sets up ALL THE THINGS needed for your dashboard project structure."
    def install(name)
      @name = Thor::Util.snake_case(name)
      directory :project, @name
    end

    desc "new_widget WIDGET_NAME", "Creates a new widget with all the fixins'"
    def new_widget(name)
      @name = Thor::Util.snake_case(name)
      directory :widget, File.join('widgets', @name)
    end 

    desc "start", "Starts the server in style!"
    def start(*args)
      args = args.join(" ")
      system("bundle exec thin -R config.ru start #{args}")
    end

    desc "job JOB_NAME AUTH_TOKEN(optional)", "Runs the specified job."
    def job(name, auth_token = "")
      self.class.auth_token = auth_token
      f = File.join(Dir.pwd, "jobs", "#{name}.rb")
      require f
    end
  
  end
end

AllTheThings::CLI.start