Class: RubyFlashbake

Inherits:
Object
  • Object
show all
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

Copyright © 2009 Cory Ondrejka. All rights reserved. See License.txt for licensing details.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyFlashbake

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

#configurationObject (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_configObject



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_commentObject



159
160
161
162
163
# File 'lib/rubyflashbake/core.rb', line 159

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
# 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])
        $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} init`
        $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} config user.name \"#{@configuration[:GIT][:NAME]}\"`
        $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} config user.email \"#{@configuration[:GIT][:EMAIL]}\"`
        File.open("#{dir}/.gitignore", "w")  do |file| 
          file.puts ".DS_Store\n.message.tmp\n"
        end
        puts "Initialized git in #{dir}"
        @configuration[:GIT_SETUP] = true
      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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubyflashbake/core.rb', line 65

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
    if File.exists?("#{dir}/.git/config")
      contents = File.read("#{dir}/.git/config")
    end
    if contents =~ /\[remote \"origin\"\]/
      @configuration[:GITHUB_SETUP] = true
    else
      if !@configuration[:GITHUB_SETUP] && @configuration[:GIT][:GITHUB_DATA][:GITHUB_URI] && @configuration[:GIT][:GITHUB_DATA][:GITHUB_ID] && @configuration[:GIT][:GITHUB_DATA][:GITHUB_REPOSITORY]
        $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} 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
  $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} pull origin master`
  return true
end

#do_internetObject



9
10
11
12
13
14
15
16
17
# File 'lib/rubyflashbake/plugins/internet.rb', line 9

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_locationObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubyflashbake/plugins/location.rb', line 9

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_pluginsObject



153
154
155
156
157
# File 'lib/rubyflashbake/core.rb', line 153

def do_plugins
  @plugins.each do |p|
    send "do_#{p.to_s.downcase}"
  end
end

#do_timeObject



7
8
9
10
11
# File 'lib/rubyflashbake/plugins/time.rb', line 7

def do_time
  if @configuration[:PLUGIN][:TIME][:ACTIVE]
    @configuration[:OUTPUT].push "#{Time.now.ctime} #{Time.now.zone}"
  end
end

#do_twitterObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubyflashbake/plugins/twitter.rb', line 10

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_weatherObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubyflashbake/plugins/weather.rb', line 8

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rubyflashbake/core.rb', line 92

def git_commit(dir, message)
  if @configuration[:GIT_SETUP] == true
    File.open("#{dir}/.message.tmp", "w") {|file| file.puts message}
    $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} add .`
    $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} commit -a --file=.message.tmp`
    File.delete("#{dir}/.message.tmp")
    if @configuration[:INTERNET_ALIVE] && @configuration[:GIT][:USE_GITHUB] && @configuration[:GITHUB_SETUP]
      $stderr.puts `git --git-dir=#{dir}/.git --work-tree=#{dir} push origin master`
    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_pluginsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubyflashbake/core.rb', line 104

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



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rubyflashbake/core.rb', line 118

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



132
133
134
135
136
137
138
139
140
141
# File 'lib/rubyflashbake/core.rb', line 132

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_watchersObject



143
144
145
146
147
# File 'lib/rubyflashbake/core.rb', line 143

def start_directory_watchers
  @dw.each do |dw| 
    dw.start
  end
end

#stop_directory_watchersObject



149
150
151
# File 'lib/rubyflashbake/core.rb', line 149

def stop_directory_watchers
  @dw.each {|dw| dw.stop}
end