Class: Artifactory::Resource::Base
- Inherits:
-
Object
- Object
- Artifactory::Resource::Base
- Defined in:
- lib/artifactory/resources/base.rb
Direct Known Subclasses
Artifact, Backup, Build, BuildComponent, Group, LDAPSetting, Layout, MailServer, PermissionTarget, Plugin, Repository, System, URLBase, User
Class Method Summary collapse
- .attribute(key, default = nil) ⇒ Object
-
.attributes ⇒ Array<Symbol>
The list of attributes defined by this class.
-
.extract_client!(options) ⇒ Object
Get the client (connection) object from the given options.
-
.find_from_config(xpath, config, options = {}) ⇒ Object
Find the text elements matching a giving xpath.
-
.format_repos!(options) ⇒ Object
Format the repos list from the given options.
-
.from_hash(hash, options = {}) ⇒ ~Resource::Base
Construct a new object from the hash.
-
.from_url(url, options = {}) ⇒ ~Resource::Base
Construct a new object from the given URL.
-
.has_attribute?(key) ⇒ true, false
Determine if this class has a given attribute.
-
.list_from_config(xpath, config, options = {}) ⇒ Object
List all the child text elements in the Artifactory configuration file of a node matching the specified xpath.
-
.url_safe(value) ⇒ String
Generate a URL-safe string from the given value.
Instance Method Summary collapse
-
#attributes ⇒ hash
The list of attributes for this resource.
-
#client ⇒ Object
Return this object’s
client
. -
#client=(value) ⇒ Object
Set this object’s
client
. -
#client? ⇒ Boolean
Determines if the
client
value exists and is truthy. - #extract_client!(options) ⇒ Object
- #format_repos!(options) ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
Create a new instance.
- #inspect ⇒ Object
-
#set(key, value) ⇒ Object
Set a given attribute on this resource.
-
#to_hash ⇒ Hash
The hash representation.
-
#to_json ⇒ String
The JSON representation of this object.
-
#to_matrix_properties(hash = {}) ⇒ Object
Create CGI-escaped string from matrix properties.
-
#to_query_string_parameters(hash = {}) ⇒ Object
Create URI-escaped querystring parameters.
- #to_s ⇒ Object
- #url_safe(value) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Base
Create a new instance
253 254 255 256 257 |
# File 'lib/artifactory/resources/base.rb', line 253 def initialize(attributes = {}) attributes.each do |key, value| set(key, value) end end |
Class Method Details
.attribute(key, default = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/artifactory/resources/base.rb', line 45 def attribute(key, default = nil) key = key.to_sym unless key.is_a?(Symbol) # Set this attribute in the top-level hash attributes[key] = nil define_method(key) do value = attributes[key] return value unless value.nil? if default.nil? value elsif default.is_a?(Proc) default.call else default end end define_method("#{key}?") do !!attributes[key] end define_method("#{key}=") do |value| set(key, value) end end |
.attributes ⇒ Array<Symbol>
The list of attributes defined by this class.
78 79 80 |
# File 'lib/artifactory/resources/base.rb', line 78 def attributes @attributes ||= {} end |
.extract_client!(options) ⇒ Object
Get the client (connection) object from the given options. If the :client
key is preset in the hash, it is assumed to contain the connection object to use for the request. If the :client
key is not present, the default Artifactory.client is used.
Warning, the value of Artifactory.client is not threadsafe! If multiple threads or processes are modifying the connection information, the same request could use a different client object. If you use the Client proxy methods, this is handled for you.
Warning, this method will remove the :client
key from the hash if it exists.
215 216 217 |
# File 'lib/artifactory/resources/base.rb', line 215 def extract_client!() .delete(:client) || Artifactory.client end |
.find_from_config(xpath, config, options = {}) ⇒ Object
Find the text elements matching a giving xpath
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/artifactory/resources/base.rb', line 156 def find_from_config(xpath, config, = {}) name_node = REXML::XPath.match(config, xpath) return nil if name_node.empty? properties = {} name_node[0].parent.each_element_with_text do |e| properties[e.name] = Util.to_type(e.text) end from_hash(properties, ) end |
.format_repos!(options) ⇒ Object
Format the repos list from the given options. This method will modify the given Hash parameter!
Warning, this method will modify the given hash if it exists.
228 229 230 231 232 |
# File 'lib/artifactory/resources/base.rb', line 228 def format_repos!() return if [:repos].nil? || [:repos].empty? [:repos] = Array([:repos]).compact.join(",") end |
.from_hash(hash, options = {}) ⇒ ~Resource::Base
Construct a new object from the hash.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/artifactory/resources/base.rb', line 180 def from_hash(hash, = {}) instance = new instance.client = extract_client!() hash.inject(instance) do |instance, (key, value)| method = :"#{Util.underscore(key)}=" if instance.respond_to?(method) instance.send(method, value) end instance end end |
.from_url(url, options = {}) ⇒ ~Resource::Base
Construct a new object from the given URL.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/artifactory/resources/base.rb', line 107 def from_url(url, = {}) # Parse the URL and only use the path so the configured # endpoint/proxy/SSL settings are used in the GET request. path = URI.parse(url_safe(url)).path client = extract_client!() # If the endpoint contains a path part, we must remove the # endpoint path part from path, because the client uses # endpoint + path as its full URI. endpoint_path = URI.parse(client.endpoint).path path.slice!(endpoint_path) from_hash(client.get(path), client: client) end |
.has_attribute?(key) ⇒ true, false
Determine if this class has a given attribute.
90 91 92 |
# File 'lib/artifactory/resources/base.rb', line 90 def has_attribute?(key) attributes.key?(key.to_sym) end |
.list_from_config(xpath, config, options = {}) ⇒ Object
List all the child text elements in the Artifactory configuration file of a node matching the specified xpath
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/artifactory/resources/base.rb', line 133 def list_from_config(xpath, config, = {}) REXML::XPath.match(config, xpath).map do |r| hash = {} r.each_element_with_text do |l| hash[l.name] = l.get_text end from_hash(hash, ) end end |
.url_safe(value) ⇒ String
Generate a URL-safe string from the given value.
243 244 245 |
# File 'lib/artifactory/resources/base.rb', line 243 def url_safe(value) URI.escape(URI.unescape(value.to_s)) end |
Instance Method Details
#attributes ⇒ hash
The list of attributes for this resource.
264 265 266 |
# File 'lib/artifactory/resources/base.rb', line 264 def attributes @attributes ||= self.class.attributes.dup end |
#client ⇒ Object
Return this object’s client
248 |
# File 'lib/artifactory/resources/base.rb', line 248 attribute :client, -> { Artifactory.client } |
#client=(value) ⇒ Object
Set this object’s client
248 |
# File 'lib/artifactory/resources/base.rb', line 248 attribute :client, -> { Artifactory.client } |
#client? ⇒ Boolean
Determines if the client
value exists and is truthy
248 |
# File 'lib/artifactory/resources/base.rb', line 248 attribute :client, -> { Artifactory.client } |
#extract_client!(options) ⇒ Object
284 285 286 |
# File 'lib/artifactory/resources/base.rb', line 284 def extract_client!() self.class.extract_client!() end |
#format_repos!(options) ⇒ Object
289 290 291 |
# File 'lib/artifactory/resources/base.rb', line 289 def format_repos!() self.class.format_repos!() end |
#inspect ⇒ Object
373 374 375 376 377 378 379 380 381 |
# File 'lib/artifactory/resources/base.rb', line 373 def inspect list = attributes.collect do |key, value| unless Resource::Base.has_attribute?(key) "#{key}: #{value.inspect}" end end.compact "#<#{short_classname} #{list.join(', ')}>" end |
#set(key, value) ⇒ Object
Set a given attribute on this resource.
279 280 281 |
# File 'lib/artifactory/resources/base.rb', line 279 def set(key, value) attributes[key.to_sym] = value end |
#to_hash ⇒ Hash
The hash representation
306 307 308 309 310 311 312 313 314 |
# File 'lib/artifactory/resources/base.rb', line 306 def to_hash attributes.inject({}) do |hash, (key, value)| unless Resource::Base.has_attribute?(key) hash[Util.camelize(key, true)] = send(key.to_sym) end hash end end |
#to_json ⇒ String
The JSON representation of this object.
323 324 325 |
# File 'lib/artifactory/resources/base.rb', line 323 def to_json JSON.fast_generate(to_hash) end |
#to_matrix_properties(hash = {}) ⇒ Object
Create CGI-escaped string from matrix properties
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/artifactory/resources/base.rb', line 332 def to_matrix_properties(hash = {}) properties = hash.map do |k, v| key = CGI.escape(k.to_s) value = CGI.escape(v.to_s) "#{key}=#{value}" end if properties.empty? nil else ";#{properties.join(';')}" end end |
#to_query_string_parameters(hash = {}) ⇒ Object
Create URI-escaped querystring parameters
352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/artifactory/resources/base.rb', line 352 def to_query_string_parameters(hash = {}) properties = hash.map do |k, v| key = URI.escape(k.to_s) value = URI.escape(v.to_s) "#{key}=#{value}" end if properties.empty? nil else properties.join("&") end end |
#to_s ⇒ Object
368 369 370 |
# File 'lib/artifactory/resources/base.rb', line 368 def to_s "#<#{short_classname}>" end |
#url_safe(value) ⇒ Object
294 295 296 |
# File 'lib/artifactory/resources/base.rb', line 294 def url_safe(value) self.class.url_safe(value) end |