rack-musicindex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-musicindex.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Rack::Musicindex
2
+
3
+ A Rack middleware to publish directries containing media files as podcast
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-musicindex'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-musicindex
18
+
19
+ ## Usage
20
+
21
+ require 'sinatra'
22
+ require 'rack-musicindex'
23
+
24
+ use Rack::MusicIndex, {
25
+ '/foo' => '/path/to/mp3s')
26
+ }
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require "rack/musicindex"
2
+ require "rack/musicindex/version"
@@ -0,0 +1,88 @@
1
+ require 'builder'
2
+
3
+ module Rack
4
+ class MusicIndex
5
+ def initialize(app, options = {})
6
+ @app = app
7
+
8
+ init(options)
9
+ end
10
+
11
+ def init(options)
12
+ @dirs = options
13
+ @files = {}
14
+ @static_paths = {}
15
+ @dirs.each do |path, dir|
16
+ @files[path] = Dir[dir + '/*.mp3']
17
+ @files[path].each do |filename|
18
+ @static_paths[path + '/' + ::File.basename(filename)] = filename
19
+ end
20
+ end
21
+ end
22
+
23
+ def call(env)
24
+ status, headers, response = @app.call(env)
25
+
26
+ path_info = env['PATH_INFO']
27
+
28
+ if dirs[path_info]
29
+ headers['Content-Type'] = 'application/xml;charset=utf-8'
30
+ body = podcast(env)
31
+ headers["Content-Length"] = body.length.to_s
32
+ response = [body]
33
+ status = 200
34
+ elsif static_paths.include?(path_info)
35
+ body = open(static_paths[path_info], 'rb').read
36
+ headers["Content-Type"] = 'audio/mpeg'
37
+ headers["Content-Length"] = body.length.to_s
38
+ response = [body]
39
+ status = 200
40
+ end
41
+
42
+ [status, headers, response]
43
+ end
44
+
45
+ private
46
+
47
+ def dirs
48
+ @dirs
49
+ end
50
+
51
+ def files(media_dir)
52
+ @files[media_dir]
53
+ end
54
+
55
+ def static_paths
56
+ @static_paths
57
+ end
58
+
59
+ def podcast(env)
60
+ path = env['PATH_INFO']
61
+ req = Rack::Request.new(env)
62
+ url = req.url
63
+ files = files(path)
64
+ xml = ::Builder::XmlMarkup.new
65
+
66
+ xml.instruct! :xml, :version => '1.0'
67
+ xml.rss :version => "2.0", 'xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd' do
68
+ xml.channel do
69
+ xml.title path
70
+ xml.description 'Generated by Rack::MusicIndex'
71
+ xml.link url
72
+
73
+ files.each do |file|
74
+ name = ::File.basename(file)
75
+ item_link = url + '/' + name
76
+ xml.item do
77
+ xml.title name
78
+ xml.description name
79
+ xml.link item_link
80
+ xml.guid item_link
81
+ xml.enclosure :url => item_link
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Musicindex
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rack/musicindex/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["youpy"]
6
+ gem.email = ["youpy@buycheapviagraonlinenow.com"]
7
+ gem.description = %q{A Rack middleware to publish directries containing media files as podcast}
8
+ gem.summary = %q{A Rack middleware to publish directries containing media files as podcast}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "rack-musicindex"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rack::Musicindex::VERSION
17
+
18
+ gem.add_dependency('builder')
19
+ gem.add_development_dependency('rspec', ['~> 2.8.0'])
20
+ gem.add_development_dependency('rake')
21
+ gem.add_development_dependency('sinatra')
22
+ gem.add_development_dependency('rack-test')
23
+ gem.add_development_dependency('nokogiri')
24
+ end
Binary file
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/../test_app')
3
+
4
+ set :environment, :test
5
+
6
+ require 'nokogiri'
7
+
8
+ describe Rack::MusicIndex do
9
+ def app
10
+ Sinatra::Application
11
+ end
12
+
13
+ it 'should index mp3s as podcast' do
14
+ get '/foo'
15
+
16
+ last_response.should be_ok
17
+
18
+ xml = Nokogiri::XML(last_response.body)
19
+
20
+ channel = xml.xpath('//channel')[0]
21
+ items = xml.xpath('//item')
22
+ item = items[0]
23
+
24
+ channel.xpath('title')[0].content.should eql('/foo')
25
+ channel.xpath('description')[0].content.should eql('Generated by Rack::MusicIndex')
26
+
27
+ items.size.should eql(1)
28
+ item.xpath('title')[0].content.should eql('test.mp3')
29
+ item.xpath('link')[0].content.should eql('http://example.org/foo/test.mp3')
30
+ item.xpath('guid')[0].content.should eql('http://example.org/foo/test.mp3')
31
+ item.xpath('enclosure')[0]['url'].should eql('http://example.org/foo/test.mp3')
32
+ end
33
+
34
+ it 'should return mp3' do
35
+ get '/foo/test.mp3'
36
+
37
+ last_response.should be_ok
38
+ last_response['Content-Type'].should eql('audio/mpeg')
39
+ last_response.body.should eql(open(fixture('/foo/test.mp3')).read)
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'rack/test'
9
+
10
+ RSpec.configure do |config|
11
+ def fixture(filename)
12
+ File.dirname(__FILE__) + '/fixtures/' + filename
13
+ end
14
+
15
+ config.treat_symbols_as_metadata_keys_with_true_values = true
16
+ config.run_all_when_everything_filtered = true
17
+ config.filter_run :focus
18
+ config.include Rack::Test::Methods
19
+ end
data/spec/test_app.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'sinatra'
2
+ require 'rack-musicindex'
3
+
4
+ use Rack::MusicIndex, {
5
+ '/foo' => File.expand_path(File.dirname(__FILE__) + '/fixtures/foo')
6
+ }
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-musicindex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - youpy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: builder
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sinatra
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: nokogiri
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: A Rack middleware to publish directries containing media files as podcast
111
+ email:
112
+ - youpy@buycheapviagraonlinenow.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - lib/rack-musicindex.rb
124
+ - lib/rack/musicindex.rb
125
+ - lib/rack/musicindex/version.rb
126
+ - rack-musicindex.gemspec
127
+ - spec/fixtures/foo/test.mp3
128
+ - spec/rack/musicindex_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/test_app.rb
131
+ homepage: ''
132
+ licenses: []
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: -4485852318847294680
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ segments:
153
+ - 0
154
+ hash: -4485852318847294680
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.24
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: A Rack middleware to publish directries containing media files as podcast
161
+ test_files:
162
+ - spec/fixtures/foo/test.mp3
163
+ - spec/rack/musicindex_spec.rb
164
+ - spec/spec_helper.rb
165
+ - spec/test_app.rb