Module: Vcard::DirectoryLookup

Included in:
Vcard
Defined in:
app/models/vcard/directory_lookup.rb

Instance Method Summary collapse

Instance Method Details

#address_matchesObject

Same address different names



114
115
116
# File 'app/models/vcard/directory_lookup.rb', line 114

def address_matches
  filtered_matches(:ignore => [:first_name, :family_name], :perfect => [:street, :city])
end

#directory_filter(match, filter = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/vcard/directory_lookup.rb', line 58

def directory_filter(match, filter = {})
  # Exact filters
  filter[:perfect].each do |field|
    return false unless match[:perfect].include?(field)
  end if filter[:perfect]
  filter[:partial].each do |field|
    return false unless match[:partial].include?(field)
  end if filter[:partial]
  filter[:bad].each do |field|
    return false unless match[:bad].include?(field)
  end if filter[:bad]

  # "Better than" filters
  filter[:partial_or_perfect].each do |field|
    return false unless match[:perfect].include?(field) || match[:partial].include?(field)
  end if filter[:partial_or_perfect]

  return true
end

#directory_found?(ignore_fields = []) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/vcard/directory_lookup.rb', line 24

def directory_found?(ignore_fields = [])
  directory_lookup(ignore_fields).present?
end

#directory_lookup(ignore_fields = []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'app/models/vcard/directory_lookup.rb', line 13

def directory_lookup(ignore_fields = [])
  search = map_for_directory

  search.reject!{|key, value| ignore_fields.include? key}

  # TODO:
  # We should fetch additional pages if the result indicates there
  # are more pages.
  ::SwissMatch.directory_service.addresses(search, :per_page => 10)
end

#directory_matches(ignore_lookup_fields = []) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/vcard/directory_lookup.rb', line 36

def directory_matches(ignore_lookup_fields = [])
  matches = directory_lookup(ignore_lookup_fields)
  matches.map do |match|
    search = map_for_directory

    perfect = []
    search.each do |key, value|
      perfect << key if normalize(match.send(key)) == normalize(value)
    end

    partial = []
    search.each do |key, value|
      partial << key if value.present? && normalize(match.send(key)).include?(normalize(value))
    end
    partial -= perfect

    bad = search.keys - perfect - partial - ignore_lookup_fields

    {:address => match, :perfect => perfect, :partial => partial, :bad => bad, :ignore => ignore_lookup_fields}
  end
end

#family_name_match?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/vcard/directory_lookup.rb', line 109

def family_name_match?
  family_name_matches.present?
end

#family_name_matchesObject

Everything but given name matches, family name might be partial



105
106
107
# File 'app/models/vcard/directory_lookup.rb', line 105

def family_name_matches
  filtered_matches(:ignore => [:first_name], :partial_or_perfect => [:family_name], :perfect => [:street, :city])
end

#filtered_matches(filters) ⇒ Object



78
79
80
81
82
83
84
# File 'app/models/vcard/directory_lookup.rb', line 78

def filtered_matches(filters)
  ignores = filters.delete(:ignore) || []

  directory_matches(ignores).collect do |match|
    match[:address] if directory_filter(match, filters)
  end.compact
end

#great_match?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/vcard/directory_lookup.rb', line 100

def great_match?
  great_matches.present?
end

#great_matchesObject

Everything is found, given or family name does is partial match



96
97
98
# File 'app/models/vcard/directory_lookup.rb', line 96

def great_matches
  filtered_matches(:partial_or_perfect => [:family_name, :first_name], :perfect => [:street, :city])
end

#locality_matchesObject

Similar name in same locality



119
120
121
# File 'app/models/vcard/directory_lookup.rb', line 119

def locality_matches
  filtered_matches(:ignore => [:first_name, :street], :partial_or_perfect => [:family_name], :perfect => [:city])
end

#map_for_directoryObject



2
3
4
5
6
7
8
9
10
11
# File 'app/models/vcard/directory_lookup.rb', line 2

def map_for_directory
  search = {}
  search[:first_name] = given_name
  search[:family_name] = family_name
  search[:street] = street_address
  search[:zip_code] = postal_code
  search[:city] = locality

  search
end

#normalize(value) ⇒ Object



28
29
30
31
32
33
34
# File 'app/models/vcard/directory_lookup.rb', line 28

def normalize(value)
  normalized_value = UnicodeUtils.downcase(value.to_s)
  normalized_value.gsub!(/str\./, 'strasse')
  normalized_value.gsub!(/str([ $])/, 'strasse\1')

  normalized_value
end

#perfect_match?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/vcard/directory_lookup.rb', line 91

def perfect_match?
  perfect_matches.present?
end

#perfect_matchesObject

Everything matches



87
88
89
# File 'app/models/vcard/directory_lookup.rb', line 87

def perfect_matches
  filtered_matches(:perfect => [:family_name, :first_name, :street, :city])
end