Class: TVTid::Client

Inherits:
Object
  • Object
show all
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-app-backend.tv2.dk'
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'
}
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

Constructor Details

#initializeClient

Constructs a new client.


24
25
26
27
# File 'library/tvtid/client.rb', line 24

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

Parameters:

  • channel (Channel)

    The channel to get the schedule for

  • date (Date) (defaults to: Date.today)

    The date of the schedule


71
72
73
# File 'library/tvtid/client.rb', line 71

def channel_schedule channel, date = Date.today
  schedules_for(date, [channel]).first
end

#channelsObject

Returns a list of channels


76
77
78
79
80
81
82
83
# File 'library/tvtid/client.rb', line 76

def channels
  @cache.fetch 'channels' do
    response = @http.get '/api/tvtid/v1/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.

Returns:


88
89
90
91
92
93
94
95
96
# File 'library/tvtid/client.rb', line 88

def get_program_details! program
  response = @http.get "/api/tvtid/v1/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

Parameters:

  • date

    A date

  • channels (defaults to: [])

    A list of channel ids to request schedules for

Returns:

  • (Array<Schedule>)

    the list of schedules


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'library/tvtid/client.rb', line 34

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 "/api/tvtid/v1/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`


63
64
65
# File 'library/tvtid/client.rb', line 63

def schedules_for_today channels = []
  schedules_for Date.today, channels
end