Class: Host

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/host.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_api(opts) ⇒ Object


8
9
10
11
12
13
# File 'app/models/host.rb', line 8

def self.from_api(opts)
  opts = opts[:host] if opts[:host] # Rails' forms wraps hashes in a root tag
  host = new(name: opts['name'], url: opts['url'])
  host.update_status if host.save
  host
end

.with_available_slotsObject


15
16
17
# File 'app/models/host.rb', line 15

def self.with_available_slots
  all.map(&:update_status).select { |h| h.available_slots > 0 }.shuffle
end

Instance Method Details

#update_statusObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/host.rb', line 19

def update_status
  return self if status_updated_at && status_updated_at > 10.seconds.ago

  self.available = false
  self.status_updated_at = Time.current

  attrs = Transcoder.host_status(self)
  if attrs
    self.total_slots          = attrs['max_slots']
    self.available_slots      = attrs['free_slots']
    self.available            = true
  end

  save

  self
end