ferver 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88fa261cbca7d3076a07ff0531243891902f7b13
4
- data.tar.gz: 2ba5cbe2dd11de73bcc3076e878529fed5c1e685
3
+ metadata.gz: 8d2f0cc442e5c638817dbd7465cac6033cbafa38
4
+ data.tar.gz: 3344fc4b12b37de5ec6e08da1694c093172a6bbe
5
5
  SHA512:
6
- metadata.gz: 66bb027b877ae36dbdcc68a53f75558480dafd2af82382ab53b47a6bdd856189b1b93ae48d95608defcf94beb289270b2094c54d3f78b1b975a78df91570d3c0
7
- data.tar.gz: a1c85bf0783ffd26e95b69a5b40b038f34aa705f2b1432f86acc15b5688479c34f671121e26e4948ba6e64da686b7ff760bacb4d77099acc9dd353b8dc6eb516
6
+ metadata.gz: 98f244dec47ab5c2dd7cd9d1ac687115763a63872622d03dd602d002125d38169210af42d66ad1eb3b8bd53d0edb72e068cf2cd7a7363debd62be2f57ce7f1b1
7
+ data.tar.gz: a111d09816eefba4e1da78e7e5af7d7172d6526d5cb14988dca5b4d6d8d4f27c54a4005963b937ec4d10bead859cc9c536e2f20015342fc55f10b3fa7cf0c11a
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.1.1
@@ -2,4 +2,9 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
- - 2.1.0
5
+ - 2.1.0
6
+ - ruby-head
7
+
8
+ addons:
9
+ code_climate:
10
+ repo_token: aaa2bcd71ac641474a13caabe294beeb26abcc7c4dd82f3395a7b9193bb20d02
data/README.md CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  #### Ferver: A simple web app to serve files over HTTP packaged as a Ruby gem.
4
4
 
5
- [![Build Status](https://travis-ci.org/rob-murray/ferver.png?branch=master)](https://travis-ci.org/rob-murray/ferver)
6
- [![Code Climate](https://codeclimate.com/github/rob-murray/ferver.png)](https://codeclimate.com/github/rob-murray/ferver)
7
- [![Coverage Status](https://coveralls.io/repos/rob-murray/ferver/badge.png)](https://coveralls.io/r/rob-murray/ferver)
8
- [![Dependency Status](https://gemnasium.com/rob-murray/ferver.png)](https://gemnasium.com/rob-murray/ferver)
9
- [![Gem Version](https://badge.fury.io/rb/ferver.png)](http://badge.fury.io/rb/ferver)
5
+ [![Build Status](https://travis-ci.org/rob-murray/ferver.svg?branch=master)](https://travis-ci.org/rob-murray/ferver)
6
+ [![Code Climate](https://codeclimate.com/github/rob-murray/ferver.png)](https://codeclimate.com/github/rob-murray/ferver)
7
+ [![Coverage Status](https://coveralls.io/repos/rob-murray/ferver/badge.png)](https://coveralls.io/r/rob-murray/ferver)
8
+ [![Dependency Status](https://gemnasium.com/rob-murray/ferver.svg)](https://gemnasium.com/rob-murray/ferver)
9
+ [![Gem Version](https://badge.fury.io/rb/ferver.svg)](http://badge.fury.io/rb/ferver)
10
10
 
11
11
  ### Description
12
12
 
@@ -31,7 +31,7 @@ $ gem install ferver
31
31
 
32
32
  ### Usage
33
33
 
34
- You can run **ferver** from any directory, just pass in the directory you want to serve files from or if leave blank to use the current directory.
34
+ You can run **ferver** from any directory, just pass in the directory you want to serve files from as a command line argument or leave blank to use the current directory.
35
35
 
36
36
  ##### Use the current directory
37
37
 
@@ -44,7 +44,15 @@ $ ferver
44
44
  For exmple, to serve files from **/Users/rob/Projects/ferver/** directory pass the path in as below;
45
45
 
46
46
  ```bash
47
- $ ferver /Users/rob/Projects/ferver/
47
+ $ ferver -d /Users/rob/Projects/ferver/
48
+ ````
49
+
50
+ ##### Command line help
51
+
52
+ For a list of arguments just use the `-h` switch.
53
+
54
+ ```bash
55
+ $ ferver -h
48
56
  ````
49
57
 
50
58
  ### Accessing files
@@ -75,14 +83,14 @@ For example to download file appearing third in the list displayed earlier, requ
75
83
 
76
84
  Please use the GitHub pull-request mechanism to submit contributions.
77
85
 
78
- After cloning the repo, you can test the application without having to install the gem package by running the `server` Rake task;
86
+ After cloning the repo, you can run the web application without having to install the gem package by running a Rake task named `server`, or it's alias `s`.
79
87
 
80
88
  ```bash
81
- $ rake server
89
+ $ rake s
82
90
 
83
91
  # or
84
92
 
85
- $ rake server /path/to/dir
93
+ $ rake s /path/to/dir
86
94
  ```
87
95
 
88
96
  ### License
data/Rakefile CHANGED
@@ -1,25 +1,27 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core'
3
3
  require 'rspec/core/rake_task'
4
-
4
+
5
5
  task :default => :spec
6
-
6
+ task :s => :server
7
+
7
8
  desc "Run all specs in spec directory (excluding plugin specs)"
8
9
  RSpec::Core::RakeTask.new(:spec)
9
10
 
10
- desc "Run ferver locally from source"
11
- task :server do |t, args|
12
-
13
- require 'rubygems'
14
- require 'bundler'
15
- Bundler.setup
16
- require 'ferver'
17
-
18
- # use the last argument (first is the rake task) as the file path to serve from
19
- ferver_path = (ARGV.length == 2 && ARGV.last) || nil
20
- Ferver::App.set :ferver_path, ferver_path
11
+ desc 'Run ferver locally from source'
12
+ task :server do
13
+ require 'rubygems'
14
+ require 'bundler'
15
+ Bundler.setup
16
+ require 'ferver'
21
17
 
22
- # run!
23
- Ferver::App.run!
18
+ # use the last argument (first is the rake task) as the file path to serve from
19
+ ferver_path = (ARGV.length == 2 && ARGV.last) || nil
20
+ Ferver.configure do |config|
21
+ config.directory_path = ferver_path
22
+ end
23
+ Ferver::App.set :raise_errors, false
24
24
 
25
- end
25
+ # run!
26
+ Ferver::App.run!
27
+ end
data/bin/ferver CHANGED
@@ -1,13 +1,28 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'optparse'
3
4
  require 'sinatra'
4
5
  require 'ferver'
5
6
 
7
+ options = {}
8
+ OptionParser.new do |opts|
9
+ opts.banner = 'Ferver: A simple web app to serve files over HTTP. Version: ' + Ferver::VERSION
10
+ opts.separator ''
6
11
 
7
- # use the first argument as the file path to serve from
8
- ferver_path = (ARGV.length == 1 && ARGV[0]) || nil
9
- Ferver::App.set :ferver_path, ferver_path
12
+ opts.on('-d', '--directory [DIRECTORY]', 'Specify the path to the directory to serve files from [optional]') do |directory|
13
+ options[:directory] = directory
14
+ end
15
+
16
+ opts.on("-h", "--help", "Displays help") do
17
+ puts opts
18
+ exit
19
+ end
20
+ end.parse!
21
+
22
+ Ferver.configure do |config|
23
+ config.directory_path = options[:directory]
24
+ end
10
25
  Ferver::App.set :environment, :production
11
26
 
12
27
  # run!
13
- Ferver::App.run!
28
+ Ferver::App.run!
@@ -4,29 +4,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'ferver/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "ferver"
7
+ spec.name = 'ferver'
8
8
  spec.version = Ferver::VERSION
9
- spec.authors = ["Rob Murray"]
10
- spec.email = ["robmurray17@gmail.com"]
11
- spec.summary = %q{A simple web app to serve files over HTTP.}
12
- spec.description = %q{Ferver is a super, simple ruby gem to serve files over http; useful as a basic file server to quickly share files on your local network.}
13
- spec.homepage = "https://github.com/rob-murray/ferver"
14
- spec.license = "MIT"
15
-
9
+ spec.authors = ['Rob Murray']
10
+ spec.email = ['robmurray17@gmail.com']
11
+ spec.summary = %q(A simple web app to serve files over HTTP.)
12
+ spec.description = %q(Ferver is a super, simple ruby gem to serve files over http; useful as a basic file server to quickly share files on your local network.)
13
+ spec.homepage = 'https://github.com/rob-murray/ferver'
14
+ spec.license = 'MIT'
15
+
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.require_paths = ["lib"]
18
+ spec.require_paths = ['lib']
19
19
 
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
-
22
- spec.add_development_dependency "bundler", "~> 1.5"
23
- spec.add_development_dependency "rake", "~> 0"
24
- spec.add_development_dependency "rspec", "~> 2.14.1"
25
- spec.add_development_dependency "rack-test", "~> 0.6.2"
26
- spec.add_development_dependency "mocha", "~> 1.0.0"
27
- spec.add_development_dependency "spork", "~> 0.9.2"
28
- spec.add_development_dependency "webrat", "~> 0.7.3"
29
- spec.add_development_dependency "coveralls", "~> 0.7.0"
30
21
 
31
- spec.add_dependency "sinatra", "~> 1.4.4"
22
+ spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'rake', '~> 0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'rack-test', '~> 0.6'
26
+ spec.add_development_dependency 'spork', '~> 0.9'
27
+ spec.add_development_dependency 'rspec-html-matchers', '~> 0.6'
28
+ spec.add_development_dependency 'coveralls', '~> 0.7'
29
+ spec.add_development_dependency 'codeclimate-test-reporter'
30
+
31
+ spec.add_dependency 'sinatra', '~> 1.4'
32
32
  end
@@ -1,7 +1,29 @@
1
- require "ferver/version"
1
+ require_relative 'ferver/app'
2
+ require_relative 'ferver/configuration'
3
+ require_relative 'ferver/directory_not_found_error'
4
+ require_relative 'ferver/file_id_request'
5
+ require_relative 'ferver/file_list'
6
+ require_relative 'ferver/version'
2
7
 
3
8
  module Ferver
4
9
  autoload :App, 'ferver/app'
5
10
 
6
- class DirectoryNotFoundError < StandardError; end
11
+ # By default, serve files from current location when the gem is called.
12
+ DEFAULT_FILE_SERVER_DIR_PATH = './'
13
+
14
+ class << self
15
+ attr_accessor :configuration
16
+ end
17
+
18
+ def self.configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def self.reset
23
+ @configuration = Configuration.new
24
+ end
25
+
26
+ def self.configure
27
+ yield(configuration)
28
+ end
7
29
  end
@@ -1,126 +1,67 @@
1
- require "sinatra"
2
- require "json"
3
- require "sinatra/base"
4
- require "ferver/file_list"
5
- require "ferver/file_id_request"
1
+ require 'sinatra'
2
+ require 'sinatra/base'
3
+ require 'json'
4
+ require_relative './directory_not_found_error'
5
+ require_relative './configuration'
6
6
 
7
7
  module Ferver
8
8
  class App < Sinatra::Base
9
-
10
- # Config
11
- set :inline_templates, true
12
9
  set :app_file, __FILE__
13
10
 
14
- # By default, serve files from current location when the gem
15
- # binary is called.
16
- DEFAULT_FILE_SERVER_DIR_PATH = "./"
11
+ before do
12
+ @ferver_list = FileList.new(current_ferver_path)
13
+ end
17
14
 
18
- # redirect to file list
19
- # /
20
- get "/" do
21
- redirect to("/files")
15
+ error Ferver::DirectoryNotFoundError do
16
+ halt 500, "Ferver: Directory '#{current_ferver_path}' not found."
22
17
  end
23
18
 
24
- # list files; repond as html or json
25
- # /files
26
- get "/files" do
27
- file_list = @ferver_list.files
19
+ # redirect to file list
20
+ get '/' do
21
+ redirect to('/files')
22
+ end
28
23
 
29
- if request.preferred_type.to_s == "application/json"
24
+ # list files
25
+ get '/files' do
26
+ if request.preferred_type.to_s == 'application/json'
30
27
  content_type :json
31
28
 
32
- file_list.to_json
29
+ ferver_list.all.to_json
33
30
  else
34
- @file_count = @ferver_list.file_count
35
- @ferver_path = File.expand_path(get_current_ferver_path)
36
- @file_list = file_list
37
-
38
- erb :file_list_view
31
+ erb :index, locals: { file_list: ferver_list.all,
32
+ ferver_path: File.expand_path(current_ferver_path),
33
+ file_count: ferver_list.size }
39
34
  end
40
-
41
35
  end
42
36
 
43
37
  # download file
44
- # /files/:id
45
- get "/files/:id" do
46
- id_request = Ferver::FileIdRequest.new(params[:id])
38
+ get '/files/:id' do
39
+ halt(400, 'Bad request') unless valid_file_request?
47
40
 
48
- halt(400, "Bad request") unless id_request.valid?
49
-
50
- if @ferver_list.file_id_is_valid?(id_request.value)
51
- file_name = @ferver_list.file_by_id(id_request.value)
52
- file = FileList.path_for_file(get_current_ferver_path, file_name)
41
+ if ferver_list.file_id_is_valid?(file_id_request.value)
42
+ file_name = ferver_list.file_by_id(file_id_request.value)
43
+ file = FileList.path_for_file(current_ferver_path, file_name)
53
44
 
54
- send_file(file, :disposition => 'attachment', :filename => File.basename(file))
45
+ send_file(file, disposition: 'attachment', filename: File.basename(file))
55
46
  else
56
47
  status 404
57
48
  end
58
49
  end
59
50
 
60
-
61
- # Find all files in `Ferver` directory.
62
- # !Called before each request
63
- #
64
- before do
65
- begin
66
- @ferver_list = FileList.new(get_current_ferver_path)
67
- rescue DirectoryNotFoundError
68
- halt(500, "Ferver: Directory '#{get_current_ferver_path}' not found.")
69
- end
70
-
71
- end
72
-
73
51
  private
74
52
 
75
- # Return the absolute path to the directory Ferver is serving files from.
76
- # This can be specified in Sinatra configuration;
77
- # i.e. `Ferver::App.set :ferver_path, ferver_path` or the default if nil
78
- #
79
- def get_current_ferver_path
80
- @current_ferver_path ||= begin
81
- path = nil
53
+ attr_reader :ferver_list
82
54
 
83
- if settings.respond_to?(:ferver_path) and settings.ferver_path
84
- path = settings.ferver_path
85
- else
86
- path = DEFAULT_FILE_SERVER_DIR_PATH
87
- end
88
- end
89
- end
55
+ def file_id_request
56
+ @file_id_request ||= FileIdRequest.new(params[:id])
57
+ end
90
58
 
59
+ def valid_file_request?
60
+ file_id_request.valid?
61
+ end
62
+
63
+ def current_ferver_path
64
+ Ferver.configuration.directory_path
65
+ end
91
66
  end
92
67
  end
93
-
94
-
95
-
96
- __END__
97
-
98
- @@file_list_view
99
- <!DOCTYPE html>
100
- <html lang="en">
101
- <head>
102
- <meta charset="utf-8">
103
- <title>File List &middot; Ferver</title>
104
- </head>
105
- <body>
106
- <h3>Files served:</h3>
107
- <ul>
108
- <% @file_list.each_with_index do |file_name, index| %>
109
-
110
- <li><a href="/files/<%= index %>"><%= file_name %></a></li>
111
-
112
- <% end %>
113
-
114
- </ul>
115
-
116
- <p><%= @file_count %> files served from: <%= @ferver_path %></p>
117
-
118
- <hr>
119
-
120
- <p>Served by <a href="https://github.com/rob-murray/ferver" title="Ferver: A simple Ruby app serving files over HTTP">Ferver</a> gem v<%= Ferver::VERSION %></p>
121
-
122
- </body>
123
- </html>
124
-
125
- <html>
126
- <body>
@@ -0,0 +1,11 @@
1
+ module Ferver
2
+ class Configuration
3
+ attr_accessor :directory_path
4
+
5
+ # Return the absolute path to the directory Ferver is serving files from.
6
+ #
7
+ def directory_path
8
+ @directory_path || Ferver::DEFAULT_FILE_SERVER_DIR_PATH
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module Ferver
3
+ class DirectoryNotFoundError < StandardError; end
4
+ end
@@ -1,28 +1,37 @@
1
1
 
2
2
  module Ferver
3
- class FileIdRequest
3
+ class FileIdRequest
4
+ attr_reader :value
4
5
 
5
- attr_reader :value
6
+ def initialize(value = nil)
7
+ @is_valid = false
6
8
 
7
- def initialize(value = nil)
8
- @is_valid = false
9
+ self.value = value
10
+ end
11
+
12
+ def value=(value)
13
+ id = parse_value(value)
9
14
 
10
- self.value = value
11
- end
15
+ if id.nil?
16
+ @is_valid = false
17
+ else
18
+ @value = id
19
+ @is_valid = true
20
+ end
21
+ end
12
22
 
13
- def value=(value)
14
- id = Integer(value) rescue nil
23
+ def valid?
24
+ @is_valid
25
+ end
15
26
 
16
- if id.nil?
17
- @is_valid = false
18
- else
19
- @value = id
20
- @is_valid = true
21
- end
22
- end
27
+ private
23
28
 
24
- def valid?
25
- @is_valid
26
- end
29
+ def parse_value(value)
30
+ begin
31
+ Integer(value)
32
+ rescue
33
+ nil
34
+ end
27
35
  end
28
- end
36
+ end
37
+ end