Skip to content

Instantly share code, notes, and snippets.

@djones
Created May 4, 2012 18:38
Show Gist options
  • Save djones/2596822 to your computer and use it in GitHub Desktop.
Save djones/2596822 to your computer and use it in GitHub Desktop.
Sudocode on how I would like Grape to work with Goliath
# 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
@djones
Copy link
Author

djones commented May 4, 2012

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.

@jpfuentes2
Copy link

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

@jpfuentes2
Copy link

Also, you can see I need to fix the naive tuple check :).

@djones
Copy link
Author

djones commented May 12, 2012

@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