Module: Kong::Base

Included in:
Acl, Api, BasicAuth, Consumer, HmacAuth, JWT, KeyAuth, OAuth2Token, OAuthApp, Plugin, Target, Upstream
Defined in:
lib/kong/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/kong/base.rb', line 150

def method_missing(method, *arguments, &block)
  if self.class.attribute_names.include?(method.to_s)
    @attributes[method.to_s]
  elsif method.to_s.end_with?('=') && self.class.attribute_names.include?(attribute = method.to_s.split('=').first)
    @attributes[attribute] = arguments[0]
  else
    super
  end
end

Instance Attribute Details

#api_end_pointObject

Returns the value of attribute api_end_point.



73
74
75
# File 'lib/kong/base.rb', line 73

def api_end_point
  @api_end_point
end

#attributesObject

Returns the value of attribute attributes.



73
74
75
# File 'lib/kong/base.rb', line 73

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kong/base.rb', line 75

def self.included(base)
  base.extend(ClassMethods)

  base.send(:define_singleton_method, :attribute_names) do
    base::ATTRIBUTE_NAMES
  end

  base.send(:define_method, :init_api_end_point) do
    @api_end_point = base::API_END_POINT
  end
end

Instance Method Details

#clientKong::Client

Get Kong API client

Returns:



96
97
98
# File 'lib/kong/base.rb', line 96

def client
  Client.instance
end

#createObject

Create resource



126
127
128
129
130
131
# File 'lib/kong/base.rb', line 126

def create
  headers = { 'Content-Type' => 'application/json' }
  response = client.post(@api_end_point, attributes, nil, headers)
  init_attributes(response)
  self
end

#create_or_updateObject

Create or update resource Data is sent to Kong in JSON format and HTTP PUT request is used



135
136
137
138
139
140
# File 'lib/kong/base.rb', line 135

def create_or_update
  headers = { 'Content-Type' => 'application/json' }
  response = client.put(@api_end_point, attributes, nil, headers)
  init_attributes(response)
  self
end

#deleteObject

Delete resource



112
113
114
# File 'lib/kong/base.rb', line 112

def delete
  client.delete("#{@api_end_point}#{self.id}")
end

#get(key = nil) ⇒ Object

Get resource

Parameters:

  • key (String) (defaults to: nil)


102
103
104
105
106
107
108
109
# File 'lib/kong/base.rb', line 102

def get(key = nil)
  key = self.id if key.nil?
  path = @api_end_point + key
  response = client.get(path) rescue nil
  return nil if response.nil?
  init_attributes(response)
  self
end

#initialize(attributes = {}) ⇒ Object

Parameters:

  • attributes (Hash) (defaults to: {})


89
90
91
92
# File 'lib/kong/base.rb', line 89

def initialize(attributes = {})
  init_api_end_point
  init_attributes(attributes)
end

#new?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/kong/base.rb', line 116

def new?
  self.id.nil?
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
# File 'lib/kong/base.rb', line 160

def respond_to?(method, include_private = false)
  if self.class.attribute_names.include?(method.to_s.split('=')[0])
    true
  else
    super
  end
end

#saveObject

Save resource to Kong



121
122
123
# File 'lib/kong/base.rb', line 121

def save
  create_or_update
end

#updateObject

Update resource



143
144
145
146
147
148
# File 'lib/kong/base.rb', line 143

def update
  headers = { 'Content-Type' => 'application/json' }
  response = client.patch("#{@api_end_point}#{self.id}", attributes, nil, headers)
  init_attributes(response)
  self
end