Skip to content

Talking to the REST API

levent edited this page Mar 12, 2013 · 5 revisions

Cosm::Client

Make sure you have a Cosm account

  1. Signup for free on cosm.com
  2. Create an api key on Cosm

Create a Feed

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 create the feed on Cosm

client = Cosm::Client.new(YOUR_API_KEY)
response = client.post('/v2/feeds.json', :body => feed.to_json)
puts response.headers['location'] # Will give us the location of the Cosm feed including the ID
=> "http://api.cosm.com/v2/feeds/SOMEID"

Create a Datapoint

The datapoint creation endpoint takes an array of datapoints

datapoint = Cosm::Datapoint.new(:at => Time.now, :value => "25")
client.post('/v2/feeds/504/datastreams/temperature/datapoints', :body => {:datapoints => [datapoint]}.to_json)