Module: CandlepinAPI

Included in:
Endpoint
Defined in:
lib/candlepin-api/base.rb,
lib/candlepin-api/version.rb,
lib/candlepin-api/entrypoint-extensions.rb

Overview

Classes for realising the Candlepin API in code.

There’ll be a class for each endpoint, and for each supported verb (entry point) for each endpoint. Because of inheritance issues, the same base class is used for all, and different subsets of the methods/attributes used as appropriate to the conditions.

One goal is to use these for parsing a request from a client; another goal is to be able to create a request as though it came from a client. Thus we should hopefully be able to do away with a lot of internal knowledge or special-casing.

The class methods access the fixed attributes of each endpoint or endpoint/verb combination. The instance variables are for those values specific to a particular request. For instance, the class method ListProduct#admin_only? indicates whether that verb can only be accessed via the super-admin role.

For simplicity’s sake (?) the class methods are available from instances as well. ListProduct#admin_only? and ListProduct.new#admin_only? return the same value. So the object representing the current request can be directly queried without resorting to determining its class first.

Defined Under Namespace

Modules: EntrypointExtensions Classes: APIError, BadAPIVersion, Endpoint

Constant Summary collapse

Placeholder_RE =

REST parameters appearing in the URI path are represented by a regular-expression placehold for easy extraction.

Regexp.new(Regexp.escape('([^/]+)'))
VERSION =

Frozen string representation of the module version number.

@version.to_s.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.basic_auth(*args_p) ⇒ nil, Array<String>

Like #server_uri, this allows setting default Basic Authentication credentials module-wide. They can be overridden on an instance-by-instance basis.



68
69
70
71
72
73
74
75
76
77
# File 'lib/candlepin-api/base.rb', line 68

def basic_auth(*args_p)
  unless ([0, 2].include?(args_p.size))
    raise ArgumentError.new('wrong number of arguments ' +
                            "(#{args_p.size} for 0 or 2)")
  end
  if (args_p.size == 2)
    @basic_auth_credentials = args_p.dup
  end
  return @basic_auth_credentials
end

.basic_auth=(value) ⇒ nil, Array<String>

Related to #basic_auth but allows setting the credentials using the more canonical syntax.



88
89
90
# File 'lib/candlepin-api/base.rb', line 88

def basic_auth=(username, password)
  return basic_auth(username, password)
end

.server_uriObject

This refers to the default module-wide URI for the Candlepin server. If individual entrypoint instances don’t set their own, this is what will be used.



54
55
56
# File 'lib/candlepin-api/base.rb', line 54

def server_uri
  @server_uri
end

.server_uri=(value) ⇒ Object

This refers to the default module-wide URI for the Candlepin server. If individual entrypoint instances don’t set their own, this is what will be used.



54
55
56
# File 'lib/candlepin-api/base.rb', line 54

def server_uri=(value)
  @server_uri = value
end

.VERSIONObject

Returns the package version number as a string.



76
77
78
# File 'lib/candlepin-api/version.rb', line 76

def VERSION
  return self::VERSION
end

.versionObject

Returns the Versionomy representation of the package version number.



68
69
70
# File 'lib/candlepin-api/version.rb', line 68

def version
  return @version
end

Instance Method Details

#normalise_rqtype(mname = self.class.const_get('API_Method_Full_Name')) ⇒ String

Turn the convoluted method path into a simple name for an entrypoint.



108
109
110
111
112
113
114
# File 'lib/candlepin-api/base.rb', line 108

def normalise_rqtype(mname=self.class.const_get('API_Method_Full_Name'))
  methname = mname.sub(%r!^org(?:\.fedoraproject)?\.candlepin\.resource\.!i, '')
  (rtype, opname) = methname.split(%r!\.!)
  rtype.sub!(%r!Resource$!, '')
  opname << rtype unless (opname =~ %r!#{rtype}!i)
  return opname.to_sym
end

#regex2printf(path_p) ⇒ String

Given a REST URI path containing placeholders, replace them with ‘%s’ printf format effectors for generation of a useable path.

See Also:



126
127
128
129
130
# File 'lib/candlepin-api/base.rb', line 126

def regex2printf(path_p)
  path = path_p.gsub(Placeholder_RE, '%s')
  path = path.sub(%r!^\^!, '/candlepin/').sub(%r!/\?\$$!, '/')
  return path
end