Class: Middleman::VCard::Generator
- Inherits:
-
Object
- Object
- Middleman::VCard::Generator
- Includes:
- Contracts
- Defined in:
- lib/middleman-vcard/generator.rb
Instance Method Summary collapse
-
#error(msg) ⇒ Object
protected
Log the provided error message and raise an error.
-
#format_msg(msg) ⇒ Object
protected
Format the provided message.
-
#generate(destination_path) ⇒ Object
Generate the VCard, storing the result in
destination_path
. -
#info(msg) ⇒ Object
protected
Log the provided informative message.
-
#initialize(name, emails: [], phones: [], addresses: [], photo: nil, logger: nil) ⇒ Generator
constructor
Create a new VCard generator.
Constructor Details
#initialize(name, emails: [], phones: [], addresses: [], photo: nil, logger: nil) ⇒ Generator
Create a new VCard generator.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/middleman-vcard/generator.rb', line 40 def initialize(name, # 1 emails: [], # 2 phones: [], # 3 addresses: [], # 4 photo: nil, # 5 logger: nil) # 6 @logger = logger || Logger.new($stdout) @card = Vpim::Vcard::Maker.make2 do |maker| maker.add_name do |n| n.given = name end emails.each do |email| maker.add_email(email[:email]) do |e| e.preferred = email[:preferred] || false e.location = email[:location] if email[:location] end end phones.each do |phone| maker.add_tel(phone[:number]) do |t| t.preferred = phone[:preferred] || false t.location = phone[:location] if phone[:location] t.capability = phone[:capability] if phone[:capability] end end addresses.each do |address| maker.add_addr do |a| a.preferred = address[:preferred] || false a.location = address[:location] if address[:location] a.street = address[:street] if address[:street] a.postalcode = address[:postalcode] if address[:postalcode] a.locality = address[:locality] if address[:locality] a.region = address[:region] if address[:region] a.country = address[:country] if address[:country] end end if photo # Normalize `photo[:path]`. photo[:path] = File.(photo[:path]) # Check correctness of `photo[:path]`. unless File.file?(photo[:path]) error("Invalid photo path: `#{photo[:path]}`.") end # Add photo informations to the VCard `maker`. maker.add_photo do |p| p.image = File.read(photo[:path]) p.type = photo[:type] end end end end |
Instance Method Details
#error(msg) ⇒ Object (protected)
Log the provided error message and raise an error.
120 121 122 123 |
# File 'lib/middleman-vcard/generator.rb', line 120 def error(msg) @logger.error(format_msg(msg)) fail("Middleman VCard generator failed") end |
#format_msg(msg) ⇒ Object (protected)
Format the provided message.
141 142 143 |
# File 'lib/middleman-vcard/generator.rb', line 141 def format_msg(msg) "[VCard] #{msg}" end |
#generate(destination_path) ⇒ Object
Generate the VCard, storing the result in destination_path
.
103 104 105 106 107 108 109 110 111 |
# File 'lib/middleman-vcard/generator.rb', line 103 def generate(destination_path) begin File.open(destination_path, "w") { |file| file.write(@card) } rescue StandardError => err error(err.msg) else info("Successfully generated VCard to `#{destination_path}`.") end end |
#info(msg) ⇒ Object (protected)
Log the provided informative message.
130 131 132 |
# File 'lib/middleman-vcard/generator.rb', line 130 def info(msg) @logger.info(format_msg(msg)) end |