From 23f18b41556e6f66f1ad856c019de07be5a3fd59 Mon Sep 17 00:00:00 2001 From: Grayson Wright Date: Sun, 13 Jan 2013 01:52:30 -0500 Subject: [PATCH 1/3] Moved option parsing into lib/annyong/application.rb --- bin/annyong | 28 +++----------------- lib/annyong.rb | 1 + lib/annyong/application.rb | 54 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 lib/annyong/application.rb diff --git a/bin/annyong b/bin/annyong index 498868a..54193b2 100755 --- a/bin/annyong +++ b/bin/annyong @@ -8,33 +8,11 @@ require "optparse" require "rack" require "annyong" -options = { - :port => 9292, - :host => "0.0.0.0" -} -opts = OptionParser.new do |opts| - opts.banner = "Usage: annyong [options] - -Options: - " - opts.on("--port [PORT]", "The port to use (default: 9292)") do |port| - options[:port] = port - end - - opts.on("--host [HOST]", "The host to listen on (default: 0.0.0.0)") do |host| - options[:host] = host - end - - #opts.on("--path [PATH]", "The directory to serve") do |path| - #options[:path] = path - #end - -end -opts.parse! +application = Annyong::Application.new(ARGV) Rack::Server.start({ :config => File.join(File.dirname(__FILE__), "../assets/rack/config.ru"), - :Port => options[:port], - :Host => options[:host], + :Port => application.port, + :Host => application.host, :AccessLog => [], }) diff --git a/lib/annyong.rb b/lib/annyong.rb index 9d36ad1..380281d 100644 --- a/lib/annyong.rb +++ b/lib/annyong.rb @@ -1,6 +1,7 @@ module Annyong VERSION = '0.3' autoload :Directory, "annyong/directory" + require "annyong/application" end unless "".respond_to?(:each) diff --git a/lib/annyong/application.rb b/lib/annyong/application.rb new file mode 100644 index 0000000..a31fb34 --- /dev/null +++ b/lib/annyong/application.rb @@ -0,0 +1,54 @@ +require 'ostruct' + +module Annyong + + class Application + + def port + options[:port] + end + + def host + options[:host] + end + + def initialize(args) + @options = parse_options(args) + end + + def options + @options ||= parse_options + end + + private + + def parse_options(args) + options = { + :port => 9292, + :host => "0.0.0.0" + } + + option_parser = OptionParser.new do |opts| + opts.banner = "Usage: annyong [options] + +Options: + " + opts.on("--port [PORT]", "The port to use (default: #{options[:port]})") do |port| + options[:port] = port + end + + opts.on("--host [HOST]", "The host to listen on (default: #{options[:host]})") do |host| + options[:host] = host + end + + #opts.on("--path [PATH]", "The directory to serve") do |path| + #options[:path] = path + #end + end + + option_parser.parse!(args) + options + end + + end +end From 3327606477649d2c591c0cefe7b9e77c5a90e553 Mon Sep 17 00:00:00 2001 From: Grayson Wright Date: Sun, 13 Jan 2013 02:06:49 -0500 Subject: [PATCH 2/3] Moved initialization of server into Annyong::Application --- bin/annyong | 8 +------- lib/annyong/application.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bin/annyong b/bin/annyong index 54193b2..ad8d03f 100755 --- a/bin/annyong +++ b/bin/annyong @@ -9,10 +9,4 @@ require "rack" require "annyong" application = Annyong::Application.new(ARGV) - -Rack::Server.start({ - :config => File.join(File.dirname(__FILE__), "../assets/rack/config.ru"), - :Port => application.port, - :Host => application.host, - :AccessLog => [], -}) +application.run diff --git a/lib/annyong/application.rb b/lib/annyong/application.rb index a31fb34..67d7e2b 100644 --- a/lib/annyong/application.rb +++ b/lib/annyong/application.rb @@ -4,6 +4,19 @@ module Annyong class Application + def server + @server ||= Rack::Server.new({ + :config => File.join(File.dirname(__FILE__), "../../assets/rack/config.ru"), + :Port => port, + :Host => host, + :AccessLog => [], + }) + end + + def run + server.start + end + def port options[:port] end From 51c5935e8c6582564c4f605a752865fc0bcc9314 Mon Sep 17 00:00:00 2001 From: Grayson Wright Date: Sun, 13 Jan 2013 02:12:37 -0500 Subject: [PATCH 3/3] Improved code behind the options banner --- lib/annyong/application.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/annyong/application.rb b/lib/annyong/application.rb index 67d7e2b..29200e9 100644 --- a/lib/annyong/application.rb +++ b/lib/annyong/application.rb @@ -42,14 +42,13 @@ def parse_options(args) } option_parser = OptionParser.new do |opts| - opts.banner = "Usage: annyong [options] + opts.banner = "Usage: annyong [options]" + opts.separator "" + opts.separator "Options:" -Options: - " opts.on("--port [PORT]", "The port to use (default: #{options[:port]})") do |port| options[:port] = port end - opts.on("--host [HOST]", "The host to listen on (default: #{options[:host]})") do |host| options[:host] = host end