Class: Cap::Vivo::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/cap/vivo/mapper.rb

Constant Summary collapse

HAS_CONTACT_INFO =
RDF::URI.parse 'http://purl.obolibrary.org/obo/ARG_2000028'
PROFILE_URI_PREFIX =
'https://profiles.stanford.edu/vivo'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ Mapper

Returns a new instance of Mapper.

Parameters:

  • profile (JSON)

    CAP API profile - json object



19
20
21
22
23
24
25
26
# File 'lib/cap/vivo/mapper.rb', line 19

def initialize(profile)
  @config = Cap::Vivo.configuration
  @profile = profile
  @id = profile['profileId']
  @uri = RDF::URI.parse "#{PROFILE_URI_PREFIX}/#{@id}"
  @rdf = RDF::Graph.new
  @rdf << [@uri, RDF.type, RDF::FOAF.Person]
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/cap/vivo/mapper.rb', line 10

def config
  @config
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/cap/vivo/mapper.rb', line 14

def id
  @id
end

#profileObject

profile



13
14
15
# File 'lib/cap/vivo/mapper.rb', line 13

def profile
  @profile
end

#rdfObject (readonly)

Returns the value of attribute rdf.



16
17
18
# File 'lib/cap/vivo/mapper.rb', line 16

def rdf
  @rdf
end

#uriObject (readonly)

Returns the value of attribute uri.



15
16
17
# File 'lib/cap/vivo/mapper.rb', line 15

def uri
  @uri
end

Instance Method Details

#create_vcardObject

Map names, addresses, contacts and web links to vcard data.



34
35
36
37
38
39
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
98
99
100
101
102
103
# File 'lib/cap/vivo/mapper.rb', line 34

def create_vcard
  vcard = RDF::Node.new
  @rdf << [@uri, HAS_CONTACT_INFO, vcard]
  @rdf << [vcard, RDF.type, RDF::Vocab::VCARD.Individual]
  # Names
  profile['names'].each_pair do |type, name|
    given_name  = name['firstName']
    family_name = name['lastName']
    vcard_name = RDF::Node.new
    @rdf << [vcard, RDF::Vocab::VCARD.hasName, vcard_name]
    @rdf << [vcard_name, RDF.type, RDF::Vocab::VCARD.Name]
    @rdf << [vcard_name, RDF::RDFS.label, type]
    @rdf << [vcard_name, RDF::Vocab::VCARD.send('given-name'), given_name]
    @rdf << [vcard_name, RDF::Vocab::VCARD.send('family-name'), family_name]
  end
  # Addresses
  offices = profile['academicOffices'] || []
  offices.each do |office|
    vcard_address = RDF::Node.new
    @rdf << [vcard, RDF::Vocab::VCARD.hasAddress, vcard_address]
    @rdf << [vcard_address, RDF.type, RDF::Vocab::VCARD.Address]
    @rdf << [vcard_address, RDF.type, RDF::Vocab::VCARD.Work]
    @rdf << [vcard_address, RDF::RDFS.label, office['type']]
    country = office['country'] || 'United States'
    @rdf << [vcard_address, RDF::Vocab::VCARD.send('country-name'), country]
    @rdf << [vcard_address, RDF::Vocab::VCARD.region, office['state']]
    @rdf << [vcard_address, RDF::Vocab::VCARD.locality, office['city']]
    @rdf << [vcard_address, RDF::Vocab::VCARD.send('postal-code'), office['zip']]
    @rdf << [vcard_address, RDF::Vocab::VCARD.send('street-address'), office['address']]
  end
  # Contacts (email and telephone)
  contacts = []
  contacts.push profile['primaryContact']
  contacts.push profile['alternateContact']
  contacts.compact!
  contacts.each do |contact|
    unless contact['email'].nil?
      email = RDF::URI.parse("mailto:#{contact['email']}")
      label = contact['type'] || contact['label'] || ''
      vcard_email = RDF::Node.new
      @rdf << [vcard, RDF::Vocab::VCARD.hasEmail, vcard_email]
      @rdf << [vcard_email, RDF.type, RDF::Vocab::VCARD.Email]
      @rdf << [vcard_email, RDF.type, RDF::Vocab::VCARD.Work]
      @rdf << [vcard_email, RDF::Vocab::VCARD.hasValue, email]
      @rdf << [vcard_email, RDF::RDFS.label, label]
    end
    phone_numbers = contact['phoneNumbers'] || []
    phone_numbers.each do |phone|
      number = phone.gsub(/\s+|[()+-]/,'')
      tel = "tel:#{number}"
      vcard_phone = RDF::Node.new
      @rdf << [vcard, RDF::Vocab::VCARD.hasTelephone, vcard_phone]
      @rdf << [vcard_phone, RDF.type, RDF::Vocab::VCARD.Tel]
      @rdf << [vcard_phone, RDF.type, RDF::Vocab::VCARD.Voice]
      @rdf << [vcard_phone, RDF.type, RDF::Vocab::VCARD.Work]
      @rdf << [vcard_phone, RDF::Vocab::VCARD.hasValue, tel]
    end
  end
  # Web Links
  profile['meta']['links'].each do |link|
    if link['rel'] == 'https://cap.stanford.edu/rel/public'
      url = RDF::URI.parse link['href']
      vcard_url = RDF::Node.new
      @rdf << [vcard, RDF::Vocab::VCARD.hasURL, vcard_url]
      @rdf << [vcard_url, RDF::Vocab::VCARD.hasValue, url]
      @rdf << [vcard_url, RDF::RDFS.label, "CAP public profile"]
      @rdf << [vcard_url, RDF.type, RDF::Vocab::VCARD.Work]
    end
  end
end

#create_vivoObject

Convert CAP profile into VIVO linked data



29
30
31
# File 'lib/cap/vivo/mapper.rb', line 29

def create_vivo
  create_vcard
end

#provObject



109
110
111
# File 'lib/cap/vivo/mapper.rb', line 109

def prov
  now = RDF::Literal.new(Time.now.utc, :datatype => RDF::XSD.dateTime)
end

#saveObject



105
106
107
# File 'lib/cap/vivo/mapper.rb', line 105

def save
  @rdf.each_statement {|s| @config.rdf_repo.insert_statement s};
end

#to_jsonldObject



113
114
115
# File 'lib/cap/vivo/mapper.rb', line 113

def to_jsonld
  @rdf.dump(:jsonld, standard_prefixes: true)
end

#to_ttlObject



117
118
119
# File 'lib/cap/vivo/mapper.rb', line 117

def to_ttl
  @rdf.to_ttl
end