Permalink
Browse files

Add debugging for the pachube service.

  • Loading branch information...
1 parent 0f810d2 commit 0a0693706cfeed30f5509376c011732f7f6338d6 @mcolyer committed Feb 10, 2012
View
7 bin/smartermeter
@@ -6,6 +6,11 @@ begin
rescue Exception
end
require 'smartermeter'
+require 'trollop'
-interface = SmarterMeter::Interfaces::CLI.new
+options = Trollop::options do
+ opt :debug, "Enable debugging statements", :default => false
+end
+
+interface = SmarterMeter::Interfaces::CLI.new(options)
SmarterMeter::Daemon.new(interface).start
View
2 lib/smartermeter/daemon.rb
@@ -208,7 +208,7 @@ def upload(date, samples)
case @config[:transport]
when :pachube
@ui.log.info("Uploading #{date} to Pachube")
- transport = SmarterMeter::Services::Pachube.new(@config[:pachube])
+ transport = SmarterMeter::Services::Pachube.new(@ui, @config[:pachube])
if transport.upload(samples)
@ui.log.info("Upload for #{date} complete")
else
View
12 lib/smartermeter/interfaces/cli.rb
@@ -3,11 +3,21 @@
module SmarterMeter
module Interfaces
class CLI
+ def initialize(options)
+ @options = options
+ end
+
# Returns a logger like interface to log errors and warnings to.
def log
return @logger if @logger
@logger = Logger.new STDOUT
- @logger.level = Logger::INFO
+
+ if @options[:debug]
+ @logger.level = Logger::DEBUG
+ else
+ @logger.level = Logger::INFO
+ end
+
@logger
end
View
4 lib/smartermeter/services/pachube.rb
@@ -3,7 +3,8 @@
module SmarterMeter
module Services
class Pachube
- def initialize(config)
+ def initialize(ui, config)
+ @ui = ui
@config = config
raise "The Pachube token must be configured" unless @config[:api_key]
raise "The Pachube feed id must be configured" unless @config[:feed_id]
@@ -22,6 +23,7 @@ def upload(samples)
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
res, body = http.post(url.path, request_body(samples), {"X-PachubeApiKey" => @config[:api_key], "Content-Type" => "text/csv"})
+ @ui.log.debug("Pacube Response: #{res}")
case res
when Net::HTTPSuccess
true
View
1 smartermeter.gemspec
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
s.add_dependency('rest-client', ["= 1.6.1"])
s.add_dependency('json_pure', ["= 1.5.1"])
s.add_dependency('rubyzip', ["= 0.9.5"])
+ s.add_dependency('trollop', ["= 1.16.2"])
## List your development dependencies here. Development dependencies are
## those that are only needed during development
View
4 spec/services/pachube_spec.rb
@@ -3,7 +3,9 @@
describe SmarterMeter::Services::Pachube do
before(:each) do
- @subject = SmarterMeter::Services::Pachube.new(:api_key => "secret",
+ @ui = mock("UI")
+ @subject = SmarterMeter::Services::Pachube.new(@ui,
+ :api_key => "secret",
:feed_id => "1",
:datastream_id => "1")
data = File.read(File.join($FIXTURES_DIR, 'data.xml'))

0 comments on commit 0a06937

Please sign in to comment.