Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Using with activerecord

Basel Farah edited this page May 14, 2013 · 4 revisions

Example with ActiveRecord

    class Datastream < ActiveRecord::Base
      belongs_to :feed
    end

    class Feed < ActiveRecord::Base
      has_many :datastreams
      extend Xively::Base
      is_xively :feed
    end

Provided methods

    @xively_feed = feed.to_xively # returns an instance of Xively::Feed
    @xively_feed.to_json(:version => "1.0.0") # converts your feed and associated datastreams into Xively V2 JSON
    @xively_feed.as_json(:version => "0.6-alpha") # provides a json hash for 0.6-alpha
    @xively_feed.to_xml(:version => "0.5.1") # converts your feed and associated datastreams into Xively V2 XML (EEML)

Supported formats

  • JSON "1.0.0" - used by Xively API v2
  • JSON "0.6-alpha" - used by Xively API v1
  • XML "0.5.1" - used by Xively API v2
  • XML "5" - used by Xively API v1
  • CSV v1 - used by Xively API v1
  • CSV v2 - used by Xively API v2

Mapped fields

See [the Xively Api docs] 1 for a description of each field.

By default the gem expects your object to have the following fields:

Feeds

  • creator
  • datastreams
  • description
  • email
  • feed
  • icon
  • id
  • location_disposition
  • location_domain
  • location_ele
  • location_exposure
  • location_lat
  • location_lon
  • location_name
  • private
  • status
  • tags
  • title
  • updated
  • website

Datastreams

  • current_value
  • datapoints
  • feed_creator
  • feed_id
  • id
  • max_value
  • min_value
  • tags
  • unit_label
  • unit_symbol
  • unit_type
  • updated

Datapoints

  • at
  • value
  • feed_id
  • datastream_id

If you use different field names, want to map custom fields or want to map fields onto instance methods you can:

    class Feed < ActiveRecord::Base
      extend Xively::Base

      has_one :geo_location
      is_xively :feed, {:location_lat => :geo_lat, :location_lon => :geo_lon}


      def geo_lat
        geo_location.try(:latitude)
      end

      def geo_lon
        geo_location.try(:longitude)
      end
    end