Class: VCardParser::VCard
- Inherits:
-
Object
- Object
- VCardParser::VCard
- Defined in:
- lib/vcard_parser/vcard.rb
Constant Summary collapse
- VCARD_FORMAT =
/ BEGIN:VCARD\s+ (.+?) END:VCARD\s* /xm
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key, group = nil) ⇒ Object
- #add_field(*args) ⇒ Object
- #add_field_from_string(line) ⇒ Object
- #each_field(&block) ⇒ Object
-
#initialize(version, fields = []) ⇒ VCard
constructor
A new instance of VCard.
- #values(key, group = nil) ⇒ Object
- #vcard(wanted_fields = []) ⇒ Object (also: #to_s)
Constructor Details
#initialize(version, fields = []) ⇒ VCard
Returns a new instance of VCard.
16 17 18 19 |
# File 'lib/vcard_parser/vcard.rb', line 16 def initialize(version, fields = []) @version = version @fields = fields end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
8 9 10 |
# File 'lib/vcard_parser/vcard.rb', line 8 def fields @fields end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
8 9 10 |
# File 'lib/vcard_parser/vcard.rb', line 8 def version @version end |
Class Method Details
.parse(data) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vcard_parser/vcard.rb', line 21 def self.parse(data) data.gsub!(/\r?\n /, "") # inline base64 data to not break parser data.scan(VCARD_FORMAT).map do |vcard_data| # find the version to choose the correct parser lines = vcard_data[0].each_line.map(&:strip) version_line_index = lines.index{|str| str.start_with?('VERSION:') } unless version_line_index raise MalformedInput, "Unable to find VERSION field" end # remove version field version_line = lines.delete_at(version_line_index) key, version = version_line.split(':') new(version).tap do |card| lines.reject{|str| str.strip.empty? }.each do |line| card.add_field_from_string(line) end end end end |
Instance Method Details
#[](key, group = nil) ⇒ Object
64 65 66 67 |
# File 'lib/vcard_parser/vcard.rb', line 64 def [](key, group = nil) v = values(key, group) v.empty? ? nil : v[0] end |
#add_field(*args) ⇒ Object
52 53 54 55 |
# File 'lib/vcard_parser/vcard.rb', line 52 def add_field(*args) f = field_class.new(*args) @fields << f end |
#add_field_from_string(line) ⇒ Object
47 48 49 50 |
# File 'lib/vcard_parser/vcard.rb', line 47 def add_field_from_string(line) f = field_class.parse(line) @fields << f end |
#each_field(&block) ⇒ Object
69 70 71 |
# File 'lib/vcard_parser/vcard.rb', line 69 def each_field(&block) @fields.each(&block) end |
#values(key, group = nil) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/vcard_parser/vcard.rb', line 57 def values(key, group = nil) @fields.select do |f| (f.name == key) && (!group || (f.group == group)) end end |
#vcard(wanted_fields = []) ⇒ Object Also known as: to_s
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vcard_parser/vcard.rb', line 73 def vcard(wanted_fields = []) ret = ["BEGIN:VCARD"] if wanted_fields.empty? || wanted_fields.include?('VERSION') ret << "VERSION:#{@version}" end @fields.each do |f| if wanted_fields.empty? || wanted_fields.include?(f.name) ret << f.to_s end end ret << "END:VCARD\n" ret.join("\n") end |