Class: CurrentCostDaemon::Publishers::CarbonDiet
- Inherits:
-
Object
- Object
- CurrentCostDaemon::Publishers::CarbonDiet
- Defined in:
- lib/currentcostd/publishers/carbondiet.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(config) ⇒ CarbonDiet
constructor
A new instance of CarbonDiet.
- #update(reading) ⇒ Object
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_section ⇒ Object
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 69 70 71 72 73 74 75 76 |
# 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, ideally. If it's 3am, upload history # if we have it. # This is horribly hacky, it post every time there is day history during # this hour. Also the carbondiet code at the other end is hacky. # All this needs improving. puts reading.to_yaml puts "stuff" if !reading.history.nil? && reading.hour == 3 puts "Storing in Carbon Diet..." # 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][0] end end end end # Send data http = Net::HTTP.new('www.carbondiet.org') http.start http.request(post) puts "done" end rescue puts "Something went wrong (carbondiet)!" puts $!.inspect nil end |