Permalink
Browse files

Added specs for connection and a few for device

  • Loading branch information...
1 parent 4477ea1 commit e5886ac2c838fdfec4a8e860f677a9592a0acc56 @jwtd committed Mar 31, 2014
View
@@ -1 +1,5 @@
require "bundler/gem_tasks"
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
+task :default => :spec
View
@@ -1,5 +1,9 @@
module XivelyConnector
+ # Base XivelyConnector exception class
+ class XivelyConnectorError < ::Exception; end
+
+ # Module level methods for establishing connections and finding objects
@@connection = nil
# Lookup methods delegate to class methods
@@ -13,15 +17,23 @@ def self.connect(options)
@@connection = Connection.new(options)
end
+ # Releases the connection
+ def self.disconnect()
+ @@connection = nil
+ end
+
+ # Lookup method delegates to class methods
+ def self.find(id, api_key=nil)
+ self.find_device_by_id(id, api_key=nil)
+ end
+
# Lookup methods delegate to class methods
def self.find_device_by_id(id, api_key=nil)
self.connect(:api_key => api_key) unless api_key.nil?
+ raise "Can not connect without an api_key or an existing connection." if self.connection.nil? and api_key.nil?
Device.find_by_id(id)
end
- # Base XivelyConnector exception class
- class XivelyConnectorError < ::Exception; end
-
end
require "xively-rb-connector/version"
@@ -12,9 +12,22 @@ class Device < Xively::Feed
include XivelyConnector::Logging
# Connect to a device by ID
- def self.find_by_id(id)
+ def self.find(id, api_key=nil)
+ self.find_by_id(id, api_key)
+ end
+
+ # Connect to a device by ID
+ def self.find_by_id(id, api_key=nil)
+
+ # First connect if necessary
+ XivelyConnector.connect(:api_key => api_key) unless api_key.nil?
+ raise "Can not connect without an api_key or an existing connection." if XivelyConnector.connection.nil? and api_key.nil?
+
XivelyConnector.connection.logger.debug "Device.find_by_id(#{id})"
+
+ # Perform the lookup
response = XivelyConnector.connection.get("/v2/feeds/#{id}.json")
+ pp response
if response.success?
self.new(:response => response)
else
@@ -1,8 +1,13 @@
module XivelyConnector
+
+ def self.version_string
+ "XivelyConnector version #{XivelyConnector::VERSION::STRING}"
+ end
+
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
- PATCH = 0
+ PATCH = 1
STRING = [MAJOR, MINOR, PATCH].join('.')
end
View
@@ -0,0 +1,77 @@
+require 'rubygems'
+require 'bundler/setup'
+Bundler.setup
+
+require 'rspec'
+require 'webmock/rspec'
+require 'time'
+
+# figure out where we are being loaded from
+if $LOADED_FEATURES.grep(/spec\/spec_helper\.rb/).any?
+ begin
+ raise "foo"
+ rescue => e
+ puts <<-MSG
+ ===================================================
+ It looks like spec_helper.rb has been loaded
+ multiple times. Normalize the require to:
+
+ require "spec/spec_helper"
+
+ Things like File.join and File.expand_path will
+ cause it to be loaded multiple times.
+
+ Loaded this time from:
+
+ #{e.backtrace.join("\n ")}
+ ===================================================
+ MSG
+ end
+end
+
+if !defined?(JRUBY_VERSION)
+ if ENV["COVERAGE"] == "on"
+ require 'simplecov'
+ SimpleCov.start do
+ add_filter "/spec/"
+ add_filter "/lib/xively-rb.rb"
+ add_filter "/vendor/"
+ minimum_coverage 100
+ end
+ end
+end
+
+#Dir['./spec/support/**/*.rb'].map {|f| require f}
+
+$:.push File.expand_path("../lib", __FILE__)
+require 'xively-rb-connector'
+
+#require File.dirname(__FILE__) + '/fixtures/models.rb'
+
+RSpec.configure do |config|
+ config.color_enabled = true
+ config.formatter = 'documentation'
+ config.run_all_when_everything_filtered = true
+
+ # Webmock
+ config.before(:each) do
+ stub_request(:get, "https://api.xively.com/v2/feeds/000000001.json").
+ with(:headers => {'User-Agent'=>'xively-rb/0.2.10', 'X-Apikey'=>'abcdefg'}).
+ to_return(:status => 200,
+ :body => File.open(File.dirname(__FILE__) + '/support/fixtures/feed_100000000.json').read,
+ :headers => {})
+ end
+
+ # Use sinatra
+ #config.before(:each) do
+ # stub_request(:any, /api.github.com/).to_rack(FakeGitHub)
+ #end
+
+
+
+end
+
+
+
+
+
@@ -0,0 +1,102 @@
+{
+ "id": 10000000,
+ "title": "Smart Meter",
+ "private": "true",
+ "website": "http://www.smart-metering.com",
+ "tags": [
+ "Smartmeter",
+ "Gas",
+ "Power",
+ "Propane",
+ "Water"
+ ],
+ "description": "This is a description.",
+ "auto_feed_url": "https://api.xively.com/v2/feeds/10000000.json",
+ "status": "frozen",
+ "updated": "2014-03-31T15:32:58.390034Z",
+ "created": "2014-03-28T16:36:30.651731Z",
+ "email": "john.doe@gmail.com",
+ "creator": "https://xively.com/users/jdoe",
+ "version": "1.0.0",
+ "datastreams": [
+ {
+ "id": "Amperage",
+ "current_value": "30",
+ "at": "2014-03-28T16:39:14.898381Z",
+ "max_value": "0.0",
+ "min_value": "40.0",
+ "tags": [
+ "Power",
+ "Amps"
+ ],
+ "unit": {
+ "symbol": "A",
+ "label": "Amps"
+ }
+ },
+ {
+ "id": "Propane",
+ "current_value": "4",
+ "at": "2014-03-28T16:45:05.133736Z",
+ "max_value": "7.5",
+ "min_value": "0.0",
+ "unit": {
+ "symbol": "cft",
+ "label": "Cubic Feet"
+ }
+ },
+ {
+ "id": "Voltage",
+ "current_value": "240",
+ "at": "2014-03-31T15:32:58.390034Z",
+ "max_value": "245.0",
+ "min_value": "238.0",
+ "tags": [
+ "Power",
+ "Volts"
+ ],
+ "unit": {
+ "symbol": "V",
+ "label": "Volts"
+ }
+ },
+ {
+ "id": "Wattage",
+ "current_value": "0",
+ "at": "2014-03-28T16:38:11.905385Z",
+ "max_value": "10000.0",
+ "min_value": "0.0",
+ "tags": [
+ "Power",
+ "Watts"
+ ],
+ "unit": {
+ "symbol": "W",
+ "label": "Watts"
+ }
+ },
+ {
+ "id": "Well",
+ "current_value": "0",
+ "at": "2014-03-28T16:41:56.135003Z",
+ "max_value": "7.5",
+ "min_value": "0.0",
+ "tags": [
+ "Water"
+ ],
+ "unit": {
+ "symbol": "cft",
+ "label": "Cubic Feet"
+ }
+ }
+ ],
+ "location": {
+ "name": "Some Address",
+ "domain": "physical",
+ "ele": "350 feet",
+ "lat": 30.12345678912345,
+ "lon": -78.0987654321098
+ },
+ "product_id": "982634kjhlkjh23412k3",
+ "device_serial": "HG68JHG07HG6"
+}
@@ -0,0 +1,79 @@
+class Feed
+ extend Xively::Base
+
+ is_xively :feed
+ attr_accessor :datastreams
+ attr_accessor :feed
+ attr_accessor :creator
+ attr_accessor :title
+ attr_accessor :website
+ attr_accessor :icon
+ attr_accessor :description
+ attr_accessor :updated
+ attr_accessor :email
+ attr_accessor :private
+ attr_accessor :tags
+ attr_accessor :location_disposition
+ attr_accessor :location_domain
+ attr_accessor :location_ele
+ attr_accessor :location_exposure
+ attr_accessor :location_lat
+ attr_accessor :location_lon
+ attr_accessor :location_name
+
+ def attributes
+ attributes = {}
+ Xively::Feed::ALLOWED_KEYS.each do |key|
+ attributes[key] = self.send(key) if self.respond_to?(key)
+ end
+ attributes
+ end
+end
+
+class Datastream
+ extend Xively::Base
+
+ is_xively :datastream, {:id => :stream_id}
+
+ attr_accessor :feed
+ attr_accessor :feed_id
+ attr_accessor :datapoints
+ attr_accessor :id
+ attr_accessor :stream_id
+ attr_accessor :feed_creator
+ attr_accessor :current_value
+ attr_accessor :min_value
+ attr_accessor :max_value
+ attr_accessor :unit_label
+ attr_accessor :unit_type
+ attr_accessor :unit_symbol
+ attr_accessor :tags
+ attr_accessor :updated
+
+ def attributes
+ attributes = {}
+ Xively::Datastream::ALLOWED_KEYS.each do |key|
+ attributes[key] = self.send(key) if self.respond_to?(key)
+ end
+ attributes
+ end
+
+end
+
+class Datapoint
+ extend Xively::Base
+
+ is_xively :datapoint
+ attr_accessor :datastream_id
+ attr_accessor :at
+ attr_accessor :value
+
+ def attributes
+ attributes = {}
+ Xively::Datapoint::ALLOWED_KEYS.each do |key|
+ attributes[key] = self.send(key) if self.respond_to?(key)
+ end
+ attributes
+ end
+
+end
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe XivelyConnector::Connection do
+
+ before do
+ @client = XivelyConnector.connect(:api_key => "abcdefg")
+ end
+
+ it "should have the base uri defined" do
+ XivelyConnector::Connection.base_uri.should == 'https://api.xively.com'
+ end
+
+ describe "#get" do
+ it "should make the appropriate request" do
+ request_stub = stub_request(:get, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504.json").
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'})
+ @client.get('/v2/feeds/504.json')
+ request_stub.should have_been_made
+ end
+ end
+
+ describe "#put" do
+ it "should make the appropriate request" do
+ request_stub = stub_request(:put, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504.json").
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'}, :body => "dataz")
+ @client.put('/v2/feeds/504.json', :body => "dataz")
+ request_stub.should have_been_made
+ end
+ end
+
+ describe "#post" do
+ it "should make the appropriate request" do
+ request_stub = stub_request(:post, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504.json").
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'}, :body => "dataz")
+ @client.post('/v2/feeds/504.json', :body => "dataz")
+ request_stub.should have_been_made
+ end
+ end
+
+ describe "#delete" do
+ it "should make the appropriate request" do
+ request_stub = stub_request(:delete, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504/datastreams/test.json").
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'})
+ @client.delete('/v2/feeds/504/datastreams/test.json')
+ request_stub.should have_been_made
+ end
+ end
+
+end
+
+
Oops, something went wrong.

0 comments on commit e5886ac

Please sign in to comment.