ferver 1.3.0 → 1.3.1

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: 53cab51e0ed1cb913ac92f9a19e927f9287115c4
4
- data.tar.gz: 2be0b531e9d3b69bca13fbda397f84851fcbc414
3
+ metadata.gz: afd7d5747292d7103cf9d23366c7c3873f3f6191
4
+ data.tar.gz: c3c46b4c00c3d4de4cfdda39e2872ea2dea74edd
5
5
  SHA512:
6
- metadata.gz: 0768729a9b85bb19183db20980214eeb22499d62a0fe23739dee08d9b635ff0eda80b937d09ee8e8801dadcefc041e1c07fbd7d3ff4087603af1e8f4d504aa43
7
- data.tar.gz: 23c93f96598a6397cdc0e073acb16f58bd3da509be09a806638895d256c3d81b742e219822de5aa2897156906996b3ea768ed52af8fe6708c01c83528f69c50f
6
+ metadata.gz: 02dd8ddfa1e9816e330dabdad1da70301a743589c8154a709122174ec5ec941c0acb9ec9bc34021b19a359253a1e496661894e2663de149e74171476fc70cb37
7
+ data.tar.gz: 601fa4f7a2e4c2b74cae5114d1c98662136e1dc4e2406cbe135648d693832991c690ce980661b77dfaedc1ffa9fe9690ef4c8611ead0748af55163737bb24d9e
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .byebug_history
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --colour
2
- --format progress
2
+ --format progress
3
+ --require spec_helper
@@ -0,0 +1,13 @@
1
+ LineLength:
2
+ Enabled: false
3
+
4
+ StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+ Enabled: true
7
+
8
+ Documentation:
9
+ Enabled: false
10
+
11
+ AllCops:
12
+ Exclude:
13
+ - "spec/spec_helper.rb"
@@ -1 +1 @@
1
- 2.2.0
1
+ 2.4.0
@@ -1,12 +1,17 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  rvm:
4
- - 1.9.3
5
4
  - 2.0.0
6
5
  - 2.1.0
7
6
  - 2.2.0
7
+ - 2.3.0
8
+ - 2.4.0
8
9
  - ruby-head
9
10
 
11
+ env:
12
+ global:
13
+ - NOKOGIRI_USE_SYSTEM_LIBRARIES=true
14
+
10
15
  addons:
11
16
  code_climate:
12
17
  repo_token: aaa2bcd71ac641474a13caabe294beeb26abcc7c4dd82f3395a7b9193bb20d02
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
2
3
 
3
4
  # Specify your gem's dependencies in ferver.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![Build Status](https://travis-ci.org/rob-murray/ferver.svg?branch=master)](https://travis-ci.org/rob-murray/ferver)
6
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)
7
+ [![Coverage Status](https://coveralls.io/repos/rob-murray/ferver/badge.svg?branch=master&service=github)](https://coveralls.io/github/rob-murray/ferver?branch=master)
8
8
  [![Dependency Status](https://gemnasium.com/rob-murray/ferver.svg)](https://gemnasium.com/rob-murray/ferver)
9
9
  [![Gem Version](https://badge.fury.io/rb/ferver.svg)](http://badge.fury.io/rb/ferver)
10
10
 
@@ -58,6 +58,14 @@ $ ferver -a
58
58
 
59
59
  > Note that zero size files will always be hidden.
60
60
 
61
+ ##### Configure webserver
62
+
63
+ If required, you can configure the bind address or port number used by the webserver. By default this is `0.0.0.0` and port `4567` which means the server is accessible from outside your machine (if firewall permits). For example if you used the configuration below then it would only be accessible from local machine and on port `9999`.
64
+
65
+ ```bash
66
+ $ ferver -p 9999 -b 127.0.0.1
67
+ ````
68
+
61
69
  ##### Command line help
62
70
 
63
71
  For a list of arguments just use the `--help` switch.
@@ -70,6 +78,8 @@ $ ferver -h
70
78
 
71
79
  The **ferver** gem uses [Sinatra](http://www.sinatrarb.com/) and runs on default port configuration so just point your browser to `http://localhost:4567` to list the files.
72
80
 
81
+ > If you are unable to connect then please check any firewall settings or the configured bind address or port number!
82
+
73
83
  #### HTML
74
84
 
75
85
  List available files in your browser.
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "bundler/gem_tasks"
2
3
  require "rspec/core"
3
4
  require "rspec/core/rake_task"
data/bin/ferver CHANGED
@@ -1,22 +1,31 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require 'optparse'
4
- $:.unshift File.join( File.dirname(__FILE__), "/../lib")
5
- require 'ferver'
4
+ require "optparse"
5
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "/../lib")
6
+ require "ferver"
6
7
 
7
8
  options = {}
8
9
  OptionParser.new do |opts|
9
- opts.banner = 'Ferver: A simple web app to serve files over HTTP. Version: ' + Ferver::VERSION
10
- opts.separator ''
10
+ opts.banner = "Ferver: A simple web app to serve files over HTTP. Version: " + Ferver::VERSION
11
+ opts.separator ""
11
12
 
12
- opts.on('-a', '--all', 'Serve hidden files') do |a|
13
+ opts.on("-a", "--all", "Serve hidden files. False by default [optional]") do |a|
13
14
  options[:hidden] = a
14
15
  end
15
16
 
16
- opts.on('-d', '--directory [DIRECTORY]', 'Specify the path to the directory to serve files from [optional]') do |directory|
17
+ opts.on("-d", "--directory [DIRECTORY]", "Specify the path to the directory to serve files from [optional]") do |directory|
17
18
  options[:directory] = directory
18
19
  end
19
20
 
21
+ opts.on("-b", "--bind [IP or hostname]", "Set the hostname or IP address of the interface to listen on when running. Defaults to '0.0.0.0' [optional]") do |bind_address|
22
+ options[:bind_address] = bind_address
23
+ end
24
+
25
+ opts.on("-p", "--port [PORT]", "Set the port used by Ferver server. Defaults to 4567 [optional]") do |port|
26
+ options[:port] = port
27
+ end
28
+
20
29
  opts.on("-h", "--help", "Displays help") do
21
30
  puts opts
22
31
  exit
@@ -24,10 +33,12 @@ OptionParser.new do |opts|
24
33
  end.parse!
25
34
 
26
35
  Ferver.configure do |config|
27
- config.serve_hidden = options[:hidden]
28
- config.directory_path = options[:directory]
36
+ config.serve_hidden = options[:hidden] if options[:hidden]
37
+ config.directory_path = options[:directory] if options[:directory]
29
38
  end
30
39
  Ferver::App.set :environment, :production
40
+ Ferver::App.set :bind, options[:bind_address] if options[:bind_address]
41
+ Ferver::App.set :port, options[:port] if options[:port]
31
42
 
32
43
  # run!
33
44
  Ferver::App.run!
@@ -1,33 +1,35 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'ferver/version'
5
+ require "ferver/version"
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = 'ferver'
8
+ spec.name = "ferver"
8
9
  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
- spec.required_ruby_version = '>= 1.9.3'
10
+ spec.authors = ["Rob Murray"]
11
+ spec.email = ["robmurray17@gmail.com"]
12
+ spec.summary = "A simple web app to serve files over HTTP."
13
+ spec.description = "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."
14
+ spec.homepage = "https://github.com/rob-murray/ferver"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 2.0.0"
16
17
 
17
18
  spec.files = `git ls-files -z`.split("\x0")
18
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- spec.require_paths = ['lib']
20
+ spec.require_paths = ["lib"]
20
21
 
21
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
23
 
23
- spec.add_development_dependency 'bundler', '~> 1.5'
24
- spec.add_development_dependency 'rake', '~> 0'
25
- spec.add_development_dependency 'rspec', '~> 3.0'
26
- spec.add_development_dependency 'rack-test', '~> 0.6'
27
- spec.add_development_dependency 'spork', '~> 0.9'
28
- spec.add_development_dependency 'rspec-html-matchers', '~> 0.6'
29
- spec.add_development_dependency 'coveralls', '~> 0.7'
30
- spec.add_development_dependency 'codeclimate-test-reporter'
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "nokogiri", "~> 1.6.0"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "rack-test", "~> 0.6"
29
+ spec.add_development_dependency "rspec-html-matchers", "~> 0.6"
30
+ spec.add_development_dependency "coveralls", "~> 0.7"
31
+ spec.add_development_dependency "codeclimate-test-reporter"
32
+ spec.add_development_dependency "byebug"
31
33
 
32
- spec.add_dependency 'sinatra', '~> 1.4'
34
+ spec.add_dependency "sinatra", "~> 1.4"
33
35
  end
@@ -1,16 +1,14 @@
1
- require_relative 'ferver/app'
2
- require_relative 'ferver/controller'
3
- require_relative 'ferver/configuration'
4
- require_relative 'ferver/directory_not_found_error'
5
- require_relative 'ferver/file_id_request'
6
- require_relative 'ferver/file_list'
7
- require_relative 'ferver/found_file'
8
- require_relative 'ferver/version'
1
+ # frozen_string_literal: true
2
+ require_relative "ferver/configuration"
3
+ require_relative "ferver/errors"
4
+ require_relative "ferver/app"
5
+ require_relative "ferver/controller"
6
+ require_relative "ferver/ferver_directory"
7
+ require_relative "ferver/file_list"
8
+ require_relative "ferver/found_file"
9
+ require_relative "ferver/version"
9
10
 
10
11
  module Ferver
11
- # By default, serve files from current location when the gem is called.
12
- DEFAULT_FILE_SERVER_DIR_PATH = './'
13
-
14
12
  class << self
15
13
  attr_accessor :configuration
16
14
 
@@ -1,13 +1,14 @@
1
- require 'sinatra'
2
- require 'sinatra/base'
3
- require_relative './controller'
1
+ # frozen_string_literal: true
2
+ require "sinatra"
3
+ require "sinatra/base"
4
+ require_relative "./controller"
4
5
 
5
6
  module Ferver
6
7
  class App < Sinatra::Application
7
8
  set :app_file, __FILE__
8
9
 
9
10
  configure do
10
- set :views, './views'
11
+ set :views, "./views"
11
12
  end
12
13
 
13
14
  use Controller
@@ -1,18 +1,27 @@
1
+ # frozen_string_literal: true
1
2
  module Ferver
2
3
  class Configuration
3
- attr_accessor :directory_path, :serve_hidden
4
+ # By default, serve files from current location when the gem is called.
5
+ DEFAULT_FILE_SERVER_DIR_PATH = "./".freeze
4
6
 
5
7
  # 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
8
+ attr_accessor :directory_path
9
+
10
+ # Default to not serving hidden files.
11
+ attr_accessor :serve_hidden
12
+
13
+ # For testing
14
+ attr_writer :directory
15
+
16
+ alias serve_hidden? serve_hidden
17
+
18
+ def initialize(directory_path = DEFAULT_FILE_SERVER_DIR_PATH, serve_hidden = false)
19
+ @directory_path = directory_path
20
+ @serve_hidden = serve_hidden
9
21
  end
10
22
 
11
- # Default to not serving hidden files
12
- #
13
- def serve_hidden
14
- @serve_hidden || false
23
+ def directory
24
+ @directory ||= FerverDirectory.new(self)
15
25
  end
16
- alias_method :serve_hidden?, :serve_hidden
17
26
  end
18
27
  end
@@ -1,19 +1,20 @@
1
- require 'sinatra'
2
- require 'sinatra/base'
3
- require 'json'
4
- require_relative './directory_not_found_error'
5
- require_relative './configuration'
1
+ # frozen_string_literal: true
2
+ require "sinatra"
3
+ require "sinatra/base"
4
+ require "json"
6
5
 
7
6
  module Ferver
8
7
  class Controller < Sinatra::Base
9
8
  before do
10
- @ferver_list = FileList.new(current_ferver_path)
9
+ @ferver_list = FileList.new(Ferver.configuration.directory.found_files)
11
10
  end
12
11
 
13
- before '/files/:id' do
14
- halt(400, 'Bad request') unless valid_file_request?
15
-
16
- find_file!
12
+ before "/files/:id" do
13
+ begin
14
+ @file = find_file! Integer(params[:id])
15
+ rescue ArgumentError
16
+ halt 400, "Bad request"
17
+ end
17
18
  end
18
19
 
19
20
  error Ferver::DirectoryNotFoundError do
@@ -21,13 +22,13 @@ module Ferver
21
22
  end
22
23
 
23
24
  # redirect to file list
24
- get '/' do
25
- redirect to('/files')
25
+ get "/" do
26
+ redirect to("/files")
26
27
  end
27
28
 
28
29
  # list files
29
- get '/files' do
30
- if request.preferred_type.to_s == 'application/json'
30
+ get "/files" do
31
+ if json_request?
31
32
  content_type :json
32
33
 
33
34
  ferver_list.map(&:name).to_json
@@ -39,36 +40,30 @@ module Ferver
39
40
  end
40
41
 
41
42
  # download file
42
- get '/files/:id' do
43
- send_file(
44
- @file.path_to_file, disposition: 'attachment', filename: @file.name
45
- )
43
+ get "/files/:id" do
44
+ send_file @file.path_to_file, disposition: String.new("attachment"), filename: @file.name
46
45
  end
47
46
 
48
47
  private
49
48
 
50
49
  attr_reader :ferver_list
51
50
 
52
- def file_id_request
53
- @file_id_request ||= FileIdRequest.new(params[:id])
54
- end
55
-
56
- def valid_file_request?
57
- file_id_request.valid?
51
+ def find_file!(file_id)
52
+ ferver_list.file_by_id(file_id)
53
+ rescue Ferver::FileNotFoundError => error
54
+ halt 404, error.message
58
55
  end
59
56
 
60
- def find_file!
61
- @file = ferver_list.file_by_id(file_id_request.value)
62
- rescue IndexError
63
- halt 404, 'File requested not found.'
57
+ def json_request?
58
+ request.preferred_type.to_s == "application/json"
64
59
  end
65
60
 
66
61
  def current_ferver_path
67
- Ferver.configuration.directory_path
62
+ Ferver.configuration.directory.path
68
63
  end
69
64
 
70
65
  def current_full_path
71
- File.expand_path(current_ferver_path)
66
+ Ferver.configuration.directory.full_path
72
67
  end
73
68
  end
74
69
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Ferver
3
+ DirectoryNotFoundError = Class.new(StandardError)
4
+ FileNotFoundError = Class.new(StandardError)
5
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ module Ferver
3
+ # A wrapper around the directory specified by config
4
+ class FerverDirectory
5
+ def initialize(configuration)
6
+ path = configuration.directory_path
7
+ raise ArgumentError, "No path is specified" if path.empty?
8
+ raise DirectoryNotFoundError unless Dir.exist?(path)
9
+
10
+ @configuration = configuration
11
+ end
12
+
13
+ def path
14
+ configuration.directory_path
15
+ end
16
+
17
+ def full_path
18
+ @_full_path ||= File.expand_path(path)
19
+ end
20
+
21
+ def found_files
22
+ find_files
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :configuration
28
+
29
+ # Iterate through files in specified dir for valid files
30
+ #
31
+ def find_files
32
+ [].tap do |results|
33
+ Dir.foreach(full_path) do |file_name|
34
+ next if file_name == "." || file_name == ".."
35
+ next if file_name =~ /^\./ && !configuration.serve_hidden?
36
+
37
+ found_file = FoundFile.new(full_path, file_name)
38
+ results << found_file if found_file.valid?
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end