contacts_txt 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1bfbcc863a75bd8144abe428442d84759fb9ed61
4
- data.tar.gz: f38289dd6ae53b64723e845a29f9599ec431cd23
3
+ metadata.gz: 36a091366cefe47c3b1f63b8e884266504b6cb2e
4
+ data.tar.gz: 87e0c7b7ca9357aa61eab4ead1fd703fc6f649a1
5
5
  SHA512:
6
- metadata.gz: 5fa29dd794ce47737787aa0dfa1c54ab9256847d4736e614ca80bfe9c049e0144617b52551d3df92b1376b2a7a902f13f4735a1aa7ed82b5909be37fdb88350e
7
- data.tar.gz: b925502e458d9c0682903d9f429cdd388f2423846bbfe1d2fe80791d3d165004dbe4dac6e8cf9232c19278537282cffcd4a79a77aab3756c2f58e880234272de
6
+ metadata.gz: 1dccaa1c491cc9a1d586d74a3c32b849b6e4f94dcbb126b6a1118ea0c7fdc728e4edaafad93aeaf7bdc5c9e1528dee089904b7e5be51d4ada102de92324a6268
7
+ data.tar.gz: 5b5a9da840d7c2c0c5c0766954a83a5d724ec33956f6beca927eb31f4cc6add131d3f8395810c3bea9211da7e01178b1a443bc1f83ae905300863c641cb16b60
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/contacts_txt.rb CHANGED
@@ -9,16 +9,27 @@ class ContactsTxt
9
9
 
10
10
  attr_reader :to_s
11
11
 
12
- def initialize(raw_filename='contacts.txt', path: File.dirname(raw_filename), \
13
- fields: %w(mobile email dob tags address notes))
14
- @fields = %w(fullname firstname lastname tel) | fields
12
+ def initialize(src=nil, fields: %w(mobile email dob tags address notes),
13
+ username: nil, password: nil)
15
14
 
16
- @path = path
17
- @filename = path == '.' ? raw_filename : File.basename(raw_filename)
15
+ @fields = %w(fullname firstname lastname tel) | fields
18
16
 
19
- fpath = File.join(path, @filename)
17
+ txt, type = if src then
18
+ RXFHelper.read(src, username: username, password: password)
19
+ else
20
+ ['', :unknown]
21
+ end
20
22
 
21
- @dx = File.exists?(fpath) ? import_to_dx(File.read(fpath)) : new_dx()
23
+ case type
24
+ when :file
25
+ @path, @filename = File.dirname(src), File.basename(src)
26
+ when :url
27
+ @path, @filename = '.', File.basename(src)
28
+ when :unknown
29
+ @path, @filename = '.', 'contacts.txt'
30
+ end
31
+
32
+ @dx = txt.lines.length > 1 ? import_to_dx(txt) : new_dx()
22
33
 
23
34
  end
24
35
 
@@ -50,7 +61,7 @@ class ContactsTxt
50
61
 
51
62
  private
52
63
 
53
- def dx_to_s(dx, title: File.basename(@filename))
64
+ def dx_to_s(dx)
54
65
 
55
66
  rows = dx.all.map do |row|
56
67
 
@@ -64,16 +75,43 @@ class ContactsTxt
64
75
  ([fullname] + a.map {|x| x.join(': ') }).join("\n")
65
76
  end
66
77
 
67
- "%s\n%s\n\n%s" % [title, '=' * title.length, rows.join("\n\n")]
78
+ "<?contacts fields='%s'?>\n\n%s" % [@fields, rows.join("\n\n")]
68
79
 
69
80
  end
70
81
 
71
82
  def import_to_dx(raw_s)
72
83
 
73
- s = raw_s.lstrip.lines[2..-1].join.strip.\
74
- split(/\s+(?=^[\w\s]+$)/).map {|x| 'fullname: ' + x }.join("\n")
84
+ s = if raw_s =~ /<?contacts / then
85
+
86
+ raw_contacts = raw_s.clone
87
+ s2 = raw_contacts.slice!(/<\?contacts [^>]+\?>/)
88
+
89
+ attributes = %w(fields delimiter id).inject({}) do |r, keyword|
90
+ found = s2[/(?<=#{keyword}=['"])[^'"]+/]
91
+ found ? r.merge(keyword.to_sym => found) : r
92
+ end
93
+
94
+ h = {
95
+ fields: @fields.join(', '),
96
+ }.merge attributes
97
+
98
+ @fields = h[:fields].split(/ *, */)
99
+
100
+ if h[:root] then
101
+ "\n\n" + h[:root] + "\n" +
102
+ raw_contacts.strip.lines.map {|line| ' ' + line}.join
103
+ else
104
+ raw_contacts
105
+ end
106
+
107
+ else
108
+
109
+ raw_s.lstrip.lines[2..-1].join.strip
110
+
111
+ end
75
112
 
76
- new_dx().import "--+\n" + s
113
+ new_dx().import "--+\n" + s.split(/\s+(?=^[\w\s]+$)/)\
114
+ .map {|x| 'fullname: ' + x }.join("\n")
77
115
 
78
116
  end
79
117
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contacts_txt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  Ebw/4t8+mfs6ivuW6xdj3SlqhEXlfJ83mZuvJ3LtQteR5p0R8DSLcTXMkOrWD6wC
32
32
  07GXbdChvjqXQg==
33
33
  -----END CERTIFICATE-----
34
- date: 2017-06-16 00:00:00.000000000 Z
34
+ date: 2017-09-25 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: dynarex
@@ -42,7 +42,7 @@ dependencies:
42
42
  version: '1.7'
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- version: 1.7.17
45
+ version: 1.7.26
46
46
  type: :runtime
47
47
  prerelease: false
48
48
  version_requirements: !ruby/object:Gem::Requirement
@@ -52,7 +52,7 @@ dependencies:
52
52
  version: '1.7'
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
- version: 1.7.17
55
+ version: 1.7.26
56
56
  description:
57
57
  email: james@jamesrobertson.eu
58
58
  executables: []
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 2.6.8
83
+ rubygems_version: 2.6.13
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: Reads a contacts.txt file
metadata.gz.sig CHANGED
Binary file