Class: BoincDrone::Report

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: "ABC123", webhook_url: "http://localhost:3000", worker_id: "123456") ⇒ Report

Returns a new instance of Report.



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
# File 'lib/boinc_drone.rb', line 9

def initialize(api_key: "ABC123", webhook_url: "http://localhost:3000", worker_id: "123456")
  @api_key = api_key
  @webhook_url = webhook_url
  @worker_id = worker_id
  
  @report = { 
    meta: {
     worker_id: @worker_id
    }
  }

  input = `boinccmd --get_state`
  input.each_line do |line|
    if line.match(/======== (.+) ========/i)
      match = line.match(/======== (.+) ========/i)
      @scope = match.captures.first.gsub(" ", "_").downcase.to_sym
      instance_variable_set("@#{@scope.to_s}", [])

      if @scope == :time_stats
        @time_stats = {}
        @current = @time_stats
      end
    end
  
    if line.match(/\d+\) -----------$/i)
      instance_variable_get("@#{@scope.to_s}") << {}
      @current = instance_variable_get("@#{@scope.to_s}").send(:last)
    elsif line.match(/(\S+): (.*)/i)
      match = line.match(/\s+(.+): (.*)/i)
      @current[match.captures.first.gsub(" ", "_").downcase.to_sym] = match.captures[1]
    else
      @report[@scope.to_sym] = instance_variable_get("@#{@scope.to_s}")
    end
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/boinc_drone.rb', line 7

def api_key
  @api_key
end

#reportObject

Returns the value of attribute report.



7
8
9
# File 'lib/boinc_drone.rb', line 7

def report
  @report
end

#webhook_urlObject

Returns the value of attribute webhook_url.



7
8
9
# File 'lib/boinc_drone.rb', line 7

def webhook_url
  @webhook_url
end

#worker_idObject

Returns the value of attribute worker_id.



7
8
9
# File 'lib/boinc_drone.rb', line 7

def worker_id
  @worker_id
end

Instance Method Details

#postObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/boinc_drone.rb', line 45

def post
    HTTParty.post(@webhook_url,
      body: { 
        report: @report
      },
      headers: {
        "X-API-KEY" => @api_key
      }
    )
end