Class: Praxis::ApiGeneralInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/api_general_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_info = nil, version: nil) ⇒ ApiGeneralInfo

Returns a new instance of ApiGeneralInfo.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/praxis/api_general_info.rb', line 6

def initialize(global_info=nil, version: nil)
  @data = Hash.new
  @global_info = global_info
  @version = version

  if @global_info.nil? # this *is* the global info
    version_with [:header, :params]
    consumes 'json', 'x-www-form-urlencoded'
    produces 'json'
  end
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/praxis/api_general_info.rb', line 4

def version
  @version
end

Instance Method Details

#base_params(type = Attributor::Struct, **opts, &block) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/praxis/api_general_info.rb', line 119

def base_params(type=Attributor::Struct, **opts, &block)
  if !block && type == Attributor::Struct
    get(:base_params)
  else
    set(:base_params, Attributor::Attribute.new(type, opts, &block) )
  end
end

#base_path(val = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/praxis/api_general_info.rb', line 82

def base_path(val=nil)
  if val
    return set(:base_path, val)
  end

  if @global_info # this is for a specific version
    global_path = @global_info.base_path
    if version_with == :path
      global_pattern = Mustermann.new(global_path)
      global_path = global_pattern.expand(Request::API_VERSION_PARAM_NAME => self.version)
    end

    version_path = @data.fetch(:base_path,'')
    "#{global_path}#{version_path}"
  else
    @data.fetch(:base_path,'')
  end
end

#consumes(*vals) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/praxis/api_general_info.rb', line 101

def consumes(*vals)
  if vals.empty?
    return get(:consumes)
  else
    return set(:consumes, vals)
  end
end

#describeObject



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/praxis/api_general_info.rb', line 127

def describe
  hash = { schema_version: "1.0".freeze }
  [:name, :title, :description, :base_path, :version_with, :endpoint, :consumes, :produces].each do |attr|
    val = self.__send__(attr)
    hash[attr] = val unless val.nil?
  end
  if base_params
    hash[:base_params] = base_params.describe[:type][:attributes]
  end
  hash
end

#description(val = nil) ⇒ Object



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

def description(val=nil)
  if val.nil?
    get(:description)
  else
    set(:description, val)
  end
end

#endpoint(val = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/praxis/api_general_info.rb', line 67

def endpoint(val=nil)
  if val.nil?
    get(:endpoint)
  else
    if @global_info.nil? # this *is* the global info
      set(:endpoint, val)
    else
      raise "Use of endpoint is only allowed in the global part of " \
        "the API definition (but you are attempting to use it in the API " \
        "definition of version #{self.version}"
    end
  end
end

#get(k) ⇒ Object



18
19
20
21
22
# File 'lib/praxis/api_general_info.rb', line 18

def get(k)
  return @data[k] if @data.key?(k)
  return @global_info.get(k) if @global_info
  nil
end

#name(val = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/praxis/api_general_info.rb', line 28

def name(val=nil)
  if val.nil?
    get(:name)
  else
    set(:name, val)
  end
end

#produces(*vals) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/praxis/api_general_info.rb', line 110

def produces(*vals)
  if vals.empty?
    return get(:produces)
  else
    return set(:produces, vals)
  end
end

#set(k, v) ⇒ Object



24
25
26
# File 'lib/praxis/api_general_info.rb', line 24

def set(k, v)
  @data[k] = v
end

#title(val = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/praxis/api_general_info.rb', line 36

def title(val=nil)
  if val.nil?
    get(:title)
  else
    set(:title, val)
  end
end

#version_with(val = nil) ⇒ Object



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

def version_with(val=nil)
  if val.nil?
    get(:version_with)
  else
    if @global_info.nil? # this *is* the global info
      Application.instance.versioning_scheme = val
      set(:version_with, val)
    else
      raise "Use of version_with is only allowed in the global part of " \
        "the API definition (but you are attempting to use it in the API " \
        "definition of version #{self.version}"
    end
  end
end