Class: JabberAdmin::Commands::GetVcard

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber_admin/commands/get_vcard.rb

Overview

Get content from a vCard.

Examples:

JabberAdmin.get_vcard!(
  :fn, 'n.given', 'org.orgunit[]', 'u.known[]',
  user: '[email protected]'
)
# => {:fn=>"Max Mustermann",
#     "n.given"=>"Max",
#     "org.orgunit"=>["Marketing", "Production"],
#     "u.known"=>nil}

**Heads up!** ejabberd version 18.01 has a bug at the get_vcard2_multi command, which just returns the first element of possible multiple values. (in an array)

Class Method Summary collapse

Class Method Details

.call(callable, *keys, user:) ⇒ Hash

Pass the correct data to the given callable.

rubocop:disable Metrics/MethodLength – because the ejabberd REST API

is hard to use in complex scenarios, so we have to work
around it

rubocop:disable Metrics/AbcSize – ditto rubocop:disable Metrics/CyclomaticComplexity – ditto rubocop:disable Metrics/PerceivedComplexity – ditto

Parameters:

  • callable (Proc, #call)

    the callable to call

  • keys (Array<String, Symbol>, String, Symbol)

    name of the vCard field (n.family for multiple levels)

  • user (String)

    user JID wo/ resource (eg. tom@localhost)

Returns:

  • (Hash)

    the vCard details



40
41
42
43
44
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
72
73
74
75
76
77
78
# File 'lib/jabber_admin/commands/get_vcard.rb', line 40

def self.call(callable, *keys, user:)
  uid, host = user.split('@')
  val = proc do |key|
    parts = key.to_s.upcase.split('.')
    args = { name: parts.shift }
    meth = 'get_vcard'

    unless parts.empty?
      args[:subname] = parts.shift
      meth = 'get_vcard2'

      if args[:subname].end_with? '[]'
        meth += '_multi'
        args[:subname].delete_suffix!('[]')
      end
    end

    res = callable.call(meth, check_res_body: false,
                              user: uid, host: host, **args)
    body = (200..299).cover?(res.code) ? JSON.parse(res.body) : nil
    body.is_a?(Hash) ? body['content'] : body
  rescue JabberAdmin::Error => e
    # When ejabberd tells us there was no value, it does this the hard way
    next if e.response.body.include? 'error_no_value_found_in_vcard'
    # Same for the case when there is no vCard at all
    next if e.response.body.include? 'error_no_vcard_found'

    raise e
  end

  # When just one key is requested, we return the value directly
  return val[keys.first] if keys.one?

  # When multiple keys are requested, we assemble a hash
  keys.to_h do |key|
    res_key = key.is_a?(String) ? key.delete_suffix('[]') : key
    [res_key, val[key]]
  end
end