Skip to content

Commit

Permalink
Merge pull request ruby-grape#2 from mystichacker/master
Browse files Browse the repository at this point in the history
Add subscription support
  • Loading branch information
Dave McNelis committed Feb 6, 2013
2 parents 7adbd1d + b1df17a commit bb693ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ def desc(description, options = {})
@last_description = options.merge(:description => description)
end

# Add tier to the next namespace or function.
def tier(tier, options = {})
tier = :any if tier == '*'
tier = tier.to_sym.downcase
tier = [:basic,:enhanced,:premium,:any,:none].include?(tier) ? tier : :none
@last_tier = options.merge(:tier => tier)
end

# Add level to the next namespace or function.
def level(level, options = {})
level = :any if level == '*'
level = level.to_sym.downcase
level = [:account,:contact,:advisor,:advisory_firm,:segment,:bundle,:any,:none].include?(level) ? level : :none
@last_level = options.merge(:level => level)
end

# Specify the default format for the API's serializers.
# May be `:json` or `:txt` (default).
def default_format(new_format = nil)
Expand Down Expand Up @@ -316,11 +332,13 @@ def route(methods, paths = ['/'], route_options = {}, &block)
endpoint_options = {
:method => methods,
:path => paths,
:route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
:route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(@namespace_tier || {}).deep_merge(@last_tier || {}).deep_merge(@namespace_level || {}).deep_merge(@last_level || {}).deep_merge(route_options || {})
}
endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block)

@last_description = nil
@last_tier = nil
@last_level = nil
reset_validations!
end

Expand Down Expand Up @@ -349,10 +367,18 @@ def namespace(space = nil, &block)
previous_namespace_description = @namespace_description
@namespace_description = (@namespace_description || {}).deep_merge(@last_description || {})
@last_description = nil
previous_namespace_tier = @namespace_tier
@namespace_tier = (@namespace_tier || {}).deep_merge(@last_tier || {})
@last_tier = nil
previous_namespace_level = @namespace_level
@namespace_level = (@namespace_level || {}).deep_merge(@last_level || {})
@last_level = nil
nest(block) do
set(:namespace, space.to_s) if space
end
@namespace_description = previous_namespace_description
@namespace_tier = previous_namespace_tier
@namespace_level = previous_namespace_level
else
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
end
Expand Down
10 changes: 10 additions & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ def params
deep_merge(env['rack.routing_args'] || {})
end

# The minimum API tier
def tier
env["api.endpoint"].options.fetch(:route_options,{}).fetch(:tier,:any)
end

# The minimum API level
def level
env["api.endpoint"].options.fetch(:route_options,{}).fetch(:level,:any)
end

# A filtering method that will return a hash
# consisting only of keys that have been declared by a
# `params` statement.
Expand Down

0 comments on commit bb693ac

Please sign in to comment.