Class: ContactsTxt

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/contacts_txt.rb

Direct Known Subclasses

ContactsTxtAgent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src = nil, fields: %w(role organisation mobile sms email dob tags address notes note mobile2 ), username: nil, password: nil, debug: false) ⇒ ContactsTxt

Returns a new instance of ContactsTxt.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/contacts_txt.rb', line 14

def initialize(src=nil, fields: %w(role organisation mobile
               sms email dob tags address notes note mobile2 ),
               username: nil, password: nil, debug: false)

  @debug = debug
  @fields  = %w(fullname firstname lastname tel) | fields

  txt, type = if src then
    RXFReader.read(src, username: username, password: password)
  else
    ['', :unknown]
  end

  case type
  when :file
    @path, @filename =  File.dirname(src), File.basename(src)
  when :url
    @path, @filename = '.', File.basename(src)
  when :dfs
    @path, @filename =  File.dirname(src), File.basename(src)
  when :unknown
    @path, @filename = '.', 'contacts.txt'
  end

  @dx = txt.lines.length > 1 ? import_to_dx(txt) : new_dx()

end

Instance Attribute Details

#to_sObject (readonly)

Returns the value of attribute to_s.



12
13
14
# File 'lib/contacts_txt.rb', line 12

def to_s
  @to_s
end

Instance Method Details

#allObject



42
43
44
# File 'lib/contacts_txt.rb', line 42

def all()
  @dx.all
end

#dxObject



46
47
48
# File 'lib/contacts_txt.rb', line 46

def dx()
  @dx
end

#email_listObject

returns a Dynarex object



116
117
118
# File 'lib/contacts_txt.rb', line 116

def email_list()
  @dx.filter {|x| x.email.length > 0}
end

#find_by_id(id) ⇒ Object



50
51
52
53
54
# File 'lib/contacts_txt.rb', line 50

def find_by_id(id)

  @dx.find_by_id id

end

#find_by_mobile(raw_number, countrycode = '44') ⇒ Object



56
57
58
59
60
61
62
# File 'lib/contacts_txt.rb', line 56

def find_by_mobile(raw_number, countrycode='44')

  number = Regexp.new raw_number.sub(/^(?:0|#{countrycode})/,'').gsub(/[ -]*/,'')

  @dx.all.find {|x| x.mobile.gsub(/[ -]*/,'') =~ number }

end

#find_by_name(s) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/contacts_txt.rb', line 64

def find_by_name(s)

  # Appending a hashtag to the name can help with finding the specific record
  # e.g. 'Peter#plumber' or 'Peter #plumber'

  raw_name, tag = s.split('#',2).map(&:strip)

  name = Regexp.new "\b#{raw_name}\b|#{raw_name}",  Regexp::IGNORECASE
  puts 'name: ' + name.inspect if @debug

  a = @dx.all.select do |x|
    x.fullname =~ name or x.firstname =~ name or x.lastname =~ name
  end

  if tag then
    a.find {|x| x.tags.split.map(&:downcase).include? tag.downcase }
  else
    a
  end

end

#find_by_sms(raw_number, countrycode = '44') ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/contacts_txt.rb', line 86

def find_by_sms(raw_number, countrycode='44')

  number = Regexp.new raw_number\
      .sub(/^(?:0|#{countrycode})/,'').gsub(/[ -]*/,'')

  @dx.all.find {|x| x.sms.gsub(/[ -]*/,'') =~ number \
                or x.mobile.gsub(/[ -]*/,'') =~ number }

end

#find_by_telno(raw_number) ⇒ Object

find using the tel, mobile, or mobile2 fields



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/contacts_txt.rb', line 98

def find_by_telno(raw_number)

  number = Regexp.new raw_number.gsub(/[ -]*/,'')

  @dx.all.find do |x|

    numbers = %i(tel mobile mobile2).map do |y|
      x.method(y).call.gsub(/[ -]*/,'') if x.respond_to? y
    end

    puts 'numbers: ' + numbers.inspect if @debug
    numbers.grep(number).any?
  end

end

#list_namesObject



120
121
122
123
124
125
126
# File 'lib/contacts_txt.rb', line 120

def list_names()

  @dx.all.inject([]) do |r, x|
    x.fullname.length >= 1 ? r << x.fullname : r
  end

end

#mobile_listObject



128
129
130
# File 'lib/contacts_txt.rb', line 128

def mobile_list()
  @dx.filter {|x| x.mobile.length > 0}
end

#multi_tel_indexObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/contacts_txt.rb', line 132

def multi_tel_index()

  a = @dx.all.map do |x|

    tel = %i(tel mobile mobile2).detect do |name|
      !x.method(name).call.empty?
    end
    next unless tel
    "%s %s" % [x.fullname, x.method(tel).call]
  end.compact


  # group by first name
  r = a.group_by {|x| x[0]}

  a2 = a.clone

  # group by last name
  r2 = a.group_by {|x| x.split(/ /,2).last[0]}
  c = r2.merge(r)

  c.each do |k, v|

    puts "k: %s v: %s" % [k, v]
    v.concat(r2[k]) if r2[k]

  end

  h = c.sort.each {|k,v| v.uniq!}.to_h

  out = []

  h.each do |k,v|

    out << ' ' + (' ' * 30) + k

    v.each do |x|

      name, phone = x.split(/(?=\d)/,2)
      out << "\n" + (name.length >= 29 ? name[0..26] + '...' : name)
      tel = (' ' + ' ' * (26 - phone.length)) + 't: ' + phone
      out <<  tel + "\n"
      out << ('-' * 30)

    end

  end

  puts out.join("\n")

end

#save(filename = @filename) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/contacts_txt.rb', line 184

def save(filename=@filename)

  s = dx_to_s(@dx)
  FileX.write File.join(@path, filename), s
  @dx.save File.join(@path, filename.sub(/\.txt$/,'.xml'))

end

#to_dxObject



192
193
194
# File 'lib/contacts_txt.rb', line 192

def to_dx()
  @dx
end