surfer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in surfer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 AdityaKamatar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Surfer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'surfer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install surfer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/surfer ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'surfer'
3
+ if (ARGV[0]=="new" && ARGV.length==2)
4
+ directory_structure=Surfer::CreateSkeleton.new()
5
+ directory_structure.generate(ARGV[1])
6
+ # puts Surfer::Application.display(ARGV[0])
7
+ elsif(ARGV[0].downcase=="generate" && ARGV[1].downcase=="model")
8
+ model=Surfer::Generate.new()
9
+ model.create(ARGV)
10
+ elsif(ARGV[0].downcase=="generate" && ARGV[1].downcase=="controller")
11
+ controller=Surfer::Generate.new()
12
+ controller.create_controller_files(ARGV[2])
13
+ elsif(ARGV[0].downcase=="run")
14
+ puts "Server Running at localhost:3005\n"
15
+ `rackup -p 3005`
16
+ else
17
+ Surfer::Options.all_opitons
18
+ end
19
+
data/lib/surfer.rb ADDED
@@ -0,0 +1,89 @@
1
+ require "surfer/version"
2
+ require "surfer/routing"
3
+ require "surfer/controller"
4
+ require "erubis"
5
+ require "dbi"
6
+ require "mysql"
7
+ require "surfer/operation"
8
+ require "surfer/create"
9
+ require "surfer/options"
10
+ require "surfer/generator"
11
+
12
+ module Surfer
13
+
14
+ class Application
15
+ @@default_path="default_home_page"
16
+ def call(env)
17
+ if env['REQUEST_METHOD']=="GET"
18
+ if env['PATH_INFO']=='/favicon.ico'
19
+ page_not_found
20
+ end
21
+ if env['PATH_INFO']=='/'
22
+ if(@@default_path=="default_home_page") # If no routes are mentioned in the routes.rb file
23
+ default_home_page
24
+ else
25
+ klass, act , resource=custom_home_page
26
+ controller = klass.new(env: env, controller: klass, action: act , resource: resource)
27
+ d = controller.send(act)
28
+ text = controller.render
29
+ [200,{'Content-Type'=>'text/html'},[text]]
30
+ end
31
+ else
32
+ klass, act , resource = get_controller_and_action(env)
33
+ if(klass=="no_such_path"|| act == "no_such_action")
34
+ page_not_found
35
+ else
36
+ controller = klass.new(env: env, controller: klass, action: act , resource: resource)
37
+ d = controller.send(act)
38
+ text = controller.render
39
+ [200,{'Content-Type'=>'text/html'},[text]]
40
+ end
41
+ end
42
+ else
43
+ return[200,{'Content-Type'=>'text/html'},["POST not Yet Supported"]]
44
+ end
45
+
46
+ end # end of call
47
+
48
+
49
+ def self.routing_config
50
+ puts "Inside routing_config"
51
+ yield(self)
52
+ end
53
+
54
+ def self.root(pth)
55
+ @@default_path=""
56
+ @@default_path=pth.split('#')
57
+ puts @@default_path
58
+ end
59
+
60
+ def custom_home_page
61
+ cont=@@default_path[0]
62
+ action =@@default_path[1]
63
+ autoload="#{cont}_controller"
64
+ puts autoload
65
+ # require "#{autoload}"
66
+ cont = cont.capitalize
67
+ [Object.const_get(cont+"Controller"), action, cont]
68
+ end
69
+
70
+ def page_not_found
71
+ pth=ROOT_PATH+"/public/page_not_found.html"
72
+ content = File.read(pth)
73
+ return [404,{'Content-Type'=>'text/html'},[content]]
74
+ end
75
+
76
+ def default_home_page
77
+ pth=ROOT_PATH+"/public/index.html"
78
+ content = File.read(pth)
79
+ return[200,{'Content-Type'=>'text/html'},[content]]
80
+ end
81
+
82
+ def self.display arg
83
+ "Hello #{arg}"
84
+ end
85
+
86
+
87
+
88
+ end #end of Class
89
+ end # end of Module
@@ -0,0 +1,8 @@
1
+ module Surfer
2
+ class Config
3
+ def self.root_path
4
+ current_path=`pwd`
5
+ current_path=current_path.gsub(/[\s|\n]/,"")
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,52 @@
1
+ module Surfer
2
+ require ::File.expand_path('../readdb',__FILE__)
3
+ class Connection
4
+ @conn=nil
5
+ def create_connection
6
+ file = ReadDbFile.new.read_file
7
+ # puts (file['development']['adapter']).emtpy?
8
+ if (!(file['development']['adapter']).nil?)
9
+ driver = (file['development']['adapter']).capitalize!
10
+ dbase = file['development']['database']
11
+ uname = file['development']['username']
12
+ password = file['development']['password']
13
+ else
14
+ abort ( " Please Complete the database.yml file first " )
15
+ end
16
+ begin
17
+ @conn = DBI.connect( "DBI:#{driver}:#{dbase}", "#{uname}", "#{password}" )
18
+ rescue DBI::DatabaseError => e
19
+ if (e.err == 1049)
20
+ @conq= DBI.connect( "DBI:#{driver}:test", "#{uname}", "#{password}" )
21
+ @conq.do( " create database #{dbase}; " )
22
+ @conq.commit()
23
+ @conq.disconnect if @conq
24
+ @conq= DBI.connect( "DBI:#{driver}:#{dbase}", "#{uname}", "#{password}" )
25
+ # @conq.disconnect if @conq
26
+ puts " #{dbase} Created successfully "
27
+ connection = @conq
28
+ return connection
29
+ # abort ("please create database #{dbase} manually before creating model")
30
+
31
+ else
32
+ puts " Error code: #{e.err} "
33
+ puts " Error message: #{e.errstr}"
34
+
35
+ end
36
+ else
37
+ puts " Connection stablished successfully. "
38
+ connection = @conn
39
+ return connection
40
+ end
41
+ end
42
+
43
+ def close_connection
44
+
45
+ if @conn
46
+ @conn.disconnect
47
+ puts "Connection closed"
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,22 @@
1
+ module Surfer
2
+ class Controller
3
+ def initialize(args)
4
+ @env = args[:env]
5
+ @controller = args[:controller]
6
+ @action = args[:action]
7
+ @resource = args[:resource].downcase
8
+ end
9
+
10
+ def env
11
+ @env
12
+ end
13
+
14
+ def render
15
+ pth=ROOT_PATH+"/app/views/#{@resource}/#{@action}.html.erb"
16
+ puts pth
17
+ content = File.read(pth)
18
+ eruby = Erubis::Eruby.new(content)
19
+ eruby.result(binding())
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,129 @@
1
+ module Surfer
2
+ class CreateSkeleton
3
+ require 'fileutils'
4
+ def create_folder str
5
+ FileUtils.mkdir "#{str}"
6
+ FileUtils.cd "#{str}"
7
+ FileUtils.mkdir "app"
8
+ FileUtils.cd "app"
9
+ FileUtils.mkdir "controllers"
10
+ FileUtils.mkdir "models"
11
+ FileUtils.mkdir "views"
12
+ FileUtils.chdir "../"
13
+ FileUtils.mkdir "config"
14
+ FileUtils.cd "config"
15
+ FileUtils.touch "application.rb"
16
+ FileUtils.touch "database.yml"
17
+ FileUtils.touch "routes.rb"
18
+ FileUtils.chdir "../"
19
+ FileUtils.mkdir "public"
20
+ FileUtils.cd "public"
21
+ FileUtils.touch "index.html"
22
+ FileUtils.touch "page_not_found.html"
23
+ FileUtils.chdir "../"
24
+ FileUtils.touch "Gemfile"
25
+ FileUtils.touch "config.ru"
26
+ end
27
+
28
+ def write_database_file
29
+ FileUtils.cd "config"
30
+ file = File.open("database.yml", "w+")
31
+ file.write "# MySQL. Versions 4.1 and 5.0 are recommended.\n"
32
+ file.write "#\n"
33
+ file.write "# Install the MYSQL driver\n"
34
+ file.write "# gem install mysql2\n"
35
+ file.write "#\n"
36
+ file.write "development:\n"
37
+ file.write " adapter: \n"
38
+ file.write " encoding: \n"
39
+ file.write " database: \n"
40
+ file.write " pool: \n"
41
+ file.write " username: \n"
42
+ file.write " password: \n"
43
+ file.write " socket: \n"
44
+ file.close()
45
+ end
46
+
47
+ def write_application_file str
48
+ str.capitalize!
49
+ file = File.open("application.rb", "w+")
50
+ file.puts 'require "surfer"'+"\n\n"
51
+ file.puts "require 'fileutils'\n"
52
+ file.puts "Dir[File.join(File.dirname(__FILE__), '../app/models/', '*.rb')].each {|file| require file }"
53
+ file.puts "Dir[File.join(File.dirname(__FILE__), '../app/controllers/', '*.rb')].each {|file| require file }"
54
+ file.puts 'root_pth= `pwd`'
55
+ file.puts 'root_pth=root_pth.gsub(/[\s|\n]/,"")'
56
+ file.puts 'ROOT_PATH = root_pth'+"\n\n"
57
+ file.puts "module #{str}"
58
+ file.puts ' class Application < Surfer::Application'
59
+ file.puts ' end'
60
+ file.puts 'end'+"\n\n"
61
+ file.puts 'load "#{ROOT_PATH}/config/routes.rb"'
62
+ file.close()
63
+ end
64
+
65
+ def write_routes_file str
66
+ file = File.open("routes.rb", "w+")
67
+ file.puts "#{str}::Application.routing_config do |app|"
68
+ file.puts " # This is the file where you make all the routing configuratons"
69
+ file.puts ' # You can have the root of your site routed with "root"'
70
+ file.puts ' # by default it is public/index.html.'
71
+ file.puts ' # app.root "computers#show"'+"\n\n"
72
+ file.puts " # Sample Get Request"
73
+ file.puts ' # app.get path: "path", controller: "controller_name", action: "action_name"'
74
+ file.puts ' # eg: app.get path: "bikers", controller: "Bikers", action: "index"'
75
+ file.puts 'end'
76
+ file.close()
77
+ end
78
+
79
+ def write_config_file str
80
+ #puts `pwd`
81
+ str.capitalize!
82
+ FileUtils.chdir "../"
83
+ # puts `pwd`
84
+ file = File.open("config.ru", "w+")
85
+ file.puts "require ::File.expand_path('../config/application',__FILE__)"
86
+ file.puts "run #{str}::Application.new"
87
+ file.close()
88
+ end
89
+
90
+ def write_gem_file
91
+ file = File.open("Gemfile", "w+")
92
+ file.puts "source :rubygems"
93
+ file.puts "gem 'surfer'"
94
+ file.close()
95
+ end
96
+
97
+ def write_html_pages(app_name)
98
+ FileUtils.cd "public"
99
+ file = File.open("index.html", "w+")
100
+ file.puts "<h1 align=\"center\">Home Page of #{app_name}</h1>"
101
+ file.close()
102
+ file = File.open("page_not_found.html", "w+")
103
+ file.puts "<h1 align=\"center\"> 404 </h1>}"
104
+ file.puts "<h3 align=\"center\">Sorry -- Page Not Found</h3>"
105
+ file.puts "<h4 align=\"center\">Please Check routes.rb for routing mismatch</h3>"
106
+ file.close()
107
+ end
108
+
109
+ def generate app_name
110
+
111
+ app_name=app_name.gsub(/\s/,'')
112
+ if app_name.nil?
113
+ puts " Application name is empty. "
114
+ else
115
+ create_folder(app_name)
116
+ write_database_file
117
+ write_application_file(app_name)
118
+ write_routes_file(app_name)
119
+ write_config_file(app_name)
120
+ write_gem_file
121
+ write_html_pages(app_name)
122
+ dir = `pwd`
123
+ cmd = "cd #{dir} && bundle install".gsub(/\n/,"")
124
+ exec cmd
125
+ end
126
+ end
127
+
128
+ end
129
+ end
@@ -0,0 +1,68 @@
1
+ module Surfer
2
+ require 'fileutils'
3
+ require ::File.expand_path('../connection',__FILE__)
4
+ require ::File.expand_path('../support',__FILE__)
5
+ require ::File.expand_path('../readdb',__FILE__)
6
+ class Generate
7
+
8
+ CONNECT = Connection.new
9
+ SUPPORT = Support.new
10
+
11
+ def create(arguments)
12
+ query = SUPPORT.create_table(arguments)
13
+ begin
14
+ @conn = CONNECT.create_connection
15
+ @conn.do("#{query}")
16
+ @conn.commit
17
+ create_model_files(arguments[2])
18
+ rescue DBI::DatabaseError => e
19
+ puts "Error code : #{e.err}"
20
+ puts "Error message : #{e.errstr}"
21
+ @conn.rollback
22
+ else
23
+ puts "Table is created."
24
+ CONNECT.close_connection
25
+ end
26
+ end
27
+
28
+
29
+ def create_model_files (model_name)
30
+ model_class=model_name.capitalize
31
+ FileUtils.cd "app"
32
+ FileUtils.cd "models"
33
+ file = File.open("#{model_name}.rb", "w+")
34
+ file.write "class #{model_class} < Surfer::Operation\n"
35
+ file.write "end"
36
+ file.close()
37
+ end
38
+
39
+ def create_controller_files (controller_name)
40
+ controller_class=controller_name.capitalize
41
+ if(!File.directory?("app"))
42
+ abort ("Please Switch to your Application Directory")
43
+ end
44
+ FileUtils.cd "app"
45
+ FileUtils.cd "controllers"
46
+ file = File.open("#{controller_name}_controller.rb", "w+")
47
+ file.write "class #{controller_class}sController < Surfer::Controller\n"
48
+ file.write " def index\n"
49
+ file.write " end\n"
50
+ file.write "end"
51
+ file.close()
52
+ FileUtils.chdir "../"
53
+ FileUtils.cd "views"
54
+ if(!File.directory?("#{controller_name}s"))
55
+ FileUtils.mkdir "#{controller_name}s"
56
+ FileUtils.cd "#{controller_name}s"
57
+ file = File.open("index.html.erb", "w+")
58
+ file.write "#{controller_class}sController index Action"
59
+ file.close()
60
+ else
61
+ FileUtils.cd "#{controller_name}s"
62
+ file = File.open("index.html.erb", "w+")
63
+ file.write "#{controller_class}sController index Action"
64
+ file.close()
65
+ end
66
+ end
67
+ end
68
+ end