Class: Praxis::ApiDefinition
- Inherits:
-
Object
- Object
- Praxis::ApiDefinition
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/praxis/api_definition.rb
Instance Attribute Summary collapse
-
#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.
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.
22 23 24 25 26 27 28 |
# File 'lib/praxis/api_definition.rb', line 22 def initialize @responses = Hash.new @traits = Hash.new @infos = Hash.new do |hash, version| hash[version] = ApiGeneralInfo.new end end |
Instance Attribute Details
#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 |
Class Method Details
.define(&block) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/praxis/api_definition.rb', line 14 def self.define(&block) if block.arity == 0 self.instance.instance_eval(&block) else yield(self.instance) end end |
Instance Method Details
#describe ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/praxis/api_definition.rb', line 54 def describe global_info = @infos[nil].describe data = Hash.new do |hash, version| hash[version] = Hash.new end # Fill in the "info" portion @infos.each do |version,info| next unless version info_hash = global_info.merge(info.describe) [:name, :title].each do |attr| raise "Error: API Global information for version '#{version}' does not have '#{attr}' defined. " unless info_hash.key? attr end data[version][:info] = info_hash end # Maybe report the traits?...or somehow the registered response_templates ... 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)
48 49 50 51 52 |
# File 'lib/praxis/api_definition.rb', line 48 def info( version=nil, &block) i = @infos[version] i.instance_eval(&block) i end |
#response(name) ⇒ Object
34 35 36 37 38 |
# File 'lib/praxis/api_definition.rb', line 34 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
30 31 32 |
# File 'lib/praxis/api_definition.rb', line 30 def response_template(name, &block) @responses[name] = Praxis::ResponseTemplate.new(name, &block) end |
#trait(name, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/praxis/api_definition.rb', line 40 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] = block end |