Class: SmarterMeter::Services::Pachube

Inherits:
Object
  • Object
show all
Defined in:
lib/smartermeter/services/pachube.rb

Instance Method Summary collapse

Constructor Details

#initialize(ui, config) ⇒ Pachube



6
7
8
9
10
11
12
# File 'lib/smartermeter/services/pachube.rb', line 6

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]
  raise "The Pachube datastream id must be configured" unless @config[:datastream_id]
end

Instance Method Details

#request_body(samples) ⇒ Object

Creates the proper XML request to send to Pachube

Returns the proper text/csv request to send to pachube



38
39
40
41
# File 'lib/smartermeter/services/pachube.rb', line 38

def request_body(samples)
  template = ERB.new(File.read(File.join(File.dirname(__FILE__), "pachube.erb")))
  template.result(binding).gsub(/^\n/, '')
end

#upload(samples) ⇒ Object

Public: Uploads an array of Samples to Pachube

samples - An array of samples to upload

Returns true on success and false otherwise



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/smartermeter/services/pachube.rb', line 19

def upload(samples)
  url = URI.parse("https://api.pachube.com/v2/feeds/#{@config[:feed_id]}/datastreams/#{@config[:datastream_id]}/datapoints.csv")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  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("Pachube Response: #{res}")
  case res
  when Net::HTTPSuccess
    true
  else
    false
  end
end