Class: CurrentCostDaemon::Publishers::CarbonDiet

Inherits:
Object
  • Object
show all
Defined in:
lib/currentcostd/publishers/carbondiet.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CarbonDiet

Returns a new instance of CarbonDiet.



33
34
35
36
37
# File 'lib/currentcostd/publishers/carbondiet.rb', line 33

def initialize(config)
  @account = config['carbondiet']['account_id']
  @username = config['carbondiet']['username']
  @password = config['carbondiet']['password']
end

Class Method Details

.config_sectionObject



29
30
31
# File 'lib/currentcostd/publishers/carbondiet.rb', line 29

def self.config_section
  'carbondiet'
end

Instance Method Details

#update(reading) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/currentcostd/publishers/carbondiet.rb', line 39

def update(reading)
  # Carbon Diet is daily, so we only want to do something if there is
  # history data, and only once a day, really. Say around 5am, why not.
  if !reading.history.nil? && reading.hour == 5
    # Create http post request
    post = Net::HTTP::Post.new("/data_entry/electricity/#{@account}/currentcost")
    post.basic_auth(@username, @password)
    # Add XML document
    xml = Builder::XmlMarkup.new
    xml.instruct!
    post.body = xml.data do
      reading.history[:days].each_index do |i|
        unless reading.history[:days][i].nil?
          xml.entry do
            xml.date Date.today - i
            xml.value reading.history[:days][i]
          end
        end
      end
    end
    # Send data
    http = Net::HTTP.new('www.carbondiet.org')
    http.start
    http.request(post)
  end
rescue
  puts "Something went wrong (carbondiet)!"
  puts $!.inspect
  nil
end