Class: TreeCard

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

Defined Under Namespace

Classes: Attribute, ContentLineNode, Param, ParamNode, ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TreeCard

Returns a new instance of TreeCard.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/treecard.rb', line 19

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.



17
18
19
# File 'lib/treecard.rb', line 17

def attributes
  @attributes
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



16
17
18
# File 'lib/treecard.rb', line 16

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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/treecard.rb', line 78

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



58
59
60
61
62
# File 'lib/treecard.rb', line 58

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.



101
102
103
104
105
# File 'lib/treecard.rb', line 101

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



64
65
66
67
68
# File 'lib/treecard.rb', line 64

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

#emailsObject



37
38
39
40
# File 'lib/treecard.rb', line 37

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

#faxObject



47
48
49
50
# File 'lib/treecard.rb', line 47

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

#nameObject



31
32
33
34
35
# File 'lib/treecard.rb', line 31

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

#phoneObject



42
43
44
45
# File 'lib/treecard.rb', line 42

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

#photoObject



52
53
54
55
56
# File 'lib/treecard.rb', line 52

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

#urlObject



70
71
72
73
74
# File 'lib/treecard.rb', line 70

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