# datacite-mapping
[![Build Status](https://travis-ci.org/CDLUC3/datacite-mapping.png?branch=master)](https://travis-ci.org/CDLUC3/datacite-mapping)
[![Code Climate](https://codeclimate.com/github/CDLUC3/datacite-mapping.png)](https://codeclimate.com/github/CDLUC3/datacite-mapping)
[![Inline docs](http://inch-ci.org/github/CDLUC3/datacite-mapping.png)](http://inch-ci.org/github/CDLUC3/datacite-mapping)
[![Gem Version](https://img.shields.io/gem/v/datacite-mapping.svg)](https://github.com/CDLUC3/datacite-mapping/releases)
A library for mapping [DataCite XML](http://schema.datacite.org/meta/kernel-4/) to Ruby objects,
based on [xml-mapping](http://multi-io.github.io/xml-mapping/) and
[xml-mapping_extensions](https://github.com/dmolesUC3/xml-mapping_extensions).
Full API documentation on [RubyDoc.info](http://www.rubydoc.info/github/CDLUC3/datacite-mapping/master/frames).
Supports [Datacite 4.3](https://schema.labs.datacite.org/meta/kernel-4.3/); backward-compatible with
[Datacite 3.1](https://schema.labs.datacite.org/meta/kernel-3/).
Note that although this gem maintains compatibility with multiple
versions of DataCite XML, changes to DataCite XML sometimes force
changes to the internal object model of the gem. So different versions
of this gem may require minor updates to how a part of the model is accessed.
## Usage
The core of the Datacite::Mapping library is the `Resource` class, corresponding to the root `` element
in a Datacite document.
### Reading
To create a `Resource` object from XML file, use `Resource.parse_xml` or `Resource.load_from_file`,
depending on the data source:
| XML source | Method to use |
| ----------------- | ------------------------- |
| file path | `Resource.load_from_file` |
| `String` | `Resource.parse_xml` |
| `IO` | `Resource.parse_xml` |
| `REXML::Document` | `Resource.parse_xml` |
| `REXML::Element` | `Resource.parse_xml` |
Example:
```ruby
require 'datacite/mapping'
include Datacite::Mapping
resource = Resource.load_from_file('datacite-example-full-v4.3.xml')
# => # # "XML example of all DataCite Metadata Schema v4.3 properties."
```
Note that Datacite::Mapping uses the [TypesafeEnum](https://github.com/dmolesUC3/typesafe_enum) gem to represent controlled
vocabularies such as [ResourceTypeGeneral](http://www.rubydoc.info/github/CDLUC3/datacite-mapping/master/Datacite/Mapping/ResourceTypeGeneral)
and [DescriptionType](http://www.rubydoc.info/github/CDLUC3/datacite-mapping/master/Datacite/Mapping/DescriptionType).
### Writing
In general, a `Resource` object must be provided with all required attributes on initialization.
```ruby
resource = Resource.new(
identifier: Identifier.new(value: '10.5555/12345678'),
creators: [
Creator.new(
name: 'Josiah Carberry',
identifier: NameIdentifier.new(
scheme: 'ORCID',
scheme_uri: URI('http://orcid.org/'),
value: '0000-0002-1825-0097'
),
affiliations: [
'Department of Psychoceramics, Brown University'
]
)
],
titles: [
Title.new(value: 'Toward a Unified Theory of High-Energy Metaphysics: Silly String Theory')
],
publisher: 'Journal of Psychoceramics',
publication_year: 2008
)
# => # " " "