Class: Artifactory::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/artifactory/resources/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Create a new instance



227
228
229
230
231
# File 'lib/artifactory/resources/base.rb', line 227

def initialize(attributes = {})
  attributes.each do |key, value|
    set(key, value)
  end
end

Class Method Details

.attribute(key, default = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/artifactory/resources/base.rb', line 27

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

.attributesArray<Symbol>

The list of attributes defined by this class.

Returns:

  • (Array<Symbol>)


60
61
62
# File 'lib/artifactory/resources/base.rb', line 60

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.

Parameters:

  • options (Hash)

    the list of options passed to the method

Options Hash (options):



189
190
191
# File 'lib/artifactory/resources/base.rb', line 189

def extract_client!(options)
  options.delete(:client) || Artifactory.client
end

.find_from_config(xpath, config, options = {}) ⇒ Object

Find the text elements matching a giving xpath

Parameters:

  • xpath (String)

    xpath expression

  • config (REXML)

    Artifactory configuration file as an REXML doc

  • options (Hash) (defaults to: {})

    the list of options



130
131
132
133
134
135
136
137
138
139
# File 'lib/artifactory/resources/base.rb', line 130

def find_from_config(xpath, config, options = {})
  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, options)
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.

Parameters:

  • options (Hash)

    the list of options to extract the repos from



202
203
204
205
206
# File 'lib/artifactory/resources/base.rb', line 202

def format_repos!(options)
  return options if options[:repos].nil? || options[:repos].empty?
  options[:repos] = Array(options[:repos]).compact.join(',')
  options
end

.from_hash(hash, options = {}) ⇒ ~Resource::Base

Construct a new object from the hash.

Parameters:

  • hash (Hash)

    the hash to create the object with

  • options (Hash) (defaults to: {})

    the list options

Options Hash (options):

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/artifactory/resources/base.rb', line 154

def from_hash(hash, options = {})
  instance = new
  instance.client = extract_client!(options)

  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.

Parameters:

  • url (String)

    the URL to find the user from

  • options (Hash) (defaults to: {})

    the list of options

Options Hash (options):

Returns:



89
90
91
92
# File 'lib/artifactory/resources/base.rb', line 89

def from_url(url, options = {})
  client = extract_client!(options)
  from_hash(client.get(url), client: client)
end

.has_attribute?(key) ⇒ true, false

Determine if this class has a given attribute.

Parameters:

  • key (#to_sym)

    the key to check as an attribute

Returns:

  • (true, false)


72
73
74
# File 'lib/artifactory/resources/base.rb', line 72

def has_attribute?(key)
  attributes.has_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

Parameters:

  • xpath (String)

    xpath expression for the parent element whose children are to be listed

  • config (REXML)

    Artifactory config as an REXML file

  • options (Hash) (defaults to: {})

    the list of options



107
108
109
110
111
112
113
114
115
116
# File 'lib/artifactory/resources/base.rb', line 107

def list_from_config(xpath, config, options = {})
  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, options)
  end
end

.url_safe(value) ⇒ String

Generate a URL-safe string from the given value.

Parameters:

  • value (#to_s)

    the value to sanitize

Returns:

  • (String)

    the URL-safe version of the string



217
218
219
# File 'lib/artifactory/resources/base.rb', line 217

def url_safe(value)
  URI.escape(value.to_s)
end

Instance Method Details

#attributeshash

The list of attributes for this resource.

Returns:

  • (hash)


238
239
240
# File 'lib/artifactory/resources/base.rb', line 238

def attributes
  @attributes ||= self.class.attributes.dup
end

#clientObject

Return this object’s client

Returns:

  • (Object)


222
# File 'lib/artifactory/resources/base.rb', line 222

attribute :client, ->{ Artifactory.client }

#client=(value) ⇒ Object

Set this object’s client

Parameters:

  • value (Object)

    the value to set for client

  • default (Object)

    the default value for this attribute



222
# File 'lib/artifactory/resources/base.rb', line 222

attribute :client, ->{ Artifactory.client }

#client?Boolean

Determines if the client value exists and is truthy

Returns:

  • (Boolean)


222
# File 'lib/artifactory/resources/base.rb', line 222

attribute :client, ->{ Artifactory.client }

#extract_client!(options) ⇒ Object

See Also:



258
259
260
# File 'lib/artifactory/resources/base.rb', line 258

def extract_client!(options)
  self.class.extract_client!(options)
end

#format_repos!(options) ⇒ Object

See Also:



263
264
265
# File 'lib/artifactory/resources/base.rb', line 263

def format_repos!(options)
  self.class.format_repos!(options)
end

#inspectObject



327
328
329
330
331
332
333
334
335
# File 'lib/artifactory/resources/base.rb', line 327

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.

Parameters:

  • key (#to_sym)

    the attribute to set

  • value (Object)

    the value to set

Returns:

  • (Object)

    the set value



253
254
255
# File 'lib/artifactory/resources/base.rb', line 253

def set(key, value)
  attributes[key.to_sym] = value
end

#to_hashHash

The hash representation

Examples:

An example hash response

{ 'key' => 'local-repo1', 'includesPattern' => '**/*' }

Returns:

  • (Hash)


280
281
282
283
284
285
286
287
288
# File 'lib/artifactory/resources/base.rb', line 280

def to_hash
  attributes.inject({}) do |hash, (key, value)|
    unless Resource::Base.has_attribute?(key)
      hash[Util.camelize(key, true)] = value
    end

    hash
  end
end

#to_jsonString

The JSON representation of this object.

Returns:

  • (String)

See Also:



297
298
299
# File 'lib/artifactory/resources/base.rb', line 297

def to_json
  JSON.fast_generate(to_hash)
end

#to_matrix_properties(hash = {}) ⇒ Object

Create URI-escaped string from matrix properties



306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/artifactory/resources/base.rb', line 306

def to_matrix_properties(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_sObject



322
323
324
# File 'lib/artifactory/resources/base.rb', line 322

def to_s
  "#<#{short_classname}>"
end

#url_safe(value) ⇒ Object

See Also:



268
269
270
# File 'lib/artifactory/resources/base.rb', line 268

def url_safe(value)
  self.class.url_safe(value)
end