Class: XFN

Inherits:
Microformat show all
Defined in:
lib/mofo/xfn.rb

Defined Under Namespace

Classes: Link

Constant Summary collapse

@@valid_relations =
%w( 
  contact
  acquaintance
  friend
  met
  co-worker
  colleague
  co-resident
  neighbor
  child
  parent
  sibling
  spouse
  kin
  muse
  crush
  date
  sweetheart
  me
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Microformat::Base

#find, #find_in_children, #timeout=

Constructor Details

#initialize(doc) ⇒ XFN

Returns a new instance of XFN.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mofo/xfn.rb', line 57

def initialize(doc)
  @links = doc.search("a[@rel]").map do |rl| 
    relation = rl[:rel].split(' ') 

    # prune invalid relations
    relation.each { |r| relation.delete(r) unless @@valid_relations.include? r }
    relation = relation.first if relation.size == 1
    next if relation.empty?

    Link.new(:name => rl.innerHTML, :link => rl[:href], :relation => relation)
  end.compact
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mofo/xfn.rb', line 81

def method_missing(method, *args, &block)
  method = method.to_s
  if (rels = method.split(/_and_/)).size > 1
    self[*rels]
  elsif @links.class.public_instance_methods.include? method
    @links.send(method, *args, &block)
  else
    check = args.first == true ? :== : :has?
    @links.select { |link| link.relation.send(check, method) }.first_or_self
  end
end

Instance Attribute Details

Returns the value of attribute links.



43
44
45
# File 'lib/mofo/xfn.rb', line 43

def links
  @links
end

Class Method Details

.find_occurences(doc) ⇒ Object Also known as: find_first, find_every



45
46
47
48
49
50
# File 'lib/mofo/xfn.rb', line 45

def self.find_occurences(doc)
  case doc
  when Hpricot::Doc then @occurences = XFN.new(doc)
  else @occurences 
  end
end

Instance Method Details

#[](*rels) ⇒ Object



74
75
76
77
78
79
# File 'lib/mofo/xfn.rb', line 74

def [](*rels)
  @links.select do |link| 
    relation = link.relation
    relation.respond_to?(:all?) && rels.all? { |rel| relation.include? rel }
  end.first_or_self
end

#relationsObject



70
71
72
# File 'lib/mofo/xfn.rb', line 70

def relations
  @relations ||= @links.map { |l| l.relation }
end