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
-
#api_end_point ⇒ Object
Returns the value of attribute api_end_point.
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Class Method Summary collapse
Instance Method Summary collapse
-
#client ⇒ Kong::Client
Get Kong API client.
-
#create ⇒ Object
Create resource.
-
#create_or_update ⇒ Object
Create or update resource Data is sent to Kong in JSON format and HTTP PUT request is used.
-
#delete ⇒ Object
Delete resource.
-
#get(key = nil) ⇒ Object
Get resource.
- #initialize(attributes = {}) ⇒ Object
- #method_missing(method, *arguments, &block) ⇒ Object
- #new? ⇒ Boolean
- #respond_to?(method, include_private = false) ⇒ Boolean
-
#save ⇒ Object
Save resource to Kong.
-
#update ⇒ Object
Update resource.
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_point ⇒ Object
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 |
#attributes ⇒ Object
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
#client ⇒ Kong::Client
Get Kong API client
96 97 98 |
# File 'lib/kong/base.rb', line 96 def client Client.instance end |
#create ⇒ Object
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_update ⇒ Object
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 |
#delete ⇒ Object
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
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
89 90 91 92 |
# File 'lib/kong/base.rb', line 89 def initialize(attributes = {}) init_api_end_point init_attributes(attributes) end |
#new? ⇒ Boolean
116 117 118 |
# File 'lib/kong/base.rb', line 116 def new? self.id.nil? end |
#respond_to?(method, include_private = false) ⇒ 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 |
#save ⇒ Object
Save resource to Kong
121 122 123 |
# File 'lib/kong/base.rb', line 121 def save create_or_update end |
#update ⇒ Object
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 |