Created
March 28, 2013 19:07
-
-
Save zampino/5265916 to your computer and use it in GitHub Desktop.
Grape with Exposures
This file contains hidden or 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
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