Skip to content

Commit 01676ed

Browse files
author
Michael Bleigh
committed
Changed Endpoint to be a factory instead of a single class. Much more sane encapsulation of behavior this way.
1 parent 894d892 commit 01676ed

File tree

5 files changed

+98
-15
lines changed

5 files changed

+98
-15
lines changed

.rspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
--color
2-
--format=nested
3-
--fail-fast
2+
--format=nested

README.markdown

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Grape
2+
3+
Grape is a REST-like API micro-framework for Ruby. It is built to complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily provide APIs. It has built-in support for common conventions such as multiple formats, subdomain/prefix restriction, and versioning.
4+
5+
## Installation
6+
7+
Grape is available as a gem, to install it just install the gem:
8+
9+
gem install grape
10+
11+
## Basic Usage
12+
13+
Grape APIs are Rack applications that are created by subclassing `Grape::API`. Below is a simple example showing some of the more common features of Grape in the context of recreating parts of the Twitter API.
14+
15+
class Twitter::API < Grape::Base
16+
version '1'
17+
18+
helpers do
19+
def current_user
20+
@current_user ||= User.authorize!(env)
21+
end
22+
end
23+
24+
resource :statuses do
25+
formats :rss, :atom
26+
27+
get :public_timeline do
28+
Tweet.limit(20)
29+
end
30+
31+
get :home_timeline do
32+
current_user.home_timeline
33+
end
34+
35+
post :update do
36+
Tweet.create(
37+
:text => params[:status]
38+
)
39+
end
40+
end
41+
end
42+
43+
# Rack endpoint
44+
Twitter::API.statuses.timelines.get(:public_timeline)
45+
46+
class Twitter::API::User < Grape::Resource::ActiveRecord
47+
represents ::User
48+
49+
property :status, lambda{|u| u.latest_status}, Twitter::API::Status
50+
end
51+
52+
== Note on Patches/Pull Requests
53+
54+
* Fork the project.
55+
* Make your feature addition or bug fix.
56+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
57+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
58+
* Send me a pull request. Bonus points for topic branches.
59+
60+
== Copyright
61+
62+
Copyright (c) 2010 Michael Bleigh and Intridea, Inc. See LICENSE for details.

lib/grape/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def build_endpoint(&block)
122122
b.use Grape::Middleware::Versioner, :versions => (version if version.is_a?(Array)) if version
123123
b.use Grape::Middleware::Formatter, :default_format => default_format || :json
124124

125-
endpoint = Grape::Endpoint.new(&block)
126-
endpoint.send :extend, helpers
125+
endpoint = Grape::Endpoint.generate(&block)
126+
endpoint.send :include, helpers
127127
b.run endpoint
128128

129129
b.to_app

lib/grape/endpoint.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@ module Grape
77
# on the instance level of this class may be called
88
# from inside a `get`, `post`, etc. block.
99
class Endpoint
10-
def initialize(&block)
11-
@block = block
10+
def self.generate(&block)
11+
c = Class.new(Grape::Endpoint)
12+
c.class_eval do
13+
@block = block
14+
end
15+
c
16+
end
17+
18+
class << self
19+
attr_accessor :block
20+
end
21+
22+
def self.call(env)
23+
new.call(env)
1224
end
1325

1426
attr_reader :env, :request
@@ -52,19 +64,12 @@ def header(key = nil, val = nil)
5264
end
5365
end
5466

55-
def reset!
56-
@params = nil
57-
@env = nil
58-
@request = nil
59-
@header = {}
60-
end
61-
6267
def call(env)
63-
reset!
6468
@env = env
69+
@header = {}
6570
@request = Rack::Request.new(@env)
6671

67-
response_text = instance_eval &@block
72+
response_text = instance_eval &self.class.block
6873

6974
[status, header, [response_text]]
7075
end

spec/grape/endpoint_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,21 @@ def app; subject end
8484
post '/new', :text => 'def'
8585
last_response.body.should == 'def'
8686
end
87+
88+
it 'should reset all instance variables (except block) between calls' do
89+
subject.helpers do
90+
def memoized
91+
@memoized ||= params[:howdy]
92+
end
93+
end
94+
95+
subject.get('/hello') do
96+
memoized
97+
end
98+
99+
get '/hello?howdy=hey'
100+
last_response.body.should == 'hey'
101+
get '/hello?howdy=yo'
102+
last_response.body.should == 'yo'
103+
end
87104
end

0 commit comments

Comments
 (0)