Class: Ruku::Clients::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/ruku/clients/simple.rb

Overview

Provides a little wrapper around a Ruku::Remotes for ease of making a command line client or messing around in an IRB session

Constant Summary collapse

OPERATION_NAMES =
%w[scan list add remove name activate help]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rs = Ruku::Remotes.new) ⇒ Simple

Returns a new instance of Simple.



14
15
16
# File 'lib/ruku/clients/simple.rb', line 14

def initialize(rs=Ruku::Remotes.new)
  @remotes = rs
end

Instance Attribute Details

#remotesObject

Returns the value of attribute remotes.



12
13
14
# File 'lib/ruku/clients/simple.rb', line 12

def remotes
  @remotes
end

Instance Method Details

#[](num) ⇒ Object

Get remotes using 1-based index for the command line



184
185
186
# File 'lib/ruku/clients/simple.rb', line 184

def [](num)
  remotes[num-1]
end

#[]=(num, box) ⇒ Object

Assign and store remotes using 1-based index for the command line



189
190
191
192
# File 'lib/ruku/clients/simple.rb', line 189

def []=(num, box)
  remotes[num-1] = box
  store
end

#activate(number = nil) ⇒ Object

Raises:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ruku/clients/simple.rb', line 156

def activate(number=nil)
  raise UsageError, 'Must specify number from remotes list or IP/hostname' if not number

  msg = 'Box '
  box = if number.is_a?(Integer) || number =~ /^\d+$/
    msg << (number).to_s
    self[number.to_i]
  else
    msg << "with IP/host #{number}"
    remotes.find_by_host(number)
  end

  if box
    remotes.set_active(box)
    store
    puts msg + ' activated for use'
  else
    puts 'Unknown box specified'
  end
end

#add(host = nil, name = 'My Roku Box') ⇒ Object

Add a box

Raises:



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruku/clients/simple.rb', line 103

def add(host=nil, name='My Roku Box')
  raise UsageError, 'Must specify host of box to add' if not host

  if existing = remotes.find_by_host(host)
    existing.name = name
  else
    remotes.add(Ruku::Remote.new(host, name))
  end
  store
  puts "Added remote with host: #{host} and name: #{name}"
end

#execute_command_from_command_lineObject

Checks the command line arguments for a command (options should have already been parsed and removed) and then sends a Roku command or runs an operation on the RemoteManager.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruku/clients/simple.rb', line 31

def execute_command_from_command_line
  cmd = ARGV[0]
  if not cmd
    puts CMD_LINE_HELP
  elsif OPERATION_NAMES.include?(cmd) && !options.force_command
    begin
      self.send(*ARGV)
    rescue ArgumentError => ex
      $stderr.puts "Wrong number of arguments (#{ARGV.size-1}) for operation: #{cmd}"
    end
  else
    send_roku_command cmd
  end
end

#handle_optionsObject

Parse and handle command line options



52
53
54
55
56
# File 'lib/ruku/clients/simple.rb', line 52

def handle_options
  opts = OptionParser.new do |opts|
    opts.on('-c', '--force-roku-command') { options.force_command = true }
  end.parse!
end

#helpObject



177
178
179
# File 'lib/ruku/clients/simple.rb', line 177

def help
  puts CMD_LINE_HELP
end

#listObject

List the boxes we know about



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruku/clients/simple.rb', line 89

def list
  if remotes.empty?
    puts "No Roku boxes known\n" +
      "Use the scan or add operations to find or add boxes"
  else
    puts 'Roku boxes:'
    remotes.each_with_index do |box, i|
      print "#{i+1}. #{box.name || '(no name)'} at #{box.host}"
      print "#{' <-- active' if i == remotes.active_index}\n"
    end
  end
end

#name(number = nil, name = nil) ⇒ Object

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ruku/clients/simple.rb', line 139

def name(number=nil, name=nil)
  raise UsageError, 'Must specify number from remotes list or IP/hostname' if not number
  raise UsageError, 'Must specify name for box' if not name

  msg = 'Box '
  if number.is_a?(Integer) || number =~ /^\d+$/
    self[number.to_i].name = name
    msg << (number).to_s
  else
    remotes.find_by_host(number).name = name
    msg << "with IP/host #{number}"
  end
  msg << " renamed to #{name}"
  store
  puts msg
end

#optionsObject

Client options generally parsed from the command line



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

def options
  @options ||= OpenStruct.new
end

#remove(number = nil) ⇒ Object

Remove a box with the given number (from the list operation) or hostname

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruku/clients/simple.rb', line 116

def remove(number=nil)
  raise UsageError, 'Must specify number from boxes list or hostname/IP address' if not number

  prev_count = remotes.size
  msg = 'Box '
  if number.is_a?(Integer) || number =~ /^\d+$/
    index = number.to_i - 1
    remotes.boxes.delete_at(index)
    msg << (index + 1).to_s
  else
    remotes.remove(number)
    msg << "with IP/host #{number}"
  end
  msg << ' removed'

  if prev_count == remotes.size + 1
    remotes.store
    puts msg
  else
    puts "Could not remove box: #{number}"
  end
end

#run_from_command_lineObject

Run from the command line. This parses options as well as the command or Ruku operation to run.



20
21
22
23
24
25
26
# File 'lib/ruku/clients/simple.rb', line 20

def run_from_command_line
  handle_exceptions do
    remotes.load
    handle_options
    execute_command_from_command_line
  end
end

#scanObject

Scan for boxes



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruku/clients/simple.rb', line 71

def scan
  remotes.boxes = Ruku::Remote.scan
  remotes.each_with_index do |box, i|
    box.name = "My Roku Box#{i == 1 ? i+1 : ''}"
  end
  if remotes.empty?
    puts 'Did not find any Roku boxes'
  else
    puts 'Roku boxes found:'
    remotes.each_with_index do |box, i|
      print "#{i+1}. #{box.name || '(no name)'} at #{box.host}"
      print "#{' <-- active' if i == remotes.active_index && remotes.size > 1}\n"
    end
    store
  end
end

#send_roku_command(cmd) ⇒ Object

Send a command to the active box



59
60
61
62
63
64
65
66
# File 'lib/ruku/clients/simple.rb', line 59

def send_roku_command(cmd)
  if remotes.empty?
    raise UsageError, "No known Roku boxes\n" +
      "Try 'ruku scan' to find them, or 'ruku add HOST NAME' to add one manually"
  else
    remotes.active.send_roku_command cmd
  end
end

#storeObject



194
195
196
# File 'lib/ruku/clients/simple.rb', line 194

def store
  remotes.store
end