Skip to content

Instantly share code, notes, and snippets.

@zampino
Created March 28, 2013 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zampino/5265916 to your computer and use it in GitHub Desktop.
Save zampino/5265916 to your computer and use it in GitHub Desktop.
Grape with Exposures
require 'ostruct'
require 'grape-entity'
require 'active_support/concern'
module Exposable
extend ActiveSupport::Concern
# included do
# base = self
# end
module ClassMethods
def exsposure_map *map
# preprocess map
build_entity map
end
def build_entity(map)
const_set("Entity", Class.new(Grape::Entity) do
map.each { |param|
send :expose, param
}
end)
end
end
end
class Exposure
include Exposable
def initialize(me)
@me = me
end
exsposure_map :minni
def minni
"mu"
end
end
class GrapeFruit < Grape::API
format :json
class Soma < OpenStruct
def mowgli
"boom"
end
class Entity < Grape::Entity
expose :frag
expose :mag, as: :barre
expose :bang
# expose :mowgli
end
end
resource "/:frag/:mag/" do
desc "some fucsia"
get "hallo" do
frag = params[:frag]
present Soma.new(params)
end
end
resource :expo do
get ":me" do
present Exposure.new(params[:me])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment