Class: TVTid::Client
- Inherits:
-
Object
- Object
- TVTid::Client
- Defined in:
- library/tvtid/client.rb
Constant Summary collapse
- CACHE_TTL =
The cache time to live.
1 * 60 * 60 * 24
- CACHE_SOFT_TTL =
The soft cache time to live.
7 * 60 * 60 * 24
- API_BASE_URI =
The API backend host.
URI 'http://tvtid-backend.tv2.dk/tvtid-app-backend'
- HTTP_REQUEST_HEADERS =
The default HTTP request headers
{ 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', 'Accept' => 'application/json' }
- DEFAULT_CHANNELS =
The default channels to return in a days schedule
[1, 3, 5, 2, 31, 133, 7, 6, 4, 10155, 10154, 10153, 8, 77, 156, 10093, 10066, 14, 10089, 12566, 10111, 70, 118, 153, 94, 12948, 145, 185, 157, 15, 71, 93, 15049, 219, 37, 248, 186]
Instance Method Summary collapse
-
#channel_schedule(channel, date = Date.today) ⇒ Object
Returns a days schedule for a given channel and date.
-
#channels ⇒ Object
Returns a list of channels.
-
#get_program_details!(program) ⇒ Program
Retrieves program details and updates the given program object.
-
#initialize ⇒ Client
constructor
Constructs a new client.
-
#schedules_for(date, channels = []) ⇒ Array<Schedule>
Returns a schedule for the provided channels the given date.
-
#schedules_for_today(channels = []) ⇒ Object
Returns a list of schedules for today.
Constructor Details
#initialize ⇒ Client
Constructs a new client.
25 26 27 28 |
# 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 |
Instance Method Details
#channel_schedule(channel, date = Date.today) ⇒ Object
Returns a days schedule for a given channel and date
78 79 80 |
# File 'library/tvtid/client.rb', line 78 def channel_schedule channel, date = Date.today schedules_for(date, [channel]).first end |
#channels ⇒ Object
Returns a list of channels
83 84 85 86 87 88 89 90 |
# 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) ⇒ Program
Retrieves program details and updates the given program object.
95 96 97 98 99 100 101 102 103 |
# 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 = []) ⇒ Array<Schedule>
Returns a schedule for the provided channels the given date
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 = []) ⇒ Object
Returns a list of schedules for today
This is equivalent to using ‘chedules_for Date.today`
64 65 66 67 68 69 70 71 72 |
# 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 |