Skip to content

Instantly share code, notes, and snippets.

@dblock
Created November 12, 2013 13:56
Show Gist options
  • Select an option

  • Save dblock/7431207 to your computer and use it in GitHub Desktop.

Select an option

Save dblock/7431207 to your computer and use it in GitHub Desktop.
require 'spec_helper'
require 'grape'
module DATA
class User
attr_accessor :loginname, :email, :person
def initialize(attrs)
@loginname = attrs[:loginname]
@email = attrs[:email]
@person = attrs[:person]
end
end
class Person
attr_accessor :name, :avatar, :birthday
def initialize(attrs)
@name = attrs[:name]
@avatar = attrs[:avatar]
@birthday = attrs[:birthday]
end
end
end
module API
class Person < Grape::Entity
expose :name, :avatar
end
class User < Grape::Entity
expose :loginname, :email
expose :person, using: API::Person
end
end
describe Grape::Entity do
include Rack::Test::Methods
subject do
Class.new(Grape::API) do
format :json
get do
person = DATA::Person.new(name: 'test', avatar: '/uri', birthday: '2013/11/11')
user = DATA::User.new(loginname: 'test user', email: 'test@test.com', person: person)
present user, with: API::User
end
end
end
def app
subject
end
it "works" do
get '/'
last_response.status.should == 200
JSON.parse(last_response.body).should == {"loginname"=>"test user", "email"=>"test@test.com", "person"=>{"name"=>"test", "avatar"=>"/uri"}}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment