-
Notifications
You must be signed in to change notification settings - Fork 6
Converting data
levent edited this page May 25, 2012
·
2 revisions
The cosm-rb library makes it easy to convert Ruby objects into Cosm ones.
feed = Cosm::Feed.new # initialize a new Feed object
feed.title = "My Cosm Feed"
feed.datastreams = [Cosm::Datastream.new(:id => 'test')] # Let's give it one datastream with id 'test'
Let's see what it looks like:
feed.to_json
=> {"title":"Test feed","version":"1.0.0","datastreams":[{"id":"test"}]}
You can also represent objects in Cosm xml and csv
The cosm-rb library makes it easy to convert Cosm data into a Ruby object
json = <<-EOJSON
{
"max_value": "658.0",
"current_value": "14",
"datapoints": [{
"value": "1",
"at": "2011-03-02T15:59:56.895922Z"
},
{
"value": "1",
"at": "2011-03-02T16:00:07.188648Z"
},
{
"value": "2",
"at": "2011-03-02T16:00:18.416500Z"
}],
"min_value": "0.0",
"id": "0",
"tags": ["humidity", "Temperature", "freakin lasers"],
"version": "1.0.0",
"unit": {
"label": "percentage",
"symbol": "%",
"type": "derived SI"
},
"at": "2011-02-16T16:21:01.834174Z"
}
EOJSON
datastream = Cosm::Datastream.new(json)
datastream.to_xml # =>
# <?xml version="1.0" encoding="UTF-8"?>
# <eeml xmlns="http://www.eeml.org/xsd/0.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd">
# <environment creator="" updated="2011-02-16T16:21:01.834174Z" id="">
# <data id="0">
# <tag>freakin lasers</tag>
# <tag>humidity</tag>
# <tag>Temperature</tag>
# <current_value at="2011-02-16T16:21:01.834174Z">14</current_value>
# <max_value>658.0</max_value>
# <min_value>0.0</min_value>
# <unit type="derived SI" symbol="%">percentage</unit>
# <datapoints>
# <value at="2011-03-02T15:59:56.895922Z">1</value>
# <value at="2011-03-02T16:00:07.188648Z">1</value>
# <value at="2011-03-02T16:00:18.416500Z">2</value>
# </datapoints>
# </data>
# </environment>
# </eeml>