Class: CurrentCostDaemon::Publishers::Twitter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Twitter

Returns a new instance of Twitter.



35
36
37
38
39
# File 'lib/currentcostd/publishers/twitter.rb', line 35

def initialize(config)
  @username = config['twitter']['username']
  @password = config['twitter']['password']
  @last_minute = Time.now.min - 1
end

Class Method Details

.config_sectionObject



31
32
33
# File 'lib/currentcostd/publishers/twitter.rb', line 31

def self.config_section
  'twitter'
end

Instance Method Details

#update(reading) ⇒ Object



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

def update(reading)
  return if reading.total_watts == 0
  # Tweet once a minute
  if Time.now.min != @last_minute
    # Store time
    @last_minute = Time.now.min
    message = "At the moment, I'm using #{reading.total_watts} watts"
    # Tweet
    puts "Tweeting..."
    req = Net::HTTP::Post.new("/statuses/update.json")
    req.basic_auth @username, @password
    req.set_form_data "status" => message
    http = Net::HTTP.new("twitter.com")
    http.start do
      response = http.request(req)
      raise response.body if (response.code[0] != "2")
    end
    puts "done"
  end
rescue
  puts "Something went wrong (twitter)!"
  puts $!.inspect
  nil
end