Class: E4z::DevBot

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DevBot

Returns a new instance of DevBot.



9
10
11
12
13
14
# File 'lib/eezee_bot.rb', line 9

def initialize(path)
  @git = Git.init(path)
  @working_dir = path
  @eezee_token = ENV['E4Z_TOKEN']
  @eezee_client = ::E4z::ApiClient.new(@eezee_token)
end

Instance Method Details

#github_repoObject



42
43
44
# File 'lib/eezee_bot.rb', line 42

def github_repo
  @github_repo ||= @git.remotes.find { |r| r.name == 'origin' }.url.match(/:(.+)\.git/)[1]
end

#project_idObject



46
47
48
# File 'lib/eezee_bot.rb', line 46

def project_id
  @project_id ||= @eezee_client.find_project_by_github_slug(github_repo)['id']
end

#project_slugObject



38
39
40
# File 'lib/eezee_bot.rb', line 38

def project_slug
  "project-#{project_id}"
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eezee_bot.rb', line 16

def run
  ::E4z::SocketClient.new(project_slug) do |data|
    instruction_id = JSON.parse(data)['instruction_id']
    instruction = @eezee_client.get_instruction(project_id, instruction_id)
    data = instruction['data']
    execute = instruction['execute']

    puts "Running `#{execute}` with #{data}"

    begin
      OpenStruct.new(data).instance_eval(execute)
    rescue Exception => e
      puts "Err log data: #{data}"
      puts "Err log execute: #{execute}"
      puts e.message
    end

    payload = { did_run: true }
    @eezee_client.update_instruction(project_id, instruction_id, payload)
  end
end