onemorehill-lame 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
File without changes
data/CHANGELOG ADDED
File without changes
data/README ADDED
@@ -0,0 +1,48 @@
1
+ = lame
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 Lee Chang
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+
4
+ VERSION = "0.0.1"
5
+
6
+ Hoe.spec 'lame' do
7
+ developer "Lee Chang", "leetchang@gmail.com"
8
+ self.rubyforge_name = "onemorehill"
9
+ history_file = "CHANGELOG"
10
+ readme_file = "README"
11
+ name = "Lame"
12
+ end
data/TODO ADDED
File without changes
data/bin/lame ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'fileutils'
5
+ require 'lame'
6
+
7
+
@@ -0,0 +1,42 @@
1
+ require 'cgi'
2
+
3
+ module CallParser
4
+
5
+ class Parser
6
+
7
+ attr_accessor :query, :path
8
+
9
+ def initailize(env)
10
+ @env = env
11
+ end
12
+
13
+ def parse_uri(request_path)
14
+ begin
15
+ uri = (request_path.split("/").collect {|r| r.intern unless r.empty?}).compact
16
+ rescue NoMethodError
17
+ $logger.debug("No URI found for this request")
18
+ uri = Hash.new
19
+ end
20
+ return uri
21
+ end
22
+
23
+ def parse_query_string(query_string)
24
+ begin
25
+ params = query_string.split('&').inject({}) {|h,(k,v)| h[k.split('=')[0].intern] = CGI.unescape(k.split('=')[1]); h}
26
+ rescue NoMethodError
27
+ $logger.debug("No Query String found for this request")
28
+ params = Hash.new
29
+ end
30
+ return params
31
+ end
32
+
33
+ def call(env)
34
+ env[:query] = parse_query_string(env["QUERY_STRING"])
35
+ env[:path] = parse_uri(env["PATH_INFO"])
36
+ resp = response(env)
37
+ [200, {"Content-Type" => "text/plain"}, [resp]]
38
+ end
39
+
40
+ end
41
+
42
+ end
data/lib/controller.rb ADDED
@@ -0,0 +1,44 @@
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
@@ -0,0 +1,176 @@
1
+ require 'yaml'
2
+ require 'logger'
3
+ require 'optparse'
4
+
5
+ module Lame
6
+
7
+ # Build configurations from yaml files
8
+ class Initializer
9
+
10
+ attr_reader :database, :system
11
+
12
+ CONTROLLER = "Controller"
13
+
14
+ def initialize
15
+ self.options
16
+ self.read_config if File.exists?($ROOT + '/conf/config.yml')
17
+ self.set_log
18
+ # Load all common ruby files in lib, and ext directories
19
+ Dir[ $ROOT + "/lib/*.rb"].each {|f| require f}
20
+ # Load modules and classes for controller
21
+ self.instantiate_controller($ROOT + '/ext/controller', '.rb')
22
+ # Load models
23
+ Dir[ $ROOT + "/ext/record/*.rb"].each {|f| require f}
24
+ # Load views
25
+ end
26
+
27
+ def options
28
+ OptionParser.new do |opts|
29
+ opts.banner = "Usage: reco_ctrl.rb [options] [action]"
30
+ opts.separator ""
31
+ 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}
38
+ opts.separator ""
39
+ 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}
43
+ opts.separator ""
44
+ opts.on("-h", "--help", "Show this help message") {puts opts; exit}
45
+ opts.separator""
46
+ opts.parse!
47
+ end
48
+ end
49
+
50
+ def read_config
51
+ # Convert key strings to symbols
52
+ 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}
55
+ rescue NoMethodError
56
+ puts "#{OPTIONS[:env].to_s.capitalize} is not a valid Environment. Please correct the problem and try again."
57
+ #exit
58
+ end
59
+ end
60
+
61
+ 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
93
+ end
94
+ end
95
+ end
96
+
97
+ def instantiate_controller(path, extname)
98
+ pre = File.join(path.split("/").delete_if { |p| p == "controller" })
99
+ dir = path.chomp("/")
100
+ while !dir.nil? do
101
+ dir = instantiate_class(dir, pre, extname)
102
+ end
103
+ end
104
+
105
+ def instantiate_class(path, pre, extname)
106
+ pre_arr = pre.split("/")
107
+ Dir[path + '/*'].each do |d|
108
+ sub_dir_hash = get_modules(d, pre_arr)
109
+ nmodule = build_modules(sub_dir_hash)
110
+ @sub_dir_found = load_class(d, nmodule, extname)
111
+ end
112
+ dir = @sub_dir_found
113
+ @sub_dir_found = nil
114
+ return dir
115
+ end
116
+
117
+ def get_modules(path, pre)
118
+ sub_dir_arr = path.split("/")
119
+ sub_dir = sub_dir_arr - pre
120
+ sub_dir_hash = sub_dir.inject({}) {|h,(k,v)| h[camel_name(k).intern] = camel_name(k) if File.extname(k) != '.rb';h}
121
+ return sub_dir_hash
122
+ end
123
+
124
+ def build_modules(sub_dir_hash)
125
+ nmodule = Module.new
126
+ sub_dir_hash.each_pair do |k,v|
127
+ if Object.constants.include?(k)
128
+ # This is the top module
129
+ nmodule = nmodule.const_get(v)
130
+ next
131
+ elsif nmodule.constants.include?(k)
132
+ # This module already exists
133
+ nmodule = nmodule.const_get(v)
134
+ else
135
+ # This is a new module
136
+ nmodule = nmodule.const_set(v, Module.new)
137
+ end
138
+ end
139
+ return nmodule
140
+ end
141
+
142
+ def load_class(path, nmodule, extname)
143
+ class_before = Object.constants # all current classes and models
144
+ last_item = path.split("/").last
145
+ #Dir.entries(path).each do |d|
146
+ if File.extname(path) == extname
147
+ require path
148
+ elsif last_item != '.' && last_item != '..' && File.directory?(path)
149
+ @sub_dir_found = path
150
+ end
151
+ #end
152
+ # Add the new classes
153
+ class_after = Object.constants
154
+ new_class = class_after - class_before
155
+ new_class.each do |c|
156
+ klass = Object.const_get("#{c}")
157
+ nmodule.const_set("#{c}", klass)
158
+ Object.instance_eval{ remove_const c }
159
+ end
160
+ return @sub_dir_found
161
+ end
162
+
163
+ def camel_name(string)
164
+ name = string.split("_").map {|s| s.capitalize }.join
165
+ return name
166
+ end
167
+
168
+ def camel_path(path, ext)
169
+ new_path = File.join(path.split("/") - ext.split("/"))
170
+ camel_path = new_path.chomp(".haml").split("/").map { |a| a.capitalize }.join.intern
171
+ return camel_path
172
+ end
173
+
174
+ end
175
+
176
+ end
data/lib/lame.rb ADDED
@@ -0,0 +1,15 @@
1
+ lib_path = File.dirname(__FILE__)
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
+
5
+ require 'rubygems'
6
+ require 'initializer'
7
+ require 'controller'
8
+ require 'record/record'
9
+ require 'main'
10
+
11
+ module Lame
12
+
13
+ VERSION = '0.0.1'
14
+
15
+ end
@@ -0,0 +1,2 @@
1
+ class Logger
2
+ end
data/lib/main.rb ADDED
@@ -0,0 +1,36 @@
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
12
+
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
31
+ end
32
+ end
33
+
34
+ $logger.warn("The logging level is set to #{$logger.level}")
35
+ end
36
+ Record::BASE.new(dbconn)
data/lib/mapper.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Lame
2
+
3
+ class Mapper
4
+
5
+ end
6
+
7
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class CrazyTime
3
+
4
+ def initialize
5
+ end
6
+
7
+ def self.strptime(string)
8
+ # Convert strings into a usable Time object. This only works for dates from MySQL.
9
+ string[/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/]
10
+ year = $1.to_i; month = $2.to_i; day = $3.to_i; hour = $4.to_i; min = $5.to_i; sec = $6.to_i
11
+ new_time = Time.local(year,month,day,hour,min,sec)
12
+ return new_time
13
+ end
14
+
15
+ end
@@ -0,0 +1,63 @@
1
+ require 'mysqlplus'
2
+ require 'record/crazy_time.rb'
3
+
4
+ module Record
5
+
6
+ class BASE
7
+
8
+ attr_reader :status
9
+
10
+ def initialize(dbconn)
11
+ begin
12
+ $db_conn = Mysql.real_connect(dbconn[:host], dbconn[:username], dbconn[:password], dbconn[:database], dbconn[:port])
13
+ #$logger.debug("Primary database initialized => #{dbconn[:host]}")
14
+ # Check to make sure there is a read db in the configs
15
+ if dbconn[:read_host].nil?
16
+ $db_conn_read = $db_conn
17
+ else
18
+ $db_conn_read = Mysql.real_connect(dbconn[:read_host], dbconn[:read_username], dbconn[:read_password], dbconn[:read_database], dbconn[:read_port])
19
+ end
20
+ #$logger.debug("Secondary database initialized => #{dbconn[:read_host]}")
21
+ rescue
22
+ # No db information include. No problem. We just won't have a database connection
23
+ #$logger.warn("No database connection initialized")
24
+ end
25
+ end
26
+
27
+ def self.query(query_string)
28
+ n = 0 # track how many times that the system had to reconnect to the db
29
+ begin
30
+ # Test to see if the query starts with a select which would mean it was a read query
31
+ if query_string.split[0].upcase == "SELECT"
32
+ #$logger.debug("Using read database => \"#{query_string}\"")
33
+ res = $db_conn_read.query(query_string)
34
+ else
35
+ #$logger.debug("Using update database => \"#{query_string}\"")
36
+ res = $db_conn.query(query_string)
37
+ end
38
+ rescue Mysql::Error => e
39
+ $logger.error("Mysql query => #{query_string}")
40
+ $logger.error("Mysql::Error '#{e.to_s}'")
41
+ case e.to_s
42
+ when 'MySQL server has gone away'
43
+ $logger.warn("Connection to database #{dbconn[:host]} has gone away. Reconnecting.") if n == 0
44
+ self.new
45
+ n += 1
46
+ retry
47
+ when 'Lost connection to MySQL server during query'
48
+ $logger.warn("Lost connection #{dbconn[:host]}. Reconnecting.") if n == 0
49
+ self.new
50
+ n += 1
51
+ retry
52
+ else
53
+ # Don't know what to do because of an unknown error so to play it safe we'll just break instead looping
54
+ $logger.warn("ERROR: #{e.to_s} Not sure what this error is from #{dbconn[:host]}.")
55
+ end
56
+ end
57
+ return res
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
data/test/test_lame.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "test/unit"
2
+ require "lame"
3
+
4
+ class TestLame < Test::Unit::TestCase
5
+ def test_sanity
6
+ flunk "write tests or I will kneecap you"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onemorehill-lame
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Lee Chang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rubygems
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.1
34
+ version:
35
+ description:
36
+ email:
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .autotest
45
+ - CHANGELOG
46
+ - README
47
+ - Rakefile
48
+ - TODO
49
+ - bin/lame
50
+ - lib/call_parser.rb
51
+ - lib/controller.rb
52
+ - lib/initializer.rb
53
+ - lib/lame.rb
54
+ - lib/log_blaster.rb
55
+ - lib/main.rb
56
+ - lib/mapper.rb
57
+ - lib/record/crazy_time.rb
58
+ - lib/record/record.rb
59
+ - test/test_lame.rb
60
+ has_rdoc: false
61
+ homepage: http://github.com/onemorehill/lame/tree/master
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: Another LaMe Web Frame-work
86
+ test_files: []
87
+