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
-
#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
136 137 138 139 140 141 142 143 144 |
# File 'lib/kong/base.rb', line 136 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.
59 60 61 |
# File 'lib/kong/base.rb', line 59 def api_end_point @api_end_point end |
#attributes ⇒ Object
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
#client ⇒ Kong::Client
Get Kong API client
82 83 84 |
# File 'lib/kong/base.rb', line 82 def client Client.instance end |
#create ⇒ Object
Create resource
112 113 114 115 116 117 |
# File 'lib/kong/base.rb', line 112 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
121 122 123 124 125 126 |
# File 'lib/kong/base.rb', line 121 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
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
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
75 76 77 78 |
# File 'lib/kong/base.rb', line 75 def initialize(attributes = {}) init_api_end_point init_attributes(attributes) end |
#new? ⇒ Boolean
102 103 104 |
# File 'lib/kong/base.rb', line 102 def new? self.id.nil? end |
#respond_to?(method, include_private = false) ⇒ Boolean
146 147 148 149 150 151 152 |
# File 'lib/kong/base.rb', line 146 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
107 108 109 |
# File 'lib/kong/base.rb', line 107 def save create_or_update end |
#update ⇒ Object
Update resource
129 130 131 132 133 134 |
# File 'lib/kong/base.rb', line 129 def update headers = { 'Content-Type' => 'application/json' } response = client.patch("#{@api_end_point}#{self.id}", attributes, nil, headers) init_attributes(response) self end |