Module: Kong::Base

Included in:
Acl, Api, BasicAuth, Consumer, 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



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

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.



59
60
61
# File 'lib/kong/base.rb', line 59

def api_end_point
  @api_end_point
end

#attributesObject

Returns the value of attribute attributes.



59
60
61
# File 'lib/kong/base.rb', line 59

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kong/base.rb', line 61

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:



82
83
84
# File 'lib/kong/base.rb', line 82

def client
  Client.instance
end

#createObject

Create resource



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

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



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

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

#deleteObject

Delete resource



98
99
100
# File 'lib/kong/base.rb', line 98

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

#get(key = nil) ⇒ Object

Get resource

Parameters:

  • key (String) (defaults to: nil)


88
89
90
91
92
93
94
95
# File 'lib/kong/base.rb', line 88

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: {})


75
76
77
78
# File 'lib/kong/base.rb', line 75

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

#new?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/kong/base.rb', line 102

def new?
  self.id.nil?
end

#primary_keyObject



106
107
108
# File 'lib/kong/base.rb', line 106

def primary_key
  self.new? ? self.name : self.id
end

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

Returns:

  • (Boolean)


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

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



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

def save
  create_or_update
end

#updateObject

Update resource



133
134
135
136
137
138
# File 'lib/kong/base.rb', line 133

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