Created
May 4, 2012 18:38
-
-
Save djones/2596822 to your computer and use it in GitHub Desktop.
Sudocode on how I would like Grape to work with Goliath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sudocode on how I would like grape to work with Goliath | |
# http://localhost:9000/randomuser #=> random user in json | |
require 'goliath' | |
require 'grape' | |
require 'em-synchrony/activerecord' | |
class User < ActiveRecord::Base | |
end | |
class MyAPI < Grape::API | |
get 'randomuser' do | |
User.offset(rand(User.count)).first | |
end | |
end | |
class Srv < Goliath::API | |
map "/" do | |
run MyAPI.new(self) | |
end | |
end |
I do something like this in my project to get a RESTful interface to an API. I should probably use class inheritance, though.
module Goliath
module RESTfulAPI
def self.included(base)
base.class_eval do
attr_reader :request
end
end
def response(env)
@request = ::Rack::Request.new(env)
http_method = env['REQUEST_METHOD'].downcase
response = send(http_method)
if response.is_a?(Array) && response.size == 3
response
else
[200, {}, response]
end
end
end
end
class InboxAPI < Goliath::API
include Goliath::RESTfulAPI
def get
Inbox.find params.slice("rpt_code")
end
end
Also, you can see I need to fix the naive tuple check :).
@jpfuentes2 do you have any other examples of Goliath and REST APIs? Or know of any out there?
Now I've created a Goliath + Grape example here: https://github.com/djones/grape-goliath-example I'm interested to write the same example just with Goliath and see how it compares speed wise.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The ActiveRecord part of this sudocode already works great - see https://github.com/postrank-labs/goliath/blob/master/examples/activerecord/srv.rb
But the integration with Grape part is completely theoretical.