1. Richo Healey
  2. twat

Commits

Richo Healey  committed 81566c7

Configurable polling interval

Warns you for slightly insane values, but the lack of an abstraction
around output is slightly worrying.

  • Participants
  • Parent commits 4f0691c
  • Branches master

Comments (0)

Files changed (5)

File lib/twat.rb

View file
  • Ignore whitespace
         puts "Your config file is out of date. Run with --update-config to rememdy"
       rescue InvalidBool
         puts "Invalid value, valid values are #{Options::BOOL_VALID.join("|")}"
+      rescue InvalidInt
+        puts "Invalid value, must be an integer"
       end
     end
   end

File lib/twat/actions.rb

View file
  • Ignore whitespace
       begin
         while true do
           last_id = process_followed(tweets) if tweets.any?
-          # Sleep 15 seconds between requests
-          # If left running at all times, that's 240 requests per hour, well
-          # under the limit
-          # sleep 15
-          # Disregard that, once per 60 seconds is plenty
-          sleep 60
+          sleep config.polling_interval
           tweets = Twitter.home_timeline(:since_id => last_id)
         end
       rescue Interrupt

File lib/twat/config.rb

View file
  • Ignore whitespace
       Options.bool_true?(beep)
     end
 
+    def polling_interval
+      config[:polling_interval] || 60
+    end
+
     def default_account
       raise NoDefaultAccount unless config.include?(:default)
       return config[:default].to_sym

File lib/twat/exceptions.rb

View file
  • Ignore whitespace
   class ConfigVersionIncorrect < Exception; end
   class InvalidSetOpt < Exception; end
   class InvalidBool < Exception; end
+  class InvalidInt < Exception; end
 end

File lib/twat/options.rb

View file
  • Ignore whitespace
       BOOL_VALS.include?(val.to_s.downcase)
     end
 
+    def self.int_valid?(val)
+      !!/^[0-9]+$/.match(val)
+    end
+
     # A set of wrappers around the global config object to set given attributes
     # Catching failures is convenient because of the method_missing? hook
     def method_missing(sym, *args, &block)
       config.save!
     end
 
+    def polling_interval=(value)
+      raise InvalidInt unless Options.int_valid?(value)
+      val = value.to_i
+      if val < 15 then
+        puts "Polling intervals of < 15secs will exceed your daily API requests"
+        exit
+      else
+        config[:polling_interval] = val
+        config.save!
+      end
+    end
+
   end
 end