Sha256: 7a88d782789164a23e900cd5216beef672ad74f904c0ebde47b14ebd457af6d6

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

require 'uri'

require 'rack'

require 'murlsh'

module Murlsh

  # Serve m3u file of audio urls.
  class M3uServer

    AudioContentTypes = %w{
      application/ogg
      audio/mpeg
      audio/ogg
      }

    def initialize(config); @config = config; end

    # Respond to a GET request for m3u file.
    def get(req)
      conditions = ['content_type IN (?)', AudioContentTypes]
      search_conditions = Murlsh::SearchConditions.new(req['q']).conditions
      unless search_conditions.empty?
        conditions[0] << " AND (#{search_conditions[0]})"
        conditions.push(*search_conditions[1..-1])
      end

      page = 1
      per_page = config.fetch('num_posts_feed', 25)

      result_set = Murlsh::UrlResultSet.new(conditions, page, per_page)
      urls = result_set.results

      feed_url = URI.join(config.fetch('root_url'), 'm3u.m3u')
      body = Murlsh::M3uBody.new(config, req, feed_url, urls)

      resp = Rack::Response.new(body, 200,
        'Cache-Control' => 'must-revalidate, max-age=0',
        'Content-Type' => 'audio/x-mpegurl')
      if u = body.updated
        resp['Last-Modified'] = u.httpdate
      end
      resp
    end

    attr_reader :config
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
murlsh-1.6.1 lib/murlsh/m3u_server.rb
murlsh-1.6.0 lib/murlsh/m3u_server.rb
murlsh-1.5.0 lib/murlsh/m3u_server.rb