class Dtvcontroller::SetTopBox

Attributes

base_url[R]
ip[R]

Public Class Methods

new(ip_address) click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 10
def initialize(ip_address)
  @ip = ip_address.to_s
  @base_url = "http://#{ip}:8080/"
end

Public Instance Methods

currently_playing() click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 19
def currently_playing
  url = "#{base_url}tv/getTuned"
  response_hash = get_json_parsed_response_body(url)
  channel = response_hash["major"].to_s
  network = response_hash["callsign"].to_s
  program = response_hash["title"].to_s
  result = "#{channel}: #{network} - #{program}"
  if response_hash.has_key?("episodeTitle")
    result = "#{result}: #{response_hash["episodeTitle"]}"
  end
  result
end
Also aliased as: get_channel
get_channel()
Alias for: currently_playing
get_sysinfo(item)
Alias for: system_info
send_key(key) click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 45
def send_key(key)
  raise "Unknown remote key: #{key}" unless SEND_KEY_OPTIONS.include?(key.to_sym)
  url = "#{base_url}remote/processKey?key=#{key}"
  response_hash = get_json_parsed_response_body(url)
  response_hash["status"]["msg"]
end
send_key_options() click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 41
def send_key_options
  SEND_KEY_OPTIONS
end
system_info(item) click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 56
def system_info(item)
  value = validated_hash_lookup(item, SYSTEM_INFO_HASH)
  url = "#{base_url}info/getVersion"
  response_hash = get_json_parsed_response_body(url)
  response_hash[value]
end
Also aliased as: get_sysinfo
system_info_options() click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 52
def system_info_options
  SYSTEM_INFO_HASH.keys
end
tune(number)
Alias for: tune_to_channel
tune_to_channel(number) click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 33
def tune_to_channel(number)
  return "no input" if number == ""
  url = "#{base_url}tv/tune?major=#{number}"
  response_hash = get_json_parsed_response_body(url)
  response_hash["status"]["msg"]
end
Also aliased as: tune
ver() click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 15
def ver
  "SetTopBox#ver is deprecated, use '$ gem which dtvcontroller'\n2.0.0"
end

Private Instance Methods

get_json_parsed_response_body(url) click to toggle source

TODO add a retry with counter and decrement per Ruby Tapas #257

# File lib/dtvcontroller/set_top_box.rb, line 75
def get_json_parsed_response_body(url)
  begin
    response = Net::HTTP.get(URI(url))
    JSON.parse(response)
  rescue Errno::ETIMEDOUT
    raise "Cannot connect to set top box"
  rescue Errno::EHOSTUNREACH
    raise "Unreachable host likely caused by no internet connect"
  end
end
validated_hash_lookup(key, hash_to_search) click to toggle source
# File lib/dtvcontroller/set_top_box.rb, line 66
def validated_hash_lookup(key, hash_to_search)
  begin
    hash_to_search.fetch(key.to_sym)
  rescue KeyError
    raise "Unrecognized system option: #{key}"
  end
end