Class: Ruku::Remotes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruku/remotes.rb,
lib/ruku/clients/web.rb

Overview

A collection of Ruku::Remotes. Keeps track of one or multiple Remotes for Roku boxes. Manages things like which box is active for use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boxes = [], storage = SimpleStorage.new) ⇒ Remotes

Returns a new instance of Remotes.



10
11
12
13
14
# File 'lib/ruku/remotes.rb', line 10

def initialize(boxes=[], storage=SimpleStorage.new)
  @boxes, @storage = boxes, storage
  boxes.uniq!
  @active_index = 0
end

Instance Attribute Details

#active_indexObject

Returns the value of attribute active_index.



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

def active_index
  @active_index
end

#boxesObject

Returns the value of attribute boxes.



8
9
10
# File 'lib/ruku/remotes.rb', line 8

def boxes
  @boxes
end

#storageObject

Returns the value of attribute storage.



8
9
10
# File 'lib/ruku/remotes.rb', line 8

def storage
  @storage
end

Instance Method Details

#[](index) ⇒ Object



28
29
30
# File 'lib/ruku/remotes.rb', line 28

def [](index)
  boxes[index]
end

#[]=(index, box) ⇒ Object



32
33
34
# File 'lib/ruku/remotes.rb', line 32

def []=(index, box)
  boxes[index] = box
end

#activeObject



36
37
38
39
# File 'lib/ruku/remotes.rb', line 36

def active
  return boxes[@active_index] if !boxes.empty? && boxes.size > active_index
  nil
end

#add(box) ⇒ Object



51
52
53
54
# File 'lib/ruku/remotes.rb', line 51

def add(box)
  boxes << box
  boxes.sort!.uniq!
end

#each(&b) ⇒ Object



16
17
18
# File 'lib/ruku/remotes.rb', line 16

def each(&b)
  boxes.each &b
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/ruku/remotes.rb', line 24

def empty?
  boxes.empty?
end

#find_by_host(host) ⇒ Object



47
48
49
# File 'lib/ruku/remotes.rb', line 47

def find_by_host(host)
  boxes.find {|box| box.host == host}
end

#loadObject



67
68
69
70
# File 'lib/ruku/remotes.rb', line 67

def load
  loaded = storage.load
  self.boxes, self.active_index = loaded.boxes, loaded.active_index
end

#remotes_from_json(json) ⇒ Object



119
120
121
122
123
124
# File 'lib/ruku/clients/web.rb', line 119

def remotes_from_json(json)
  parsed = JSON.parse(json)
  @boxes = []
  parsed['remotes'].each {|r| @boxes << Remote.new(r['host'], r['name'], r['port'] || 8080) }
  @active_index = parsed['active'] || 0
end

#remotes_to_jsonObject



115
116
117
# File 'lib/ruku/clients/web.rb', line 115

def remotes_to_json
  "{\"remotes\":[#{ @boxes.map{|b| b.to_json}.join(',') }], \"active\":#{@active_index}}"
end

#remove(box) ⇒ Object



56
57
58
59
60
61
# File 'lib/ruku/remotes.rb', line 56

def remove(box)
  host = box.is_a?(Remote) ? box.host : box
  boxes.reject! { |b| b.host == host }
  self.active_index = 0 if @active_index >= boxes.size
  boxes.sort!
end

#set_active(box) ⇒ Object



41
42
43
44
45
# File 'lib/ruku/remotes.rb', line 41

def set_active(box)
  new_index = nil
  boxes.each_with_index {|b,i| new_index = i if b == box} if box.is_a? Remote
  self.active_index = new_index if new_index and new_index < boxes.size
end

#sizeObject



20
21
22
# File 'lib/ruku/remotes.rb', line 20

def size
  boxes.size
end

#storeObject



63
64
65
# File 'lib/ruku/remotes.rb', line 63

def store
  storage.store(self)
end