Module: TivoHMO::Server::Helpers

Includes:
Rack::Utils
Defined in:
lib/tivohmo/server.rb

Instance Method Summary collapse

Instance Method Details

#container_url(container) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/tivohmo/server.rb', line 115

def container_url(container)
  url = "/TiVoConnect"
  params = {
      Command: 'QueryContainer',
      Container: container.title_path
  }
  url << "?" << build_query(params)
end

#format_date(time) ⇒ Object

to format date/time as needed by tivo in builders



146
147
148
# File 'lib/tivohmo/server.rb', line 146

def format_date(time)
  "0x#{time.to_i.to_s(16)}"
end

#format_iso_date(time) ⇒ Object



150
151
152
153
# File 'lib/tivohmo/server.rb', line 150

def format_iso_date(time)
  return "" unless time
  time.utc.strftime("%FT%T.%6N")
end

#format_iso_duration(duration) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/tivohmo/server.rb', line 155

def format_iso_duration(duration)
  return "" unless duration
  seconds = duration % 60
  minutes = (duration / 60) % 60
  hours = (duration / (60 * 60)) % 24
  days = (duration / (60 * 60 * 24)).to_i
  'P%sDT%sH%sM%sS' % [days, hours, minutes, seconds]
end

#format_uuid(s) ⇒ Object

to format uuids as needed by tivo in builders



141
142
143
# File 'lib/tivohmo/server.rb', line 141

def format_uuid(s)
  Zlib.crc32(s)
end

#item_detail_url(item) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tivohmo/server.rb', line 128

def item_detail_url(item)
  url = "/TiVoConnect"
  container = item.app.title
  file = item.title_path.sub("/#{container}/", '')
  params = {
      Command: 'TVBusQuery',
      Container: container,
      File: file
  }
  url << "?" << build_query(params)
end

#item_url(item) ⇒ Object



124
125
126
# File 'lib/tivohmo/server.rb', line 124

def item_url(item)
  item.title_path
end

#loggerObject



62
63
64
# File 'lib/tivohmo/server.rb', line 62

def logger
  Server.logger
end

#pad(length, align) ⇒ Object



164
165
166
167
168
# File 'lib/tivohmo/server.rb', line 164

def pad(length, align)
  extra = length % align
  extra = align - extra if extra > 0
  extra
end

#select_all_items(children) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tivohmo/server.rb', line 79

def select_all_items(children)
  just_items = []
  all = children.dup
  all.each do |child|
    if child.is_a?(TivoHMO::API::Container)
      all.concat(child.children)
    else
      just_items << child
    end
  end
  children = just_items.uniq {|node| node.identifier }
end

#serverObject



66
67
68
# File 'lib/tivohmo/server.rb', line 66

def server
  @server
end

#sort(items, sort_order) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tivohmo/server.rb', line 92

def sort(items, sort_order)
  sort_order = sort_order.split(/\s*,\s*/)

  sort_order.each do |order|

    reverse = false
    if order[0] == '!'
      reverse = true
      order = order[1..-1]
    end

    case order
      when 'Title'
        items = items.sort_by(&:title)
      when 'CaptureDate'
        items = items.sort_by(&:created_at)
    end

    items = items.reverse if reverse
  end
  items
end

#tivo_header(item, mime) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tivohmo/server.rb', line 170

def tivo_header(item, mime)
  if mime == 'video/x-tivo-mpeg-ts'
    flag = 45
  else
    flag = 13
  end

  item_details_xml = builder :item_details, layout: true, locals: {item: item}
  item_details_xml.force_encoding('ascii-8bit')

  ld = item_details_xml.bytesize
  chunk = item_details_xml + '\0' * (pad(ld, 4) + 4)
  lc = chunk.bytesize
  blocklen = lc * 2 + 40
  padding = pad(blocklen, 1024)

  data = 'TiVo'
  data << [4, flag, 0, padding + blocklen, 2].pack('nnnNn')
  data << [lc + 12, ld, 1, 0].pack('NNnn')
  data << chunk
  data << [lc + 12, ld, 2, 0].pack('NNnn')
  data << chunk
  data << "\0" * padding

  data
end

#transliterate(s) ⇒ Object



197
198
199
200
# File 'lib/tivohmo/server.rb', line 197

def transliterate(s)
  # unidecoder gem
  s.to_ascii rescue s
end

#tsnObject



75
76
77
# File 'lib/tivohmo/server.rb', line 75

def tsn
  headers['TiVo_TCD_ID']
end

#unsupportedObject



70
71
72
73
# File 'lib/tivohmo/server.rb', line 70

def unsupported
  status 404
  builder :unsupported, layout: true
end