onemorehill-lame 0.0.4 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/initializer.rb CHANGED
@@ -9,37 +9,45 @@ module Lame
9
9
 
10
10
  attr_reader :database, :system
11
11
 
12
- CONTROLLER = "Controller"
12
+ #CONTROLLER = "Controller"
13
13
 
14
- def initialize
15
- self.options
16
- self.read_config if File.exists?($ROOT + '/conf/config.yml')
14
+ def initialize(root)
15
+ @root = root
16
+ @options = {
17
+ :env => (ENV['RACK_ENV'] || :development).to_sym,
18
+ :config => @root + '/conf/',
19
+ :log_path => @root + '/logs',
20
+ :verbose => false
21
+ }
22
+ self.set_options
23
+ self.read_config if File.exists?(@root + '/conf/config.yml')
24
+ self.read_database_config if File.exists?(@root + '/conf/database.yml')
17
25
  self.set_log
18
26
  # Load all common ruby files in lib, and ext directories
19
- Dir[ $ROOT + "/lib/*.rb"].each {|f| require f}
27
+ Dir[ @root + "/lib/*.rb"].each {|f| require f}
20
28
  # Load modules and classes for controller
21
- self.instantiate_controller($ROOT + '/ext/controller', '.rb')
29
+ self.instantiate_controller(@root + '/ext/controller', '.rb')
22
30
  # Load models
23
- Dir[ $ROOT + "/ext/record/*.rb"].each {|f| require f}
31
+ Dir[ @root + "/ext/record/*.rb"].each {|f| require f}
24
32
  # Load views
25
33
  end
26
34
 
27
- def options
35
+ def set_options
28
36
  OptionParser.new do |opts|
29
- opts.banner = "Usage: reco_ctrl.rb [options] [action]"
37
+ opts.banner = "Usage: lame [options] [action]"
30
38
  opts.separator ""
31
39
  opts.separator "Options:"
32
- opts.on("-c", "--config PATH", "Path to configuration files") {|config| OPTIONS[:config] = config}
33
- opts.on("-e", "--env ENVIRONMENT", "Environment to run as") {|env| OPTIONS[:env] = env.intern}
34
- opts.on("-d", "--daemon", "Run as a Daemon") {OPTIONS[:daemon] = true}
35
- opts.on("-p", "--port PORT", "Port for the server to listen on") {|port| OPTIONS[:port] = port}
36
- opts.on("-P", "--pid PATH", "Path to store the PID file") {|pid| OPTIONS[:pid] = pid}
37
- opts.on("-l", "--log PATH", "Path to log files") {|log| OPTIONS[:logs] = log}
40
+ opts.on("-c", "--config PATH", "Path to configuration files") {|config| @options[:config] = config}
41
+ opts.on("-e", "--env ENVIRONMENT", "Environment to run as") {|env| @options[:env] = env.intern}
42
+ opts.on("-d", "--daemon", "Run as a Daemon") {@options[:daemon] = true}
43
+ opts.on("-p", "--port PORT", "Port for the server to listen on") {|port| @options[:port] = port}
44
+ opts.on("-P", "--pid PATH", "Path to store the PID file") {|pid| @options[:pid] = pid}
45
+ opts.on("-l", "--log PATH", "Path to log files") {|log| @options[:logs] = log}
38
46
  opts.separator ""
39
47
  opts.separator "Actions"
40
- opts.on("--start", "Start the server") {OPTIONS[:action] = :start}
41
- opts.on("--stop", "Stop the server") {OPTIONS[:action] = :stop}
42
- opts.on("--restart", "Restart the server") {OPTIONS[:action] = :restart}
48
+ opts.on("--start", "Start the server") {@options[:action] = :start}
49
+ opts.on("--stop", "Stop the server") {@options[:action] = :stop}
50
+ opts.on("--restart", "Restart the server") {@options[:action] = :restart}
43
51
  opts.separator ""
44
52
  opts.on("-h", "--help", "Show this help message") {puts opts; exit}
45
53
  opts.separator""
@@ -50,50 +58,39 @@ module Lame
50
58
  def read_config
51
59
  # Convert key strings to symbols
52
60
  begin
53
- @system = YAML.load_file(OPTIONS[:config] + "config.yml")[OPTIONS[:env].to_s].inject({}) {|h,(k,v)| h[k.intern] =v; h}
54
- @database = YAML.load_file(OPTIONS[:config] + "database.yml")[OPTIONS[:env].to_s].inject({}) {|h,(k,v)| h[k.intern] = v; h}
61
+ @system = YAML.load_file(@options[:config] + "config.yml")[@options[:env].to_s].inject({}) {|h,(k,v)| h[k.intern] =v; h}
55
62
  rescue NoMethodError
56
- puts "#{OPTIONS[:env].to_s.capitalize} is not a valid Environment. Please correct the problem and try again."
57
- #exit
58
63
  end
59
64
  end
60
65
 
61
66
  def set_log
62
- log_path = OPTIONS[:logs] || @system[:logs]
63
- OPTIONS[:log_path] = log_path + '/' + OPTIONS[:type].to_s + OPTIONS[:env].to_s + ".log"
64
- OPTIONS[:rotate] = !@system.nil? ? @system[:rotate] : nil
65
- end
66
-
67
- # Deprecated use instantiate_controller instead
68
- def load_controller
69
- # Include subdirectories and place the class into the module based on the subdirectory name -- ONLY TWO DEEP!
70
- Dir[ ROOT + "/ext/controller/*"].each do |d|
71
- if File.file?(d) && d != '.' && d != '..'
72
- require d if File.extname(d) == '.rb'
73
- elsif d != '.' && d != '..'
74
- # This is a subdirectory
75
- sub_dir = d.split("/").last
76
- module_name = camel_name(sub_dir)
77
- new_module = Object.const_set(module_name,Module.new)
78
- before_classes = Object.constants # these are all the current classes and modules
79
- Dir[d + "/*"].each do |f|
80
- if File.extname(f) == '.rb'
81
- require f
82
- end
83
- end
84
- # Make hash of sub_dirs
85
- after_classes = Object.constants # these are all the classes after loading the files
86
- new_classes = after_classes - before_classes
87
- new_classes.each do |c|
88
- class_name = c.to_s
89
- klass = Object.const_get(class_name) # make class_name into a constant
90
- new_module.const_set(class_name, klass)
91
- Object.instance_eval{ remove_const class_name.intern } # Remove the class from Object
92
- end
67
+ log_path = @system[:log_path] || @options[:log_path]
68
+ @options[:log_path] = log_path + '/' + @options[:env].to_s + ".log"
69
+ @options[:rotate] = !@system.nil? ? @system[:rotate] : nil
70
+ # Set up logging
71
+ if File.exists?(@root + "/logs")
72
+ $logger = Logger.new(@options[:log_path], @options[:rotate])
73
+
74
+ case @options[:env]
75
+ when :development
76
+ $logger.level = Logger::DEBUG
77
+ when :test
78
+ $logger.level = Logger::DEBUG
79
+ when :staging
80
+ $logger.level = Logger::DEBUG
81
+ when :production
82
+ $logger.level = Logger::WARN
93
83
  end
84
+
85
+ $logger.warn("The logging level is set to #{$logger.level}")
94
86
  end
95
87
  end
96
88
 
89
+ def read_database_config
90
+ # Convert key strings to symbols
91
+ @database = YAML.load_file(@options[:config] + "database.yml")[@options[:env].to_s].inject({}) {|h,(k,v)| h[k.intern] = v; h}
92
+ end
93
+
97
94
  def instantiate_controller(path, extname)
98
95
  pre = File.join(path.split("/").delete_if { |p| p == "controller" })
99
96
  dir = path.chomp("/")
data/lib/lame.rb CHANGED
@@ -1,15 +1,7 @@
1
1
  lib_path = File.dirname(__FILE__)
2
2
  $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
3
- $ROOT =File.join(File.expand_path(File.dirname(__FILE__)), "../") if $ROOT.nil?
4
3
 
5
4
  require 'rubygems'
6
- require 'initializer'
7
- require 'controller'
8
- require 'record/record'
9
5
  require 'main'
10
6
 
11
- module Lame
12
-
13
- VERSION = '0.0.1'
14
-
15
- end
7
+ Controller = Class.new
data/lib/main.rb CHANGED
@@ -1,36 +1,73 @@
1
- OPTIONS = {
2
- :config => $ROOT + '/conf/',
3
- :env => :development,
4
- :logs => $ROOT + '/logs',
5
- :verbose => false,
6
- :daemon => false
7
- }
8
-
9
- # Load it all up
10
- $CONFIG = Lame::Initializer.new
11
- dbconn = $CONFIG.database
1
+ require 'rack'
2
+ require 'rack/builder'
3
+ require 'initializer'
4
+ require 'cgi'
12
5
 
13
- # Set up logging
14
- if File.exists?($ROOT + "/logs")
15
- $logger = Logger.new(OPTIONS[:log_path], OPTIONS[:rotate])
16
-
17
- if OPTIONS[:verbose]
18
- $logger.level = Logger::DEBUG
19
- else
20
- case OPTIONS[:env]
21
- when :development
22
- $logger.level = Logger::DEBUG
23
- when :integration
24
- $logger.level = Logger::DEBUG
25
- when :staging
26
- $logger.level = Logger::DEBUG
27
- when :production_test
28
- $logger.level = Logger::DEBUG
29
- when :production
30
- $logger.level = Logger::WARN
6
+ module Lame
7
+
8
+ class Request < Rack::Request
9
+
10
+ def uri
11
+ begin
12
+ @uri = (@env["PATH_INFO"].split("/").collect {|r| r.intern unless r.empty?}).compact
13
+ rescue NoMethodError
14
+ @uri = {}
15
+ end
16
+ end
17
+
18
+ def params
19
+ begin
20
+ @params = @env["QUERY_STRING"].split('&').inject({}) {|h,(k,v) | h[k.split('=')[0].intern] = CGI.unescape(k.split('=')[1]); h}
21
+ rescue
22
+ @params = {}
23
+ end
31
24
  end
25
+
26
+ end
27
+
28
+ class Response < Rack::Response
29
+
32
30
  end
31
+
32
+ class NotFound < NameError
33
+ def code ; 404 ; end
34
+ end
35
+
36
+ class Base
37
+
38
+ attr_accessor :query, :path
39
+
40
+ def initailize(env)
41
+ @env = env
42
+ end
43
+
44
+ def parse_uri(request_path)
45
+ begin
46
+ uri = (request_path.split("/").collect {|r| r.intern unless r.empty?}).compact
47
+ rescue NoMethodError
48
+ uri = Hash.new
49
+ end
50
+ return uri
51
+ end
33
52
 
34
- $logger.warn("The logging level is set to #{$logger.level}")
35
- end
36
- Record::BASE.new(dbconn)
53
+ def parse_query_string(query_string)
54
+ begin
55
+ params = query_string.split('&').inject({}) {|h,(k,v)| h[k.split('=')[0].intern] = CGI.unescape(k.split('=')[1]); h}
56
+ rescue NoMethodError
57
+ params = Hash.new
58
+ end
59
+ return params
60
+ end
61
+
62
+ def call(env)
63
+ env[:query] = self.parse_query_string(env['QUERY_STRING'])
64
+ env[:path] = self.parse_uri(env['PATH_INFO'])
65
+ resp = response(env)
66
+ status_code = resp[:status_code] || 200
67
+ content_type = resp[:content_type] || "text/plain"
68
+ [status_code, {"Content-Type" => content_type}, [resp[:body]]]
69
+ end
70
+
71
+ end
72
+
73
+ end
data/lib/record/record.rb CHANGED
@@ -1,6 +1,3 @@
1
- require 'mysqlplus'
2
- require 'record/crazy_time.rb'
3
-
4
1
  module Record
5
2
 
6
3
  class BASE
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onemorehill-lame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Chang
@@ -38,7 +38,6 @@ files:
38
38
  - TODO
39
39
  - bin/lame
40
40
  - lib/call_parser.rb
41
- - lib/controller.rb
42
41
  - lib/initializer.rb
43
42
  - lib/lame.rb
44
43
  - lib/log_blaster.rb
data/lib/controller.rb DELETED
@@ -1,44 +0,0 @@
1
-
2
- require 'cgi'
3
-
4
- module Controller
5
-
6
- class Base
7
-
8
- attr_accessor :query, :path
9
-
10
- def initailize(env)
11
- @env = env
12
- end
13
-
14
- def parse_uri(request_path)
15
- begin
16
- uri = (request_path.split("/").collect {|r| r.intern unless r.empty?}).compact
17
- rescue NoMethodError
18
- $logger.debug("No URI found for this request")
19
- uri = Hash.new
20
- end
21
- return uri
22
- end
23
-
24
- def parse_query_string(query_string)
25
- begin
26
- params = query_string.split('&').inject({}) {|h,(k,v)| h[k.split('=')[0].intern] = CGI.unescape(k.split('=')[1]); h}
27
- rescue NoMethodError
28
- $logger.debug("No Query String found for this request")
29
- params = Hash.new
30
- end
31
- return params
32
- end
33
-
34
- def call(env)
35
- env[:query] = parse_query_string(env["QUERY_STRING"])
36
- env[:path] = parse_uri(env["PATH_INFO"])
37
- resp = response(env)
38
- content_type = resp[:content_type] || "text/plain"
39
- [200, {"Content-Type" => resp[:content_type]}, [resp[:body]]]
40
- end
41
-
42
- end
43
-
44
- end