Class: Praxis::ApiDefinition
- Inherits:
-
Object
- Object
- Praxis::ApiDefinition
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/praxis/api_definition.rb
Instance Attribute Summary collapse
-
#global_info ⇒ Object
readonly
Returns the value of attribute global_info.
-
#infos ⇒ Object
readonly
Returns the value of attribute infos.
-
#responses ⇒ Object
readonly
Returns the value of attribute responses.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
-
#versioning_scheme ⇒ Object
Returns the value of attribute versioning_scheme.
Class Method Summary collapse
Instance Method Summary collapse
- #describe ⇒ Object
-
#info(version = nil, &block) ⇒ Object
Setting info to the nil version, means setting it for all versions (if they don’t override them).
-
#initialize ⇒ ApiDefinition
constructor
A new instance of ApiDefinition.
- #response(name) ⇒ Object
- #response_template(name, &block) ⇒ Object
- #trait(name, &block) ⇒ Object
Constructor Details
#initialize ⇒ ApiDefinition
Returns a new instance of ApiDefinition.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/praxis/api_definition.rb', line 25 def initialize @responses = Hash.new @traits = Hash.new @base_path = '' @global_info = ApiGeneralInfo.new @infos = Hash.new do |hash, version| hash[version] = ApiGeneralInfo.new(@global_info, version: version) end end |
Instance Attribute Details
#global_info ⇒ Object (readonly)
Returns the value of attribute global_info.
13 14 15 |
# File 'lib/praxis/api_definition.rb', line 13 def global_info @global_info end |
#infos ⇒ Object (readonly)
Returns the value of attribute infos.
12 13 14 |
# File 'lib/praxis/api_definition.rb', line 12 def infos @infos end |
#responses ⇒ Object (readonly)
Returns the value of attribute responses.
11 12 13 |
# File 'lib/praxis/api_definition.rb', line 11 def responses @responses end |
#traits ⇒ Object (readonly)
Returns the value of attribute traits.
10 11 12 |
# File 'lib/praxis/api_definition.rb', line 10 def traits @traits end |
#versioning_scheme ⇒ Object
Returns the value of attribute versioning_scheme.
15 16 17 |
# File 'lib/praxis/api_definition.rb', line 15 def versioning_scheme @versioning_scheme end |
Class Method Details
.define(&block) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/praxis/api_definition.rb', line 17 def self.define(&block) if block.arity == 0 self.instance.instance_eval(&block) else yield(self.instance) end end |
Instance Method Details
#describe ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/praxis/api_definition.rb', line 71 def describe data = Hash.new do |hash, version| hash[version] = Hash.new end data[:global][:info] = @global_info.describe # Fill in the "info" portion @infos.each do |version,info| data[version][:info] = info.describe end if traits.any? data[:traits] = {} traits.each do |name, trait| data[:traits][name] = trait.describe end end data end |
#info(version = nil, &block) ⇒ Object
Setting info to the nil version, means setting it for all versions (if they don’t override them)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/praxis/api_definition.rb', line 55 def info(version=nil, &block) if version.nil? if block_given? @global_info.instance_eval(&block) else @global_info end else i = @infos[version] if block_given? i.instance_eval(&block) end i end end |
#response(name) ⇒ Object
41 42 43 44 45 |
# File 'lib/praxis/api_definition.rb', line 41 def response(name) return @responses.fetch(name) do raise ArgumentError, "no response template defined with name #{name.inspect}. Are you forgetting to register it with ApiDefinition?" end end |
#response_template(name, &block) ⇒ Object
37 38 39 |
# File 'lib/praxis/api_definition.rb', line 37 def response_template(name, &block) @responses[name] = Praxis::ResponseTemplate.new(name, &block) end |
#trait(name, &block) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/praxis/api_definition.rb', line 47 def trait(name, &block) if self.traits.has_key? name raise Exceptions::InvalidTrait.new("Overwriting a previous trait with the same name (#{name})") end self.traits[name] = Trait.new(&block) end |