Class: RubyFlashbake
- Inherits:
-
Object
- Object
- RubyFlashbake
- Defined in:
- lib/rubyflashbake/plugins/time.rb,
lib/rubyflashbake/core.rb,
lib/rubyflashbake/plugins/twitter.rb,
lib/rubyflashbake/plugins/weather.rb,
lib/rubyflashbake/plugins/internet.rb,
lib/rubyflashbake/plugins/location.rb
Overview
time plug in Copyright © 2009 Cory Ondrejka. All rights reserved. See License.txt for licensing details.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #check_config ⇒ Object
- #checkin_comment ⇒ Object
- #configure_git(dir) ⇒ Object
- #configure_github(dir) ⇒ Object
- #do_internet ⇒ Object
- #do_location ⇒ Object
- #do_plugins ⇒ Object
- #do_time ⇒ Object
- #do_twitter ⇒ Object
- #do_weather ⇒ Object
- #git_commit(dir, message) ⇒ Object
-
#initialize ⇒ RubyFlashbake
constructor
A new instance of RubyFlashbake.
- #load_file(file) ⇒ Object
- #load_plugins ⇒ Object
- #setup_directory_watchers(basedir, &block) ⇒ Object
- #setup_watch_commits(bdir) ⇒ Object
- #start_directory_watchers ⇒ Object
- #stop_directory_watchers ⇒ Object
Constructor Details
#initialize ⇒ RubyFlashbake
Returns a new instance of RubyFlashbake.
12 13 14 15 16 17 |
# File 'lib/rubyflashbake/core.rb', line 12 def initialize @configuration = nil @filename = nil @dw = [] @plugins = [] end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/rubyflashbake/core.rb', line 10 def configuration @configuration end |
Instance Method Details
#check_config ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/rubyflashbake/core.rb', line 33 def check_config unless @configuration puts "Configuration not loaded before trying to work with it" puts "Please make sure code path loads configuration before trying to load plugins." exit 1 end end |
#checkin_comment ⇒ Object
165 166 167 168 169 |
# File 'lib/rubyflashbake/core.rb', line 165 def checkin_comment @configuration[:OUTPUT] = [] do_plugins @configuration[:OUTPUT].to_yaml end |
#configure_git(dir) ⇒ 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 65 |
# File 'lib/rubyflashbake/core.rb', line 41 def configure_git(dir) check_config unless @configuration[:GIT_SETUP] == true if File.directory?("#{dir}/.git") && File.exists?("#{dir}/.git/config") @configuration[:GIT_SETUP] = true else if (@configuration[:GIT][:NAME] && @configuration[:GIT][:EMAIL]) Dir.chdir("#{dir}") do puts `git init` puts `git config user.name "#{@configuration[:GIT][:NAME]}"` puts `git config user.email #{@configuration[:GIT][:EMAIL]}` File.open(".gitignore", "w") do |file| file.puts ".DS_Store\ncoverage/\n.message.tmp\n" end puts "Initialized git in #{dir}" @configuration[:GIT_SETUP] = true end else puts "Can't configure git without git :NAME and :EMAIL configured in config file." exit 1 end end end return true end |
#configure_github(dir) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rubyflashbake/core.rb', line 67 def configure_github(dir) check_config unless @configuration[:GIT_SETUP] == true puts "Trying to configure github without previous call to config_git." exit 1 end unless @configuration[:GITHUB_SETUP] == true contents = File.read("#{dir}/.git/config") if contents =~ /\[remote \"origin\"\]/ @configuration[:GITHUB_SETUP] = true else Dir.chdir("#{dir}") do if !@configuration[:GITHUB_SETUP] && @configuration[:GIT][:GITHUB_DATA][:GITHUB_URI] && @configuration[:GIT][:GITHUB_DATA][:GITHUB_ID] && @configuration[:GIT][:GITHUB_DATA][:GITHUB_REPOSITORY] puts `git remote add origin #{configuration[:GIT][:GITHUB_DATA][:GITHUB_URI]}:#{@configuration[:GIT][:GITHUB_DATA][:GITHUB_ID]}/#{@configuration[:GIT][:GITHUB_DATA][:GITHUB_REPOSITORY]}.git` puts "Initialized github in #{dir}" @configuration[:GITHUB_SETUP] = true else puts "Can't configure github without :GITHUB_URI, :GITHUB_ID, and :GITHUB_REPOSITORY configured in config file." exit 1 end end end end Dir.chdir("#{dir}") do puts `git pull origin master` end return true end |
#do_internet ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/rubyflashbake/plugins/internet.rb', line 10 def do_internet @configuration[:INTERNET_ALIVE] = false if @configuration[:PLUGIN][:INTERNET][:ACTIVE] @configuration[:INTERNET_ALIVE] = Ping.pingecho(@configuration[:PLUGIN][:INTERNET][:OPTIONAL_HASH][:INTERNET_TEST_URI],2,80) unless @configuration[:INTERNET_ALIVE] @configuration[:OUTPUT].push "offline" end end end |
#do_location ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rubyflashbake/plugins/location.rb', line 10 def do_location if @configuration[:PLUGIN][:LOCATION][:ACTIVE] && @configuration[:INTERNET_ALIVE] location = Net::HTTP.get(URI.parse("http://j.maxmind.com/app/geoip.js")) location_array = location.scan(/return \'(.*)\'/) @configuration[:LOCATION_CITY] = "#{location_array[2][0]}" @configuration[:LOCATION_STATE] = "#{location_array[3][0]}" @configuration[:LOCATION_COUNTRY] = "#{location_array[0][0]}" if @configuration[:LOCATION_COUNTRY] == "US" @configuration[:LOCATION_CACHE] = "#{@configuration[:LOCATION_CITY]},#{@configuration[:LOCATION_STATE]},#{@configuration[:LOCATION_COUNTRY]}" else @configuration[:LOCATION_CACHE] = "#{@configuration[:LOCATION_CITY]},#{@configuration[:LOCATION_COUNTRY]}" end @configuration[:OUTPUT].push "#{@configuration[:LOCATION_CACHE]} #{location_array[5][0]},#{location_array[6][0]}" end end |
#do_plugins ⇒ Object
159 160 161 162 163 |
# File 'lib/rubyflashbake/core.rb', line 159 def do_plugins @plugins.each do |p| send "do_#{p.to_s.downcase}" end end |
#do_time ⇒ Object
8 9 10 11 12 |
# File 'lib/rubyflashbake/plugins/time.rb', line 8 def do_time if @configuration[:PLUGIN][:TIME][:ACTIVE] @configuration[:OUTPUT].push "#{Time.now.ctime} #{Time.now.zone}" end end |
#do_twitter ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rubyflashbake/plugins/twitter.rb', line 11 def do_twitter @configuration[:OUTPUT] = ["Couldn't reach twitter"] if @configuration[:PLUGIN][:TIME][:ACTIVE] && @configuration[:INTERNET_ALIVE] && @configuration[:PLUGIN][:TWITTER][:OPTIONAL_HASH][:TWITTER_ID] xml = Net::HTTP.get(URI.parse("http://twitter.com/statuses/user_timeline/#{@configuration[:PLUGIN][:TWITTER][:OPTIONAL_HASH][:TWITTER_ID]}.xml")) doc = Hpricot(xml) (0..2).each do |i| if ((doc/"status"/"created_at")[i]) @configuration[:OUTPUT].push "Twitter: #{(doc/"status"/"created_at")[i].inner_html} #{(doc/"status"/"text")[i].inner_html}" end end end end |
#do_weather ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rubyflashbake/plugins/weather.rb', line 9 def do_weather @configuration[:OUTPUT].push "No valid weather found" if @configuration[:PLUGIN][:WEATHER][:ACTIVE] && @configuration[:INTERNET_ALIVE] if @configuration[:LOCATION_CACHE] weather = Net::HTTP.get(URI.parse("http://www.google.com/ig/api?weather=#{@configuration[:LOCATION_CACHE]}")) if weather =~ /temp_f data=\"(\d+)\"/ && weather =~ /condition data=\"([\s\w]+)\"/ @configuration[:OUTPUT].push "#{weather.scan(/temp_f data=\"(\d+)\"/)[0][0]} #{weather.scan(/condition data=\"([\s\w]+)\"/)[0][0]}" end end end end |
#git_commit(dir, message) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rubyflashbake/core.rb', line 96 def git_commit(dir, ) if @configuration[:GIT_SETUP] == true Dir.chdir("#{dir}") do File.open(".message.tmp", "w") {|file| file.puts } puts `git add .` puts `git commit -a --file=.message.tmp` File.delete(".message.tmp") if @configuration[:INTERNET_ALIVE] && @configuration[:GIT][:USE_GITHUB] && @configuration[:GITHUB_SETUP] puts `git push origin master` end end end end |
#load_file(file) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rubyflashbake/core.rb', line 19 def load_file(file) # load the configuration file @filename = file begin @configuration = YAML.load_file(file) @configuration[:GIT_SETUP] = false @configuration[:GITHUB_SETUP] = false rescue SystemCallError => e puts "Configuration file \"#{file}\" not found!" puts "Please make sure code path loads configuration before trying to load plugins." exit 1 end end |
#load_plugins ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rubyflashbake/core.rb', line 110 def load_plugins check_config @configuration[:PLUGIN_DIRECTORIES].each do |dir| libdir = File.dirname(__FILE__) pattern = File.join(libdir, "#{dir}", "*.rb") Dir.glob(pattern).each do |file| require file end end @configuration[:PLUGIN_CALL_ORDER].each {|p| @plugins.push p } end |
#setup_directory_watchers(basedir, &block) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rubyflashbake/core.rb', line 124 def setup_directory_watchers(basedir, &block) check_config dw = DirectoryWatcher.new("#{basedir}") dw.interval = @configuration[:DIRECTORY_MONITOR_INTERVAL] dw.stable = @configuration[:STABLE_INTERVALS] dw.glob = "**/*" dw.add_observer do |*events| block.call(events, basedir, checkin_comment) end @dw.push dw end |
#setup_watch_commits(bdir) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rubyflashbake/core.rb', line 138 def setup_watch_commits(bdir) setup_directory_watchers(bdir) do |events, basedir, func| events.each do |event| if event.type == :stable puts "#{event}" if @configuration[:VERBOSE] git_commit("#{basedir}", func) end end end end |
#start_directory_watchers ⇒ Object
149 150 151 152 153 |
# File 'lib/rubyflashbake/core.rb', line 149 def start_directory_watchers @dw.each do |dw| dw.start end end |
#stop_directory_watchers ⇒ Object
155 156 157 |
# File 'lib/rubyflashbake/core.rb', line 155 def stop_directory_watchers @dw.each {|dw| dw.stop} end |