class TVTid::Client
Constants
- API_BASE_URI
- CACHE_SOFT_TTL
- CACHE_TTL
The cache time to live.
- DEFAULT_CHANNELS
The default channels to return in a days schedule
- HTTP_REQUEST_HEADERS
The default HTTP request headers
Public Class Methods
new()
click to toggle source
Constructs a new client.
# File library/tvtid/client.rb, line 25 def initialize @cache = LRUCache.new ttl: CACHE_TTL, soft_ttl: CACHE_SOFT_TTL @http = Net::HTTP.new API_BASE_URI.host, API_BASE_URI.port end
Public Instance Methods
channel_schedule(channel, date = Date.today)
click to toggle source
Returns a days schedule for a given channel and date
@param channel [Channel] The channel to get the schedule for @param date [Date] The date of the schedule
# File library/tvtid/client.rb, line 78 def channel_schedule channel, date = Date.today schedules_for(date, [channel]).first end
channels()
click to toggle source
Returns a list of channels
# File library/tvtid/client.rb, line 83 def channels @cache.fetch 'channels' do response = @http.get '/tvtid-app-backend/channels', HTTP_REQUEST_HEADERS json_data = MultiJson.load response.body json_data.map{|json_channel_data| Channel.from_json json_channel_data } end end
get_program_details!(program)
click to toggle source
Retrieves program details and updates the given program object.
@return [Program] the program
# File library/tvtid/client.rb, line 95 def get_program_details! program response = @http.get "/tvtid-app-backend/channels/#{program.channel_id}/programs/#{program.id}", HTTP_REQUEST_HEADERS if response.code == '200' program.parse_json! MultiJson.load(response.body) end program end
schedules_for(date, channels = [])
click to toggle source
Returns a schedule for the provided channels the given date
@param date A date @param channels A list of channel ids to request schedules for @return [Array<Schedule>] the list of schedules
# File library/tvtid/client.rb, line 35 def schedules_for date, channels = [] return nil unless date.is_a? Date channels = self.channels.select{|c| DEFAULT_CHANNELS.include? c.id } if channels.empty? formatted_date = date.iso8601 cache_key = "schedule-#{formatted_date}-#{channels.map(&:id).join ','}" @cache.fetch cache_key do channel_queries = channels.map{|c| "ch=#{c.id}" }.join '&' response = @http.get "/tvtid-app-backend/dayviews/#{formatted_date}?#{channel_queries}", HTTP_REQUEST_HEADERS json_data = MultiJson.load response.body json_data.map do |schedule| channel = channels.find{|channel| channel.id == schedule['id']} programs = schedule['programs'].map do |program| program = Program.from_json program program.channel_id = schedule['id'] program end programs.sort!{|a, b| a.start_time <=> b.start_time } Schedule.new channel, programs end end end
schedules_for_today(channels = [])
click to toggle source
Returns a list of schedules for today
This is equivalent to using `chedules_for Date.today`
# File library/tvtid/client.rb, line 64 def schedules_for_today channels = [] date_time = DateTime.now if date_time.hour <= 5 and date_time.hour >= 0 date_time = date_time - 1 end schedules_for date_time.to_date, channels end