Skip to content

Commit

Permalink
refactored app to be more portable
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoluwa Akinniranye committed Dec 11, 2015
1 parent 53eb618 commit f5c87f0
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 158 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gem 'rack'

group :development, :test do
gem 'pry'
end
20 changes: 20 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
GEM
specs:
coderay (1.1.0)
method_source (0.8.2)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
slop (3.6.0)

PLATFORMS
ruby

DEPENDENCIES
pry
rack

BUNDLED WITH
1.10.6
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Reico Server

## Inspiration
I've been using the Sinatra framework, as well as the Ruby on Rails framework for a while but I've always loved the way someone or a group of
people come together to build something which ends up making our lives easier.

I was most fascinated by Sinatra which was built with less than 3000 lines of code with very little baggage and I kept telling myself that if someone else could do it, so can I.

`Currently, its been configured to only respond with a content type of 'application/json'`

To define your routes, all you have to do is:

```ruby
require './dsl'

class Reico
include DSL

def root_path
response({data: :amazing}.to_json)
end
end

Reico.serve do
get '/path', to: "controller#action"

get '/path2' do
#evaluate path2 here
end
end
```

In you `config.ru` you may want to run, depending on the class you are serving from. Mine is from `Reico`

```ruby
Rack::Handler::WEBrick.run Reico
```

There are a lot of implementations that has not been done in this, including security, error handling, testing amongst others, but as they say `Great things starts small`
9 changes: 4 additions & 5 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'rack'
require 'json'
require 'pry'
require './serv'
require './reico'

app = Proc.new{ |env|
[200, {'Content-Type' => 'application/json'}, [{mail: 'application'}.to_json]]
Expand All @@ -14,9 +12,10 @@ class RoutingError < NameError
end

Reico.serve do
# get '/', '/game', '/amazing' do
get '/oreoluwa' do
{can: :do, amazing: :things}.to_json
end

# end
get '/', to: 'oreoluwa#action'

get '/cent', to: 'cent#whatever'
Expand Down
93 changes: 42 additions & 51 deletions dsl.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
require 'rack'
require 'json'

module DSL
def self.included(base)
# base.send :include, InstanceMethods
base.extend ClassMethods
end

module ClassMethods
def call(env)

route_handler = new
req = Rack::Request.new env

routes_and_handler = route_handler.all_routes

path = req.path
method_append = path.match(/^[\/]?(.*)[\/]?/)

method_called = req.request_method.downcase.to_sym

methods_allowed = routes_and_handler.keys
defined_method = "#{method_called}_#{$1}"

if methods_allowed.include?(method_called) && route_handler.respond_to?(defined_method)
route_handler.send(defined_method)
else
route_handler.invalid_route
end
end

def serve(&block)
method_to_call = new
method_to_call.instance_eval(&block)
end


def router_reg(route_param, method_called)
def router_reg(route_param, method_called, &_block)
path = route_param[:path]
action_defined = route_param[:use]

path_defined = path.match(/^[\/]?(.*)[\/]?/)
action = "#{method_called}_#{$1}"

define_method action do #|arg|
if action_defined
if block_given?
response(yield)
elsif action_defined
resp = action_to_call(action_defined)
response(resp)
elsif action == 'get_' || action == 'post_'
Expand Down Expand Up @@ -63,7 +90,7 @@ def analyze_method(method_name, *args, &_block)
if [:get, :post].include? method_name
args = manipulate(args)

self.class.router_reg(args, method_name)
self.class.router_reg(args, method_name, &_block)

register_routes(method_name, args)
all_routes
Expand All @@ -72,6 +99,18 @@ def analyze_method(method_name, *args, &_block)
end
end

def invalid_route message: 'Invalid URL Specified',status: 404
Rack::Response.new(message, status)
end

def response(doc)
resp = Rack::Response.new
resp.write(doc)
resp['Content-Type'] = 'application/json'
resp.status = 200
resp.finish
end

def manipulate(_args)
route = {}
_args.flatten.each do |_arg|
Expand All @@ -92,51 +131,3 @@ def manipulate(_args)
route
end
end

class Oreoluwa
def action
# 'Oreoluwa is awesome'
{kool: :right, awesome: :amazing}.to_json
end
end


class Cent
def whatever
{president: :amity, awesome: :cool}.to_json
end
end

class Kay
def coolio
{jumper: :yeah, kay: :kode}.to_json
end
end

class Daisi
def yes
{this: :is, it: :yes}.to_json
end
end


# routes.each do |route|
# end

# route[arg]
# route
# puts 'This is cool'
# puts route
# @@methods_and_routes[method].flatten

# def get(*args, &block)
# # register_routes()
# puts "I called get"
# end
#
# def post(*args, &block)
# puts args
# end
# puts args
# send(:register_routes, args, &block)
# end
44 changes: 44 additions & 0 deletions reico.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require './dsl'

class Reico
include DSL

PID_FILE = "tmp/pids/server.pid"

# @@token = []

def root_path
response({data: 'Got root url good'}.to_json)
end


def request_params

end

end

class Oreoluwa
def action
{kool: :right, awesome: :amazing}.to_json
end
end


class Cent
def whatever
{president: :amity, awesome: :cool}.to_json
end
end

class Kay
def coolio
{jumper: :yeah, kay: :kode}.to_json
end
end

class Daisi
def yes
{this: :is, it: :yes}.to_json
end
end
102 changes: 0 additions & 102 deletions serv.rb

This file was deleted.

0 comments on commit f5c87f0

Please sign in to comment.