Class: ContactSport::VcardReader

Inherits:
Object
  • Object
show all
Defined in:
lib/contact_sport/vcard_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ VcardReader

Returns a new instance of VcardReader.



9
10
11
# File 'lib/contact_sport/vcard_reader.rb', line 9

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/contact_sport/vcard_reader.rb', line 7

def file
  @file
end

Instance Method Details

#contactsObject



13
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/contact_sport/vcard_reader.rb', line 13

def contacts
  begin
    @contacts ||= Vcard::Vcard.decode(contents).map do |card|
      Contact.new(
        first_name:    card.name.given,
        last_name:     card.name.family,
        name:          card.name.fullname,

        email:         email(card),
        url:           url(card),

        company:       company(card),
        office_phone:  work_phone(card),
        mobile_phone:  mobile_phone(card),
        fax:           fax(card),

        address1:      address_first_line(card),
        address2:      address_second_line(card),
        city:          address(:locality, card),
        region:        address(:region, card),
        postcode:      address(:postalcode, card),
        country:       address(:country, card)
      )
    end
  rescue Vcard::InvalidEncodingError, Vcard::UnsupportedError, Vcard::Unencodeable => e
    raise ContactSport::FormatError, e.message
  end
end