Class: SmarterMeter::Services::GooglePowerMeter

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GooglePowerMeter

Returns a new instance of GooglePowerMeter.



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

def initialize(config)
  @config = config
  raise "The Google PowerMeter token must be configured" unless @config[:token]
  raise "The Google PowerMeter variable must be configured" unless @config[:variable]
end

Instance Method Details

#request_body(samples) ⇒ Object

Creates the proper XML request to send to Google.

Returns the proper atom/xml response to send to google



36
37
38
39
# File 'lib/smartermeter/services/google_powermeter.rb', line 36

def request_body(samples)
  template = ERB.new(File.read(File.join(File.dirname(__FILE__), "google_powermeter.erb")))
  template.result(binding)
end

#upload(samples) ⇒ Object

Public: Uploads an array of Samples to Google PowerMeter

samples - An array of samples to upload

Returns true on success and false otherwise



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

def upload(samples)
  url = URI.parse('https://www.google.com/powermeter/feeds/event')
  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), {"Authorization" => "AuthSub token=\"#{@config[:token]}\"", "Content-Type" => "application/atom+xml"})
  case res
  when Net::HTTPSuccess
    true
  else
    false
  end
end