Skip to content

Commit

Permalink
#295: Storing original API source block in endpoint's attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Dec 27, 2012
1 parent 81fa919 commit e2b12e4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* [#296](https://github.com/intridea/grape/issues/296): Fix: ArgumentError with default error formatter - [@dblock](https://github.com/dblock).
* [#297](https://github.com/intridea/grape/issues/297): Added `default_error_formatter` - [@dblock](https://github.com/dblock).
* [#297](https://github.com/intridea/grape/issues/297): Setting `format` will automatically set `default_error_formatter` - [@dblock](https://github.com/dblock).
* [#295](https://github.com/intridea/grape/issues/295): Storing original API source block in endpoint's `source` attribute - [@dblock](https://github.com/dblock).

* Your contribution here.

0.2.3 (24/12/2012)
Expand Down
5 changes: 3 additions & 2 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Grape
# An Endpoint is the proxy scope in which all routing
# blocks are executed. In other words, any methods
# on the instance level of this class may be called
# from inside a `get`, `post`, etc. block.
# from inside a `get`, `post`, etc.
class Endpoint
attr_accessor :block, :options, :settings
attr_accessor :block, :source, :options, :settings
attr_reader :env, :request

class << self
Expand Down Expand Up @@ -40,6 +40,7 @@ def initialize(settings, options = {}, &block)
@settings = settings
if block_given?
method_name = "#{options[:method]} #{settings.gather(:namespace).join( "/")} #{Array(options[:path]).join("/")}"
@source = block
@block = self.class.generate_api_method(method_name, &block)
end
@options = options
Expand Down
19 changes: 19 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,25 @@ def self.call(object, env)
end
end

describe ".endpoint" do
before(:each) do
subject.format :json
subject.get '/endpoint/options' do
{
:path => options[:path],
:source_location => source.source_location
}
end
end
it 'path' do
get '/endpoint/options'
options = MultiJson.load(last_response.body)
options["path"].should == ["/endpoint/options"]
options["source_location"][0].should include "api_spec.rb"
options["source_location"][1].to_i.should > 0
end
end

describe '.route' do
context 'plain' do
before(:each) do
Expand Down
11 changes: 7 additions & 4 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ def app; subject end

describe '#initialize' do
it 'takes a settings stack, options, and a block' do
expect{ Grape::Endpoint.new(Grape::Util::HashStack.new, {
:path => '/',
:method => :get
}, &Proc.new{}) }.not_to raise_error
p = Proc.new {}
expect {
Grape::Endpoint.new(Grape::Util::HashStack.new, {
:path => '/',
:method => :get
}, &p)
}.not_to raise_error
end
end

Expand Down

0 comments on commit e2b12e4

Please sign in to comment.