Class: Tilia::CardDav::Xml::Filter::PropFilter

Inherits:
Object
  • Object
show all
Includes:
Xml::XmlDeserializable
Defined in:
lib/tilia/card_dav/xml/filter/prop_filter.rb

Overview

PropFilter parser.

This class parses the urn:ietf:params:xml:ns:carddavprop-filter XML element, as defined in:

tools.ietf.org/html/rfc6352#section-10.5.1

The result will be spit out as an array.

Class Method Summary collapse

Class Method Details

.xml_deserialize(reader) ⇒ Object

The deserialize method is called during xml parsing.

This method is called statictly, this is because in theory this method may be used as a type of constructor, or factory method.

Often you want to return an instance of the current class, but you are free to return other data as well.

You are responsible for advancing the reader to the next element. Not doing anything will result in a never-ending loop.

If you just want to skip parsing for this element altogether, you can just call reader.next

reader.parse_inner_tree will parse the entire sub-tree, and advance to the next element.

Parameters:

  • Reader

    reader

Returns:

  • mixed



35
36
37
38
39
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
# File 'lib/tilia/card_dav/xml/filter/prop_filter.rb', line 35

def self.xml_deserialize(reader)
  result = {
    'name'           => nil,
    'test'           => 'anyof',
    'is-not-defined' => false,
    'param-filters'  => [],
    'text-matches'   => []
  }

  att = reader.parse_attributes
  result['name'] = att['name']

  result['test'] = 'allof' if att['test'] == 'allof'

  elems = reader.parse_inner_tree

  (elems || []).each do |elem|
    case elem['name']
    when "{#{Plugin::NS_CARDDAV}}param-filter"
      result['param-filters'] << elem['value']
    when "{#{Plugin::NS_CARDDAV}}is-not-defined"
      result['is-not-defined'] = true
    when "{#{Plugin::NS_CARDDAV}}text-match"
      match_type = elem['attributes'].key?('match-type') ? elem['attributes']['match-type'] : 'contains'

      fail Dav::Exception::BadRequest, "Unknown match-type: #{match_type}" unless %w(contains equals starts-with ends-with).include?(match_type)

      result['text-matches'] << {
        'negate-condition' => elem['attributes'].key?('negate-condition') && elem['attributes']['negate-condition'] == 'yes',
        'collation'        => elem['attributes'].key?('collation') ? elem['attributes']['collation'] : 'i;unicode-casemap',
        'value'            => elem['value'],
        'match-type'       => match_type
      }
    end
  end

  result
end