Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit ed9e0b2b0a6d6e827daaaa8e2bd20f0e1476217f @youpy committed Jun 18, 2012
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,2 @@
+--color
+--format progress
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in rack-musicindex.gemspec
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2012 TODO: Write your name
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# Rack::Musicindex
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'rack-musicindex'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install rack-musicindex
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,10 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
+
+require 'rspec/core'
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = FileList['spec/**/*_spec.rb']
+end
+
+task :default => :spec
@@ -0,0 +1,2 @@
+require "rack/musicindex"
+require "rack/musicindex/version"
@@ -0,0 +1,88 @@
+require 'builder'
+
+module Rack
+ class MusicIndex
+ def initialize(app, options = {})
+ @app = app
+
+ init(options)
+ end
+
+ def init(options)
+ @dirs = options
+ @files = {}
+ @static_paths = {}
+ @dirs.each do |path, dir|
+ @files[path] = Dir[dir + '/*.mp3']
+ @files[path].each do |filename|
+ @static_paths[path + '/' + ::File.basename(filename)] = filename
+ end
+ end
+ end
+
+ def call(env)
+ status, headers, response = @app.call(env)
+
+ path_info = env['PATH_INFO']
+
+ if dirs[path_info]
+ headers['Content-Type'] = 'application/xml;charset=utf-8'
+ body = podcast(env)
+ headers["Content-Length"] = body.length.to_s
+ response = [body]
+ status = 200
+ elsif static_paths.include?(path_info)
+ body = open(static_paths[path_info], 'rb').read
+ headers["Content-Type"] = 'audio/mpeg'
+ headers["Content-Length"] = body.length.to_s
+ response = [body]
+ status = 200
+ end
+
+ [status, headers, response]
+ end
+
+ private
+
+ def dirs
+ @dirs
+ end
+
+ def files(media_dir)
+ @files[media_dir]
+ end
+
+ def static_paths
+ @static_paths
+ end
+
+ def podcast(env)
+ path = env['PATH_INFO']
+ req = Rack::Request.new(env)
+ url = req.url
+ files = files(path)
+ xml = ::Builder::XmlMarkup.new
+
+ xml.instruct! :xml, :version => '1.0'
+ xml.rss :version => "2.0", 'xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd' do
+ xml.channel do
+ xml.title path
+ xml.description 'Generated by Rack::MusicIndex'
+ xml.link url
+
+ files.each do |file|
+ name = ::File.basename(file)
+ item_link = url + '/' + name
+ xml.item do
+ xml.title name
+ xml.description name
+ xml.link item_link
+ xml.guid item_link
+ xml.enclosure :url => item_link
+ end
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Rack
+ module Musicindex
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/rack/musicindex/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["TODO: Write your name"]
+ gem.email = ["TODO: Write your email address"]
+ gem.description = %q{TODO: Write a gem description}
+ gem.summary = %q{TODO: Write a gem summary}
+ gem.homepage = ""
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "rack-musicindex"
+ gem.require_paths = ["lib"]
+ gem.version = Rack::Musicindex::VERSION
+
+ gem.add_dependency('builder')
+ gem.add_development_dependency('rspec', ['~> 2.8.0'])
+ gem.add_development_dependency('rake')
+ gem.add_development_dependency('sinatra')
+ gem.add_development_dependency('rack-test')
+ gem.add_development_dependency('nokogiri')
+end
Binary file not shown.
@@ -0,0 +1,41 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../test_app')
+
+set :environment, :test
+
+require 'nokogiri'
+
+describe Rack::MusicIndex do
+ def app
+ Sinatra::Application
+ end
+
+ it 'should index mp3s as podcast' do
+ get '/foo'
+
+ last_response.should be_ok
+
+ xml = Nokogiri::XML(last_response.body)
+
+ channel = xml.xpath('//channel')[0]
+ items = xml.xpath('//item')
+ item = items[0]
+
+ channel.xpath('title')[0].content.should eql('/foo')
+ channel.xpath('description')[0].content.should eql('Generated by Rack::MusicIndex')
+
+ items.size.should eql(1)
+ item.xpath('title')[0].content.should eql('test.mp3')
+ item.xpath('link')[0].content.should eql('http://example.org/foo/test.mp3')
+ item.xpath('guid')[0].content.should eql('http://example.org/foo/test.mp3')
+ item.xpath('enclosure')[0]['url'].should eql('http://example.org/foo/test.mp3')
+ end
+
+ it 'should return mp3' do
+ get '/foo/test.mp3'
+
+ last_response.should be_ok
+ last_response['Content-Type'].should eql('audio/mpeg')
+ last_response.body.should eql(open(fixture('/foo/test.mp3')).read)
+ end
+end
@@ -0,0 +1,19 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+
+require 'rack/test'
+
+RSpec.configure do |config|
+ def fixture(filename)
+ File.dirname(__FILE__) + '/fixtures/' + filename
+ end
+
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+ config.include Rack::Test::Methods
+end
@@ -0,0 +1,6 @@
+require 'sinatra'
+require 'rack-musicindex'
+
+use Rack::MusicIndex, {
+ '/foo' => File.expand_path(File.dirname(__FILE__) + '/fixtures/foo')
+}

0 comments on commit ed9e0b2

Please sign in to comment.