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
137 138 139 140 141 142 143 144 145 |
# File 'lib/kong/base.rb', line 137 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
113 114 115 116 117 118 |
# File 'lib/kong/base.rb', line 113 def create headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } response = client.post(@api_end_point, nil, attributes, 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
122 123 124 125 126 127 |
# File 'lib/kong/base.rb', line 122 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
99 100 101 |
# File 'lib/kong/base.rb', line 99 def delete client.delete("#{@api_end_point}#{self.id}") end |
#get(key = nil) ⇒ Object
Get resource
89 90 91 92 93 94 95 96 |
# File 'lib/kong/base.rb', line 89 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
103 104 105 |
# File 'lib/kong/base.rb', line 103 def new? self.id.nil? end |
#respond_to?(method, include_private = false) ⇒ Boolean
147 148 149 150 151 152 153 |
# File 'lib/kong/base.rb', line 147 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
108 109 110 |
# File 'lib/kong/base.rb', line 108 def save create_or_update end |
#update ⇒ Object
Update resource
130 131 132 133 134 135 |
# File 'lib/kong/base.rb', line 130 def update headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } response = client.patch("#{@api_end_point}#{self.id}", nil, attributes, headers) init_attributes(response) self end |