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.



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

def initialize(global_info = nil, version: nil)
  @data = {}
  @global_info = global_info
  @version = version

  return unless @global_info.nil?

  # this *is* the global info
  version_with %i[header params]
  consumes 'json', 'x-www-form-urlencoded'
  produces 'json'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, val = nil) ⇒ Object

Allow any custom method to get/set any value



21
22
23
24
25
26
27
# File 'lib/praxis/api_general_info.rb', line 21

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

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

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



146
147
148
149
150
151
152
# File 'lib/praxis/api_general_info.rb', line 146

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/praxis/api_general_info.rb', line 113

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

  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 => version.to_s)
    end

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

#consumes(*vals) ⇒ Object



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

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

#describeObject



154
155
156
157
158
159
160
161
162
# File 'lib/praxis/api_general_info.rb', line 154

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

#description(val = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/praxis/api_general_info.rb', line 68

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

#documentation_url(val = nil) ⇒ Object



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

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

#endpoint(val = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/praxis/api_general_info.rb', line 89

def endpoint(val = nil)
  if val.nil?
    get(:endpoint)
  elsif @global_info.nil?
    set(:endpoint, val) # this *is* the global info
  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 #{version}"
  end
end

#get(key) ⇒ Object



33
34
35
36
37
38
# File 'lib/praxis/api_general_info.rb', line 33

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

  nil
end

#logo_url(val = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/praxis/api_general_info.rb', line 60

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

#name(val = nil) ⇒ Object



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

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

#produces(*vals) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/praxis/api_general_info.rb', line 138

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

#respond_to_missing?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/praxis/api_general_info.rb', line 29

def respond_to_missing?(*)
  true
end

#set(key, val) ⇒ Object



40
41
42
# File 'lib/praxis/api_general_info.rb', line 40

def set(key, val)
  @data[key] = val
end

#title(val = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/praxis/api_general_info.rb', line 52

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

#version_with(val = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/praxis/api_general_info.rb', line 76

def version_with(val = nil)
  if val.nil?
    get(:version_with)
  elsif @global_info.nil?
    Application.instance.versioning_scheme = val
    set(:version_with, val) # this *is* the global info
  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 #{version}"
  end
end