Class: TreeCard

Inherits:
Object
  • Object
show all
Defined in:
lib/treecard.rb,
lib/treecard.rb

Defined Under Namespace

Classes: Attribute, ContentLineNode, Param, ParamNode

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TreeCard

Returns a new instance of TreeCard.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/treecard.rb', line 16

def initialize(data)
  @attributes = Hash.new {|h, k| h[k] = []}
  @raw_data = self.class.unfold_lines(data)
  @parser = VCardGrammarParser.new
  ast = @parser.parse(@raw_data)
  if ast
    parse(ast)
  else
    raise TreeCard::ParseError, @parser.failure_reason
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



14
15
16
# File 'lib/treecard.rb', line 14

def attributes
  @attributes
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



13
14
15
# File 'lib/treecard.rb', line 13

def raw_data
  @raw_data
end

Class Method Details

.unfold_lines(data) ⇒ Object

VCards automatically wrap lines longer than 75 characters. Wrapped lines are signified by leading whitespace.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/treecard.rb', line 69

def self.unfold_lines(data)
  unfolded_data = ""
  current_line = ""
  data.lines.each do |line|
    if line =~ /^\s\S+/
      current_line << line[1..-1].chomp
    else
      if current_line != ""
        if current_line =~ /=0D=0A=$/ #weird quoted printable thing
          unfolded_data << current_line.chomp
        else
          unfolded_data << current_line + "\n"
        end
      end
      current_line = line.chomp
    end
  end
  unfolded_data << current_line + "\n"
  unfolded_data
end

Instance Method Details

#addressObject



55
56
57
58
59
# File 'lib/treecard.rb', line 55

def address
  attr = attributes['adr']
  attr &&= attr.first
  attr.value.split(';').join("\n").strip if attr
end

#attribute_with_param(attr_name, param_name) ⇒ Object

Returns the first attribute with attr_name where param_name is set.



92
93
94
95
96
# File 'lib/treecard.rb', line 92

def attribute_with_param(attr_name, param_name)
  attributes = self.attributes[attr_name.downcase]
  found_attributes = attributes.select { |attribute| attribute.params[param_name.downcase] }
  found_attributes.first
end

#companyObject



61
62
63
64
65
# File 'lib/treecard.rb', line 61

def company
  attr = attributes['org']
  attr &&= attr.first
  attr.value if attr
end

#emailsObject



34
35
36
37
# File 'lib/treecard.rb', line 34

def emails
  attr = attributes['email']
  attr.map {|email| email.value} if attr
end

#faxObject



44
45
46
47
# File 'lib/treecard.rb', line 44

def fax
  attr = attribute_with_param('tel', 'fax')
  attr.value if attr
end

#nameObject



28
29
30
31
32
# File 'lib/treecard.rb', line 28

def name
  attr = attributes['fn']
  attr &&= attr.first
  attr.value if attr
end

#phoneObject



39
40
41
42
# File 'lib/treecard.rb', line 39

def phone
  attr = attribute_with_param('tel', 'voice')
  attr.value if attr
end

#photoObject



49
50
51
52
53
# File 'lib/treecard.rb', line 49

def photo
  attr = attributes['photo']
  attr &&= attr.first
  attr.value if attr
end