xively-rb-connector 0.1.3 → 0.1.4

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: a50ebc4f70611bff7ec5580415a211ab47e38a50
4
- data.tar.gz: f718ea2b7572645c7b9819442c887f5d474e9d33
3
+ metadata.gz: 2242f1160f39605cefa4bdbe99eaa725662e6976
4
+ data.tar.gz: 60d227c0cb4ff27e962708db758883ee8ec35551
5
5
  SHA512:
6
- metadata.gz: 4dd0cc6f2dc13b82397a3e797c25edca5997950890c1c57ece6d475e7e51bd6717864916b41f8889cd2c02a2502dee858d39257429f61822150a8bae93ce2f52
7
- data.tar.gz: 3c63bccae09298f15bb74ac8be4e52176110bd8d3a6f424c0a66170e123406f20663dab6b806fa78f567c8cf98bddaeddb9bcc58cb738ef29210e4f9a5208f9f
6
+ metadata.gz: 503e83fb982140c09a987b025968c292416239f861bb77007d50103e1617ee800bae85cd971e9131e15e81bec03e37844e15ca513d1e47f98b8d7b03995c132b
7
+ data.tar.gz: 1c1cd2da6a717739211d002f6ef65753f9117cb87ce27316fdeabf8a013e2aa253289dc1d5efa4b4b656e25c00302fc48c8b20a78ef4bcb8ce3e9ed187bda5b6
@@ -17,6 +17,11 @@ module XivelyConnector
17
17
  @@connection = Connection.new(options)
18
18
  end
19
19
 
20
+ # Lookup methods delegate to class methods
21
+ def self.connected?
22
+ not @@connection.nil?
23
+ end
24
+
20
25
  # Releases the connection
21
26
  def self.disconnect()
22
27
  @@connection = nil
@@ -28,16 +33,14 @@ module XivelyConnector
28
33
  end
29
34
 
30
35
  # Lookup methods delegate to class methods
31
- def self.find_device_by_id(id, api_key=nil)
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?
34
- Device.find_by_id(id)
36
+ def self.find_device_by_id(id, options)
37
+ Device.find_by_id(id, options)
35
38
  end
36
39
 
37
40
  end
38
41
 
39
42
  require "xively-rb-connector/version"
40
- require "xively-rb-connector/logging"
43
+ require "xively-rb-connector/logger"
41
44
  require "xively-rb-connector/connection"
42
45
  require "xively-rb-connector/device"
43
46
  require "xively-rb-connector/datastream"
@@ -3,12 +3,11 @@ require 'xively-rb'
3
3
  module XivelyConnector
4
4
 
5
5
  class Connection < Xively::Client
6
- include XivelyConnector::Logging
7
6
 
8
7
  format :json
9
8
 
10
9
  # Mix in the ability to log
11
- include XivelyConnector::Logging
10
+ include Logger
12
11
 
13
12
  attr_accessor :config
14
13
 
@@ -11,7 +11,7 @@ module XivelyConnector
11
11
  attr :datapoint_buffer_size, :only_save_changes
12
12
 
13
13
  # Mix in the ability to log
14
- include XivelyConnector::Logging
14
+ include Logger
15
15
 
16
16
  def initialize(options)
17
17
  @logger = options[:logger] || logger
@@ -26,15 +26,15 @@ module XivelyConnector
26
26
  super(data)
27
27
 
28
28
  # Set the default buffer size (1 reading per minute)
29
- @datapoint_buffer_size = options[:datapoint_buffer_size] || 60
30
- @only_save_changes = options[:only_save_changes] || true
29
+ @datapoint_buffer_size = options[:datapoint_buffer_size] || 1
30
+ @only_save_changes = options[:only_save_changes] || false
31
31
 
32
32
  # Initialize Xively::Datastream's datapoint array to allow shift style loading
33
33
  @datapoints = []
34
34
 
35
35
  # Fix the unit attribute assignments
36
- @unit_symbol = data['unit']['symbol']
37
- @unit_label = data['unit']['label']
36
+ @unit_symbol = data['unit']['symbol']
37
+ @unit_label = data['unit']['label']
38
38
 
39
39
  # Cast the current value as a BigDecimal for casting strings and comparing to ints and floats
40
40
  @current_value = 0 if current_value.nil?
@@ -2,37 +2,35 @@ require 'xively-rb'
2
2
  require 'json'
3
3
  require 'ostruct'
4
4
 
5
-
6
5
  module XivelyConnector
7
6
 
8
7
  # Extend https://github.com/xively-rb-connector/xively-rb
9
8
  class Device < Xively::Feed
10
9
 
11
- # Mix in the ability to log
12
- include XivelyConnector::Logging
10
+ attr_reader :datapoint_buffer_size, :only_save_changes
13
11
 
14
- # Connect to a device by ID
15
- def self.find(id, api_key=nil)
16
- self.find_by_id(id, api_key)
17
- end
12
+ # Mix in the ability to log
13
+ include Logger
18
14
 
19
15
  # Connect to a device by ID
20
- def self.find_by_id(id, api_key=nil)
16
+ def self.find_by_id(id, options={})
21
17
 
22
18
  # 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?
19
+ unless XivelyConnector.connected?
20
+ raise XivelyConnectorError, "Can not connect without an api_key or an existing connection." unless options[:api_key]
21
+ XivelyConnector.connect(options)
22
+ end
25
23
 
26
24
  XivelyConnector.connection.logger.debug "Device.find_by_id(#{id})"
27
25
 
28
- # Perform the lookup
29
- response = XivelyConnector.connection.get("/v2/feeds/#{id}.json")
26
+ # Perform the lookup and add the response
27
+ options[:response] = XivelyConnector.connection.get("/v2/feeds/#{id}.json")
30
28
 
31
- if response.success?
32
- self.new(:response => response)
29
+ if options[:response].success?
30
+ self.new(options)
33
31
  else
34
- logger.error response.response
35
- response.response
32
+ logger.error options[:response].response
33
+ options[:response].response
36
34
  end
37
35
  end
38
36
 
@@ -45,6 +43,8 @@ class Device < Xively::Feed
45
43
 
46
44
  # initialize parent
47
45
  data = options[:response]
46
+ @datapoint_buffer_size = options[:datapoint_buffer_size] || 1
47
+ @only_save_changes = options[:only_save_changes] || false
48
48
  super(options[:response])
49
49
 
50
50
  # Convert date strings to ruby dates
@@ -87,7 +87,10 @@ class Device < Xively::Feed
87
87
  @datastreams << datastream
88
88
  elsif datastream.is_a?(Hash)
89
89
  #@datastreams << Datastream.new(datastream)
90
- @datastreams << XivelyConnector::Datastream.new(:device => self, :data => datastream)
90
+ @datastreams << XivelyConnector::Datastream.new(:device => self,
91
+ :data => datastream,
92
+ :datapoint_buffer_size => datapoint_buffer_size,
93
+ :only_save_changes => only_save_changes)
91
94
  end
92
95
  end
93
96
  end
@@ -0,0 +1,121 @@
1
+ require 'logging'
2
+
3
+ module XivelyConnector
4
+
5
+ @@global_logger = nil
6
+
7
+ # Returns true or false, reflecting whether a global logger is configured
8
+ def self.global_logger_configured?
9
+ (not @@global_logger.nil?)
10
+ end
11
+
12
+ # Returns true or false, reflecting whether a global logger is configured
13
+ def self.configure_global_logger(options={})
14
+
15
+ # Collect and validate log level
16
+ log_level = options[:log_level] || :debug
17
+ raise "Can not initialize global logger, because #{log_level.inspect} is an unrecognized log level." unless [:off, :all, :debug, :info, :warn, :error, :fatal].include?(log_level)
18
+ Logging.logger.root.level = log_level
19
+
20
+ # When set to true backtraces will be written to the logs
21
+ trace_exceptions = options[:trace_exceptions] || true
22
+ Logging.logger.root.trace = trace_exceptions
23
+
24
+ # Setup colorized output for stdout (this scheme was setup for a terminal with a black background)
25
+ # :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white
26
+ # :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white
27
+ # :blink, :bold, :underline, :underscore
28
+ stdout_colors = options[:stdout_colors] || {:levels => {
29
+ :debug => :white,
30
+ :info => [:white, :on_blue, :bold],
31
+ :warn => [:black, :on_yellow, :bold] ,
32
+ :error => [:white, :on_red, :bold],
33
+ :fatal => [:white, :on_red, :bold, :blink]
34
+ },
35
+ :date => :yellow,
36
+ :logger => :cyan,
37
+ :message => :white}
38
+ Logging.color_scheme('stdout_colors', stdout_colors)
39
+
40
+ # Always log info to stdout
41
+ log_to_stdout = options[:log_to_stdout] || true
42
+ if log_to_stdout
43
+ Logging.logger.root.add_appenders Logging.appenders.stdout(
44
+ 'stdout',
45
+ :layout => Logging.layouts.pattern(
46
+ #:pattern => '[%d] %-5l %c: %m\n',
47
+ :color_scheme => 'stdout_colors'
48
+ )
49
+ )
50
+ end
51
+
52
+ if options[:log_file]
53
+
54
+ # Make sure log directory exists
55
+ log_file = File.expand_path(options[:log_file])
56
+ log_dir = File.dirname(log_file)
57
+ raise "The log file can not be created, because its directory does not exist #{log_dir}" if Dir.exists?(log_dir)
58
+
59
+ # Determine layout. The available layouts are :basic, :json, :yaml, or a pattern such as '[%d] %-5l: %m\n'
60
+ layout = options[:log_file_layout] || :basic
61
+ if options[:log_file_layout]
62
+ if layout == :basic
63
+ use_layout = Logging.layouts.basic
64
+ elsif layout == :json
65
+ use_layout = Logging.layouts.json
66
+ elsif layout == :yaml
67
+ use_layout = Logging.layouts.yaml
68
+ else
69
+ use_layout = Logging.layouts.pattern(:pattern => layout) # '[%d] %-5l: %m\n'
70
+ end
71
+ end
72
+
73
+ # Determine if this should be a single or rolling log file
74
+ rolling = options[:rolling_log_file_age] || false
75
+
76
+ # Build the file appender
77
+ if rolling
78
+ Logging.logger.root.add_appenders Logging.appenders.rolling_file(
79
+ 'development.log',
80
+ :age => rolling,
81
+ :layout => use_layout
82
+ )
83
+ else
84
+ # Non-rolling log file
85
+ Logging.logger.root.add_appenders Logging.appenders.file(log_file, :layout => use_layout)
86
+ end
87
+
88
+ # Growl on error
89
+ growl_on_error = options[:growl_on_error] || false
90
+ if options[:growl_on_error]
91
+ Logging.logger.root.add_appenders Logging.appenders.growl(
92
+ 'growl',
93
+ :level => :error,
94
+ :layout => Logging.layouts.pattern(:pattern => '[%d] %-5l: %m\n')
95
+ )
96
+ end
97
+
98
+ end
99
+ # Return the root logger
100
+ Logging.logger.root
101
+ end
102
+
103
+
104
+ module Logger
105
+
106
+ def logger
107
+ @logger ||= XivelyConnector::Logger.logger
108
+ end
109
+
110
+ def self.logger
111
+ @logger ||= self.configure_logger_for(self.class.name)
112
+ end
113
+
114
+ def self.configure_logger_for(classname)
115
+ XivelyConnector.configure_global_logger() unless XivelyConnector.global_logger_configured?
116
+ l = Logging.logger[classname]
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -7,7 +7,7 @@ module XivelyConnector
7
7
  module VERSION #:nodoc:
8
8
  MAJOR = 0
9
9
  MINOR = 1
10
- PATCH = 3
10
+ PATCH = 4
11
11
 
12
12
  STRING = [MAJOR, MINOR, PATCH].join('.')
13
13
  end
@@ -34,7 +34,7 @@ web. Xively provides a fantastic development portal and prototyping accounts are
34
34
  spec.add_development_dependency "webmock"
35
35
 
36
36
  # Runtime dependencies
37
- spec.add_runtime_dependency "log4r", "~> 1.1"
37
+ spec.add_runtime_dependency "logging", "~> 1.8"
38
38
  spec.add_runtime_dependency "xively-rb", "~> 0.2"
39
39
  spec.add_runtime_dependency "bigdecimal", "~> 1.2"
40
40
 
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.3
4
+ version: 0.1.4
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-04-03 00:00:00.000000000 Z
11
+ date: 2014-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,19 +67,19 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: log4r
70
+ name: logging
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ~>
74
74
  - !ruby/object:Gem::Version
75
- version: '1.1'
75
+ version: '1.8'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
- version: '1.1'
82
+ version: '1.8'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: xively-rb
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -133,7 +133,7 @@ files:
133
133
  - lib/xively-rb-connector/connection.rb
134
134
  - lib/xively-rb-connector/datastream.rb
135
135
  - lib/xively-rb-connector/device.rb
136
- - lib/xively-rb-connector/logging.rb
136
+ - lib/xively-rb-connector/logger.rb
137
137
  - lib/xively-rb-connector/version.rb
138
138
  - spec/spec_helper.rb
139
139
  - spec/support/fixtures/feed_100000000.json
@@ -1,25 +0,0 @@
1
- require 'log4r'
2
- include Log4r
3
- module XivelyConnector
4
-
5
- module Logging
6
-
7
- def logger
8
- @logger ||= XivelyConnector::Logging.logger
9
- end
10
-
11
- def self.logger
12
- @logger ||= self.configure_logger_for(self.class.name)
13
- end
14
-
15
- def self.configure_logger_for(classname)
16
- l = Logger.new(classname)
17
- l.level = ERROR
18
- l.trace = false
19
- l.add Log4r::Outputter.stderr
20
- l
21
- end
22
-
23
- end
24
-
25
- end