xively-rb-connector 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea7c9c6444f3e234d46349afce7ce495e7b44b6d
4
- data.tar.gz: 9bab33c8f37fa6b4783d77f100ffe665c9a26685
3
+ metadata.gz: e3603d6b40f350f03f7c098653eb455baa9e876a
4
+ data.tar.gz: 1196c5ea6f043590d61fa467c9de9b09acc1b968
5
5
  SHA512:
6
- metadata.gz: 13b6b634f4e5d98ed315cabd8989c96ab2779ac798e6d3b96bcc42be27cd7e23d480448b8451cb7c17d1e7bd84b30d01d79aa023bddd4d68f2f3d0a80f3fe358
7
- data.tar.gz: 73618b4fb24df88f295708236bc0507509e0bc7fb9c2d437979ee2e9cca37589c9718ffd397e016702dd35c78d80d18a04759a0ba5680aa6bcd384d774589525
6
+ metadata.gz: 54ebdc4e4adf196b618734693f2e54459cde65e0de9a92be6b67bc3905a2ab8c4f40151a7018e623e36f1ffc06e4b855c3235c8f490a740fdb5dba5d7ec54279
7
+ data.tar.gz: 022905b2d1093136cb2ecc9fd0ce30343acbe1a6ace381f5802b22b59f1348a723929fc33cdc890ddb3990710100ec7aef38743f7655dd02d8ed3861766b164e
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ module XivelyConnector
2
+
3
+ class << self
4
+ attr_writer :configuration
5
+ end
6
+
7
+ def self.configuration
8
+ @configuration ||= Configuration.new
9
+ end
10
+
11
+ def self.reset
12
+ @configuration = Configuration.new
13
+ end
14
+
15
+ def self.configure
16
+ yield(configuration)
17
+ end
18
+
19
+ class Configuration
20
+ attr_accessor :api_key
21
+ attr_accessor :datapoint_buffer_size
22
+ attr_accessor :only_save_changes
23
+ end
24
+
25
+ end
@@ -10,12 +10,16 @@ module XivelyConnector
10
10
  # Mix in the ability to log
11
11
  include XivelyConnector::Logging
12
12
 
13
+ attr_accessor :config
14
+
13
15
  def initialize(options)
14
16
  @logger = options[:logger] || logger
17
+ @config = options[:config] || Configuration.new
15
18
  @logger.debug "XivelyConnector::Connection initialize"
16
19
  super(options[:api_key])
17
20
  end
18
21
 
22
+
19
23
  #Set HTTParty params that we need to set after initialize is called
20
24
  #These params come from @options within initialize and include the following:
21
25
  #:ssl_ca_file - SSL CA File for SSL connections
@@ -12,9 +12,22 @@ class Device < Xively::Feed
12
12
  include XivelyConnector::Logging
13
13
 
14
14
  # Connect to a device by ID
15
- def self.find_by_id(id)
15
+ def self.find(id, api_key=nil)
16
+ self.find_by_id(id, api_key)
17
+ end
18
+
19
+ # Connect to a device by ID
20
+ def self.find_by_id(id, api_key=nil)
21
+
22
+ # First connect if necessary
23
+ XivelyConnector.connect(:api_key => api_key) unless api_key.nil?
24
+ raise "Can not connect without an api_key or an existing connection." if XivelyConnector.connection.nil? and api_key.nil?
25
+
16
26
  XivelyConnector.connection.logger.debug "Device.find_by_id(#{id})"
27
+
28
+ # Perform the lookup
17
29
  response = XivelyConnector.connection.get("/v2/feeds/#{id}.json")
30
+
18
31
  if response.success?
19
32
  self.new(:response => response)
20
33
  else
@@ -1,8 +1,13 @@
1
1
  module XivelyConnector
2
+
3
+ def self.version_string
4
+ "XivelyConnector version #{XivelyConnector::VERSION::STRING}"
5
+ end
6
+
2
7
  module VERSION #:nodoc:
3
8
  MAJOR = 0
4
9
  MINOR = 1
5
- PATCH = 0
10
+ PATCH = 2
6
11
 
7
12
  STRING = [MAJOR, MINOR, PATCH].join('.')
8
13
  end
@@ -1,5 +1,9 @@
1
1
  module XivelyConnector
2
2
 
3
+ # Base XivelyConnector exception class
4
+ class XivelyConnectorError < ::Exception; end
5
+
6
+ # Module level methods for establishing connections and finding objects
3
7
  @@connection = nil
4
8
 
5
9
  # Lookup methods delegate to class methods
@@ -13,15 +17,23 @@ module XivelyConnector
13
17
  @@connection = Connection.new(options)
14
18
  end
15
19
 
20
+ # Releases the connection
21
+ def self.disconnect()
22
+ @@connection = nil
23
+ end
24
+
25
+ # Lookup method delegates to class methods
26
+ def self.find(id, api_key=nil)
27
+ self.find_device_by_id(id, api_key=nil)
28
+ end
29
+
16
30
  # Lookup methods delegate to class methods
17
31
  def self.find_device_by_id(id, api_key=nil)
18
32
  self.connect(:api_key => api_key) unless api_key.nil?
33
+ raise "Can not connect without an api_key or an existing connection." if self.connection.nil? and api_key.nil?
19
34
  Device.find_by_id(id)
20
35
  end
21
36
 
22
- # Base XivelyConnector exception class
23
- class XivelyConnectorError < ::Exception; end
24
-
25
37
  end
26
38
 
27
39
  require "xively-rb-connector/version"
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.setup
4
+
5
+ require 'rspec'
6
+ require 'webmock/rspec'
7
+ require 'time'
8
+
9
+ # figure out where we are being loaded from
10
+ if $LOADED_FEATURES.grep(/spec\/spec_helper\.rb/).any?
11
+ begin
12
+ raise "foo"
13
+ rescue => e
14
+ puts <<-MSG
15
+ ===================================================
16
+ It looks like spec_helper.rb has been loaded
17
+ multiple times. Normalize the require to:
18
+
19
+ require "spec/spec_helper"
20
+
21
+ Things like File.join and File.expand_path will
22
+ cause it to be loaded multiple times.
23
+
24
+ Loaded this time from:
25
+
26
+ #{e.backtrace.join("\n ")}
27
+ ===================================================
28
+ MSG
29
+ end
30
+ end
31
+
32
+ if !defined?(JRUBY_VERSION)
33
+ if ENV["COVERAGE"] == "on"
34
+ require 'simplecov'
35
+ SimpleCov.start do
36
+ add_filter "/spec/"
37
+ add_filter "/lib/xively-rb.rb"
38
+ add_filter "/vendor/"
39
+ minimum_coverage 100
40
+ end
41
+ end
42
+ end
43
+
44
+ #Dir['./spec/support/**/*.rb'].map {|f| require f}
45
+
46
+ $:.push File.expand_path("../lib", __FILE__)
47
+ require 'xively-rb-connector'
48
+
49
+ #require File.dirname(__FILE__) + '/fixtures/models.rb'
50
+
51
+ RSpec.configure do |config|
52
+ config.color_enabled = true
53
+ config.formatter = 'documentation'
54
+ config.run_all_when_everything_filtered = true
55
+
56
+ # Webmock
57
+ config.before(:each) do
58
+ stub_request(:get, "https://api.xively.com/v2/feeds/000000001.json").
59
+ with(:headers => {'User-Agent'=>'xively-rb/0.2.10', 'X-Apikey'=>'abcdefg'}).
60
+ to_return(:status => 200,
61
+ :body => File.open(File.dirname(__FILE__) + '/support/fixtures/feed_100000000.json').read,
62
+ :headers => {})
63
+ end
64
+
65
+
66
+ end
67
+
68
+
69
+
70
+
71
+
@@ -0,0 +1,102 @@
1
+ {
2
+ "id": 10000000,
3
+ "title": "Smart Meter",
4
+ "private": "true",
5
+ "website": "http://www.smart-metering.com",
6
+ "tags": [
7
+ "Smartmeter",
8
+ "Gas",
9
+ "Power",
10
+ "Propane",
11
+ "Water"
12
+ ],
13
+ "description": "This is a description.",
14
+ "auto_feed_url": "https://api.xively.com/v2/feeds/10000000.json",
15
+ "status": "frozen",
16
+ "updated": "2014-03-31T15:32:58.390034Z",
17
+ "created": "2014-03-28T16:36:30.651731Z",
18
+ "email": "john.doe@gmail.com",
19
+ "creator": "https://xively.com/users/jdoe",
20
+ "version": "1.0.0",
21
+ "datastreams": [
22
+ {
23
+ "id": "Amperage",
24
+ "current_value": "30",
25
+ "at": "2014-03-28T16:39:14.898381Z",
26
+ "max_value": "0.0",
27
+ "min_value": "40.0",
28
+ "tags": [
29
+ "Power",
30
+ "Amps"
31
+ ],
32
+ "unit": {
33
+ "symbol": "A",
34
+ "label": "Amps"
35
+ }
36
+ },
37
+ {
38
+ "id": "Propane",
39
+ "current_value": "4",
40
+ "at": "2014-03-28T16:45:05.133736Z",
41
+ "max_value": "7.5",
42
+ "min_value": "0.0",
43
+ "unit": {
44
+ "symbol": "cft",
45
+ "label": "Cubic Feet"
46
+ }
47
+ },
48
+ {
49
+ "id": "Voltage",
50
+ "current_value": "240",
51
+ "at": "2014-03-31T15:32:58.390034Z",
52
+ "max_value": "245.0",
53
+ "min_value": "238.0",
54
+ "tags": [
55
+ "Power",
56
+ "Volts"
57
+ ],
58
+ "unit": {
59
+ "symbol": "V",
60
+ "label": "Volts"
61
+ }
62
+ },
63
+ {
64
+ "id": "Wattage",
65
+ "current_value": "0",
66
+ "at": "2014-03-28T16:38:11.905385Z",
67
+ "max_value": "10000.0",
68
+ "min_value": "0.0",
69
+ "tags": [
70
+ "Power",
71
+ "Watts"
72
+ ],
73
+ "unit": {
74
+ "symbol": "W",
75
+ "label": "Watts"
76
+ }
77
+ },
78
+ {
79
+ "id": "Well",
80
+ "current_value": "0",
81
+ "at": "2014-03-28T16:41:56.135003Z",
82
+ "max_value": "7.5",
83
+ "min_value": "0.0",
84
+ "tags": [
85
+ "Water"
86
+ ],
87
+ "unit": {
88
+ "symbol": "cft",
89
+ "label": "Cubic Feet"
90
+ }
91
+ }
92
+ ],
93
+ "location": {
94
+ "name": "Some Address",
95
+ "domain": "physical",
96
+ "ele": "350 feet",
97
+ "lat": 30.12345678912345,
98
+ "lon": -78.0987654321098
99
+ },
100
+ "product_id": "982634kjhlkjh23412k3",
101
+ "device_serial": "HG68JHG07HG6"
102
+ }
@@ -0,0 +1,79 @@
1
+ class Feed
2
+ extend Xively::Base
3
+
4
+ is_xively :feed
5
+ attr_accessor :datastreams
6
+ attr_accessor :feed
7
+ attr_accessor :creator
8
+ attr_accessor :title
9
+ attr_accessor :website
10
+ attr_accessor :icon
11
+ attr_accessor :description
12
+ attr_accessor :updated
13
+ attr_accessor :email
14
+ attr_accessor :private
15
+ attr_accessor :tags
16
+ attr_accessor :location_disposition
17
+ attr_accessor :location_domain
18
+ attr_accessor :location_ele
19
+ attr_accessor :location_exposure
20
+ attr_accessor :location_lat
21
+ attr_accessor :location_lon
22
+ attr_accessor :location_name
23
+
24
+ def attributes
25
+ attributes = {}
26
+ Xively::Feed::ALLOWED_KEYS.each do |key|
27
+ attributes[key] = self.send(key) if self.respond_to?(key)
28
+ end
29
+ attributes
30
+ end
31
+ end
32
+
33
+ class Datastream
34
+ extend Xively::Base
35
+
36
+ is_xively :datastream, {:id => :stream_id}
37
+
38
+ attr_accessor :feed
39
+ attr_accessor :feed_id
40
+ attr_accessor :datapoints
41
+ attr_accessor :id
42
+ attr_accessor :stream_id
43
+ attr_accessor :feed_creator
44
+ attr_accessor :current_value
45
+ attr_accessor :min_value
46
+ attr_accessor :max_value
47
+ attr_accessor :unit_label
48
+ attr_accessor :unit_type
49
+ attr_accessor :unit_symbol
50
+ attr_accessor :tags
51
+ attr_accessor :updated
52
+
53
+ def attributes
54
+ attributes = {}
55
+ Xively::Datastream::ALLOWED_KEYS.each do |key|
56
+ attributes[key] = self.send(key) if self.respond_to?(key)
57
+ end
58
+ attributes
59
+ end
60
+
61
+ end
62
+
63
+ class Datapoint
64
+ extend Xively::Base
65
+
66
+ is_xively :datapoint
67
+ attr_accessor :datastream_id
68
+ attr_accessor :at
69
+ attr_accessor :value
70
+
71
+ def attributes
72
+ attributes = {}
73
+ Xively::Datapoint::ALLOWED_KEYS.each do |key|
74
+ attributes[key] = self.send(key) if self.respond_to?(key)
75
+ end
76
+ attributes
77
+ end
78
+
79
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe XivelyConnector::Connection do
4
+
5
+ before do
6
+ @client = XivelyConnector.connect(:api_key => "abcdefg")
7
+ end
8
+
9
+ it "should have the base uri defined" do
10
+ XivelyConnector::Connection.base_uri.should == 'https://api.xively.com'
11
+ end
12
+
13
+ describe "#get" do
14
+ it "should make the appropriate request" do
15
+ request_stub = stub_request(:get, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504.json").
16
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'})
17
+ @client.get('/v2/feeds/504.json')
18
+ request_stub.should have_been_made
19
+ end
20
+ end
21
+
22
+ describe "#put" do
23
+ it "should make the appropriate request" do
24
+ request_stub = stub_request(:put, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504.json").
25
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'}, :body => "dataz")
26
+ @client.put('/v2/feeds/504.json', :body => "dataz")
27
+ request_stub.should have_been_made
28
+ end
29
+ end
30
+
31
+ describe "#post" do
32
+ it "should make the appropriate request" do
33
+ request_stub = stub_request(:post, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504.json").
34
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'}, :body => "dataz")
35
+ @client.post('/v2/feeds/504.json', :body => "dataz")
36
+ request_stub.should have_been_made
37
+ end
38
+ end
39
+
40
+ describe "#delete" do
41
+ it "should make the appropriate request" do
42
+ request_stub = stub_request(:delete, "#{XivelyConnector::Connection.base_uri}/v2/feeds/504/datastreams/test.json").
43
+ with(:headers => {'User-Agent' => XivelyConnector::Connection.user_agent, 'X-ApiKey' => 'abcdefg'})
44
+ @client.delete('/v2/feeds/504/datastreams/test.json')
45
+ request_stub.should have_been_made
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe XivelyConnector::Device do
4
+
5
+ describe ".find" do
6
+
7
+ it "should use an existing connection if it exists" do
8
+ XivelyConnector.disconnect
9
+ XivelyConnector.connect(:api_key=>"abcdefg")
10
+ expect {XivelyConnector::Device.find('000000001')}.not_to raise_error
11
+ end
12
+
13
+ it "should raise an error if a connection doesn't exists and no api_key is provided" do
14
+ XivelyConnector.disconnect
15
+ expect {XivelyConnector::Device.find('000000001')}.to raise_error
16
+ end
17
+
18
+ it "should return a device object" do
19
+ d = XivelyConnector::Device.find('000000001', "abcdefg")
20
+ d.title.should == 'Smart Meter'
21
+ end
22
+
23
+ end
24
+
25
+ describe ".find_by_id" do
26
+
27
+ it "should use an existing connection if it exists" do
28
+ XivelyConnector.disconnect
29
+ XivelyConnector.connect(:api_key=>"abcdefg")
30
+ expect {XivelyConnector::Device.find_by_id('000000001')}.not_to raise_error
31
+ end
32
+
33
+ it "should raise an error if a connection doesn't exists and no api_key is provided" do
34
+ XivelyConnector.disconnect
35
+ expect {XivelyConnector::Device.find_by_id('000000001')}.to raise_error
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe XivelyConnector do
4
+
5
+ it 'should return correct version string' do
6
+ XivelyConnector.version_string.should == "XivelyConnector version #{XivelyConnector::VERSION::STRING}"
7
+ end
8
+
9
+ describe ".connection" do
10
+
11
+ it "should be available from the module" do
12
+ XivelyConnector.connect(:api_key=>"abcdefg")
13
+ XivelyConnector.connection.class.should == XivelyConnector::Connection
14
+ XivelyConnector.connection.api_key.should == 'abcdefg'
15
+ end
16
+
17
+ end
18
+
19
+ describe ".connect" do
20
+
21
+ it "should connect using an api-key" do
22
+ XivelyConnector.connect(:api_key=>'abcdefg').api_key.should == 'abcdefg'
23
+ XivelyConnector.connection.class.should == XivelyConnector::Connection
24
+ XivelyConnector.connection.api_key.should == 'abcdefg'
25
+ end
26
+
27
+ end
28
+
29
+ describe ".disconnect" do
30
+
31
+ it "should release the connection" do
32
+ XivelyConnector.connect(:api_key=>"abcdefg")
33
+ XivelyConnector.disconnect
34
+ XivelyConnector.connection.should == nil
35
+ end
36
+
37
+ end
38
+
39
+ describe ".find" do
40
+
41
+ it "should use an existing connection if it exists" do
42
+ XivelyConnector.disconnect
43
+ XivelyConnector.connect(:api_key=>"abcdefg")
44
+ expect {XivelyConnector.find('000000001')}.not_to raise_error
45
+ end
46
+
47
+ it "should raise an error if a connection doesn't exists and no api_key is provided" do
48
+ XivelyConnector.disconnect
49
+ expect {XivelyConnector.find('000000001')}.to raise_error
50
+ end
51
+
52
+ end
53
+
54
+
55
+ describe ".find_device_by_id" do
56
+
57
+ it "should use an existing connection if it exists" do
58
+ XivelyConnector.disconnect
59
+ XivelyConnector.connect(:api_key=>"abcdefg")
60
+ expect {XivelyConnector.find('000000001')}.not_to raise_error
61
+ end
62
+
63
+ it "should raise an error if a connection doesn't exists and no api_key is provided" do
64
+ XivelyConnector.disconnect
65
+ expect {XivelyConnector.find('000000001')}.to raise_error
66
+ end
67
+
68
+ end
69
+
70
+
71
+ end
@@ -30,6 +30,8 @@ web. Xively provides a fantastic development portal and prototyping accounts are
30
30
  # Development dependencies
31
31
  spec.add_development_dependency "bundler", "~> 1.3"
32
32
  spec.add_development_dependency "rake", "~> 10.2"
33
+ spec.add_development_dependency "rspec"
34
+ spec.add_development_dependency "webmock"
33
35
 
34
36
  # Runtime dependencies
35
37
  spec.add_runtime_dependency "log4r", "~> 1.1"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xively-rb-connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Duggan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: log4r
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -101,11 +129,18 @@ files:
101
129
  - README.md
102
130
  - Rakefile
103
131
  - lib/xively-rb-connector.rb
132
+ - lib/xively-rb-connector/configuration.rb
104
133
  - lib/xively-rb-connector/connection.rb
105
134
  - lib/xively-rb-connector/datastream.rb
106
135
  - lib/xively-rb-connector/device.rb
107
136
  - lib/xively-rb-connector/logging.rb
108
137
  - lib/xively-rb-connector/version.rb
138
+ - spec/spec_helper.rb
139
+ - spec/support/fixtures/feed_100000000.json
140
+ - spec/support/fixtures/models.rb
141
+ - spec/xively-rb-connector/connection_spec.rb
142
+ - spec/xively-rb-connector/device_spec.rb
143
+ - spec/xively-rb-connector/module_spec.rb
109
144
  - xively-rb-connector.gemspec
110
145
  homepage: https://github.com/jwtd/xively-rb-connector
111
146
  licenses:
@@ -131,4 +166,10 @@ rubygems_version: 2.2.2
131
166
  signing_key:
132
167
  specification_version: 4
133
168
  summary: Ruby gem that provides a high level interface to Xively
134
- test_files: []
169
+ test_files:
170
+ - spec/spec_helper.rb
171
+ - spec/support/fixtures/feed_100000000.json
172
+ - spec/support/fixtures/models.rb
173
+ - spec/xively-rb-connector/connection_spec.rb
174
+ - spec/xively-rb-connector/device_spec.rb
175
+ - spec/xively-rb-connector/module_spec.rb