Class: Praxis::ApiDefinition

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/praxis/api_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiDefinition

Returns a new instance of ApiDefinition.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/praxis/api_definition.rb', line 23

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)
  end
end

Instance Attribute Details

#global_infoObject (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

#infosObject (readonly)

Returns the value of attribute infos.



12
13
14
# File 'lib/praxis/api_definition.rb', line 12

def infos
  @infos
end

#responsesObject (readonly)

Returns the value of attribute responses.



11
12
13
# File 'lib/praxis/api_definition.rb', line 11

def responses
  @responses
end

#traitsObject (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



15
16
17
18
19
20
21
# File 'lib/praxis/api_definition.rb', line 15

def self.define(&block)
  if block.arity == 0
    self.instance.instance_eval(&block)
  else
    yield(self.instance)
  end
end

Instance Method Details

#describeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/praxis/api_definition.rb', line 69

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)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/praxis/api_definition.rb', line 53

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



39
40
41
42
43
# File 'lib/praxis/api_definition.rb', line 39

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



35
36
37
# File 'lib/praxis/api_definition.rb', line 35

def response_template(name, &block)
  @responses[name] = Praxis::ResponseTemplate.new(name, &block)
end

#trait(name, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/praxis/api_definition.rb', line 45

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