Module: TwitterFriends::TwitterRdf

Defined in:
lib/wuclan/rdf_output/twitter_rdf.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.encode_datetime(dt) ⇒ Object

RDF-formatted date



7
8
9
10
11
12
13
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 7

def self.encode_datetime dt
  begin
    DateTime.parse(dt).to_s
  rescue ArgumentError => e
    nil
  end
end

.rdf_triple(subj, pred, obj, comment = nil) ⇒ Object

RDF Triple string for the given (subject, object, predicate)

http://www.w3.org/TR/rdf-testcases/#ntriples


47
48
49
50
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 47

def self.rdf_triple subj, pred, obj, comment=nil
  comment = "\t# " + comment.to_s unless comment.blank?
  %Q{%-55s\t%-39s\t%-23s\t.%s} % [subj, pred, obj, comment]
end

Instance Method Details

#mutable?(attr) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 52

def mutable?(attr)
  false
end

#rdf_component(val, type) ⇒ Object

Emit a component (subject or object) with the right semantic encoding

Use :boolskip if a false property should just be left out.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 20

def rdf_component val, type
  case type
  when :tweet         then %Q{<http://twitter.com/statuses/show/#{val}.xml>}
  when :user          then %Q{<http://twitter.com/users/show/#{val}.xml>}
  when :bool          then ((!val) || (val==0) || (val=="0")) ? '"false"^^<xsd:boolean>' : '"true"^^<xsd:boolean>'
  when :boolskip      then ((!val) || (val==0) || (val=="0")) ? nil                      : '"true"^^<xsd:boolean>'
  when :int           then %Q{"#{val.to_i}"^^<xsd:integer>}
  when :date          then %Q{"#{TwitterRdf.encode_datetime(val)}"^^<xsd:dateTime>}
  when :str           then %Q{"#{val}"}
  else raise "Don't know how to encode #{type}"
  end
end

#rdf_pred(pred) ⇒ Object

Express relationship (predicate) in RDF



36
37
38
39
40
41
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 36

def rdf_pred pred
  case pred
  when :created_at  then %Q{<http://twitter.com/##{pred}>}
  else                   %Q{<http://twitter.com/##{pred}>}
  end
end

#to_rdf3Object

Convert an object to an rdf triple.

Appends scraped at to #mutable? attributes



77
78
79
80
81
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 77

def to_rdf3
  to_rdf3_tuples.map do |tuple|
    self.class.rdf_triple tuple
  end.join("\n")
end

#to_rdf3_tuplesObject

Extract [subject, predicate, object, (extra)] tuples.

(extra) is set to scraped at for #mutable? attributes, blank otherwise.



61
62
63
64
65
66
67
68
69
70
# File 'lib/wuclan/rdf_output/twitter_rdf.rb', line 61

def to_rdf3_tuples
  members_with_types.map do |attr, type|
    next if self[attr].blank?
    subj    = rdf_resource
    pred    = rdf_pred(attr)
    obj     = rdf_component(self[attr], type) or next
    comment = scraped_at if mutable?(attr)
    [subj, pred, obj, comment]
  end.compact
end