class Nowtv::Client

Constants

API_URL

Public Instance Methods

get_program_list(region_id) click to toggle source
# File lib/nowtv/client.rb, line 10
def get_program_list(region_id)
  programs = get_programs(region_id)
  restruct_program_list(programs)
rescue
  raise ParseError, "Failed to parse program data"
end

Private Instance Methods

channel_id(display_channel) click to toggle source
# File lib/nowtv/client.rb, line 42
def channel_id(display_channel)
  half_width(display_channel).scan(/\d+/).first.to_i
end
get_programs(region_id) click to toggle source
# File lib/nowtv/client.rb, line 19
def get_programs(region_id)
  url = API_URL + region_id.to_s
  JSON.parse(open(url).read)["ProgramScheduleInfomartion"]["MediaLocation"]["StationLocation"]
end
half_width(str) click to toggle source
# File lib/nowtv/client.rb, line 38
def half_width(str)
  NKF.nkf("-wZ0", str).gsub(" ", " ")
end
parse_datetime(datetime) click to toggle source
# File lib/nowtv/client.rb, line 46
def parse_datetime(datetime)
  date, time = datetime.split(" ")
  hour, minute = time.split(":").map(&:to_i)

  hour >= 24 ? parse_midnight_time(date, hour, minute) : Time.parse(datetime)
end
parse_midnight_time(date_str, hour, minute) click to toggle source
# File lib/nowtv/client.rb, line 53
def parse_midnight_time(date_str, hour, minute)
  day_add = hour / 24
  hour %= 24

  datetime = Time.parse(date_str)
  datetime += day_add * 60 * 60 * 24
  datetime += hour * 60 * 60
  datetime += minute * 60

  datetime
end
restruct_program_list(programs) click to toggle source
# File lib/nowtv/client.rb, line 24
def restruct_program_list(programs)
  programs.map do |program|
    info = program["ProgramInformation"]

    {
      station: half_width(program["stationDispName"]),
      channel_id: channel_id(program["additionalDisplayChannel"]),
      title: half_width(info["programTitle"]),
      start_time: parse_datetime(info["startDateTime"]),
      end_time: parse_datetime(info["endDateTime"])
    }
  end.sort_by { |program| program[:channel_id] }
end