Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Gem cleanup and refactoring. Bump version to 0.4.0.

  • Loading branch information...
commit cb966268d2701fa08b8410cb67538a71b33babcc 1 parent 65b454d
Matias Korhonen authored May 17, 2010
8  .gitignore
@@ -13,10 +13,16 @@ tmtags
13 13
 ## VIM
14 14
 *.swp
15 15
 
  16
+## Other editors
  17
+.idea
  18
+tags
  19
+
16 20
 ## PROJECT::GENERAL
17 21
 coverage
18 22
 rdoc
19 23
 pkg
  24
+.bundle
  25
+*.gem
20 26
 
21 27
 ## PROJECT::SPECIFIC
22  
-.idea
  28
+
9  Gemfile
... ...
@@ -0,0 +1,9 @@
  1
+source "http://rubygems.org"
  2
+
  3
+# Will automatically pull in this gem and all its
  4
+# dependencies specified in the gemspec
  5
+gem "wol", :path => File.expand_path("..", __FILE__)
  6
+
  7
+# These are development dependencies 
  8
+gem "rake"
  9
+gem "rspec", "2.0.0.beta.8"
59  Rakefile
... ...
@@ -1,55 +1,14 @@
1  
-require 'rubygems'
2  
-require 'rake'
  1
+require "bundler"
  2
+Bundler.setup
3 3
 
4  
-begin
5  
-  require 'jeweler'
6  
-  Jeweler::Tasks.new do |gem|
7  
-    gem.name = "wol"
8  
-    gem.summary = %Q{Ruby WoL}
9  
-    gem.description = %Q{Ruby Wake-On-LAN}
10  
-    gem.email = "korhonen.matt@gmail.com"
11  
-    gem.homepage = "http://github.com/k33l0r/wol"
12  
-    gem.authors = ["Matias Korhonen"]
13  
-    gem.rubyforge_project = 'wol'
14  
-    gem.add_development_dependency "rspec", ">= 0"
15  
-    # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16  
-  end
17  
-rescue LoadError
18  
-  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19  
-end
20  
-
21  
-require 'spec/rake/spectask'
22  
-Spec::Rake::SpecTask.new(:spec) do |spec|
23  
-  spec.libs << 'lib' << 'spec'
24  
-  spec.spec_files = FileList['spec/**/*_spec.rb']
25  
-end
26  
-
27  
-Spec::Rake::SpecTask.new(:rcov) do |spec|
28  
-  spec.libs << 'lib' << 'spec'
29  
-  spec.pattern = 'spec/**/*_spec.rb'
30  
-  spec.rcov = true
31  
-end
32  
-
33  
-task :spec => :check_dependencies
34  
-
35  
-task :default => :spec
  4
+require "rspec/core/rake_task"
  5
+Rspec::Core::RakeTask.new(:spec)
36 6
 
37  
-require 'rake/rdoctask'
38  
-gem 'rdoc'
39  
-require 'rdoc/rdoc'
  7
+gemspec = eval(File.read("wol.gemspec"))
40 8
 
41  
-Rake::RDocTask.new do |rdoc|
42  
-  version = File.exist?('VERSION') ? File.read('VERSION') : ""
  9
+task :build => "#{gemspec.full_name}.gem"
43 10
 
44  
-  rdoc.rdoc_dir = 'rdoc'
45  
-  rdoc.title = "Ruby Wake-On-LAN #{version}"
46  
-  rdoc.rdoc_files.include('README*')
47  
-  rdoc.rdoc_files.include('COPYING')
48  
-  rdoc.rdoc_files.include('LICENSE')
49  
-  rdoc.rdoc_files.include('lib/**/*.rb')
50  
-  
51  
-  rdoc.options += [
52  
-          '-HN',
53  
-          '-f', 'darkfish',
54  
-  ]
  11
+file "#{gemspec.full_name}.gem" => gemspec.files + ["wol.gemspec"] do
  12
+  system "gem build wol.gemspec"
  13
+  system "gem install wol-#{Wol::VERSION}.gem"
55 14
 end
1  VERSION
... ...
@@ -1 +0,0 @@
1  
-0.3.3
5  bin/wol
... ...
@@ -1,7 +1,6 @@
1 1
 #!/usr/bin/env ruby
2 2
 
3  
-$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4  
-
5 3
 require 'wol'
  4
+wol = Wol::Runner.new
6 5
 
7  
-exit Wol::Runner.run!(ARGV)
  6
+exit wol.run(ARGV)
3  lib/wol.rb
... ...
@@ -1,3 +1,4 @@
1 1
 require 'wol/wakeonlan'
2 2
 require 'wol/runner'
3  
-require 'wol/parsefile'
  3
+require 'wol/parsefile'
  4
+require 'wol/version'
185  lib/wol/runner.rb
@@ -5,10 +5,9 @@
5 5
 #
6 6
 require 'optparse'
7 7
 
8  
-module Wol
9  
-  # Run the Ruby-Wake-On-LAN command, +wol+, and parse the given options
10  
-  module Runner
11  
-    @sample_file = %{# File structure
  8
+# Run the Ruby-Wake-On-LAN command, +wol+, and parse the given options
  9
+class Wol::Runner  
  10
+  @sample_file = %{# File structure
12 11
 # --------------
13 12
 # - blank lines are ignored
14 13
 # - comment lines are ignored (lines starting with a hash mark '#')
@@ -27,6 +26,7 @@ module Runner
27 26
 07:09:09:0A:0B:0C   example.com
28 27
 0D:0E:0F:00:10:11}
29 28
 
  29
+  def initialize
30 30
     @options = { :quiet => false,
31 31
                  :address => "255.255.255.255",
32 32
                  :port => 9,
@@ -34,125 +34,120 @@ module Runner
34 34
                  :count => 3,
35 35
                  :macs => [],
36 36
                  :file => nil,
37  
-                 :nothing_to_wake => false}
38  
-
39  
-    # Parse the give arguments
40  
-    def self.parse!(args)
41  
-      args = ["-h"] if args.empty?
  37
+                :nothing_to_wake => false }                
  38
+  end
42 39
 
43  
-      opts = OptionParser.new do |opts|
44  
-        # Set a banner, displayed at the top
45  
-        # of the help screen.
46  
-        opts.banner = "Ruby Wake-On-LAN #{version}\nUsage: wol -i ADDRESS ff:ff:ff:ff:ff:ff"
  40
+  # Parse the give arguments
  41
+  def parse!(args)
  42
+    args = ["-h"] if args.empty?
47 43
 
48  
-        opts.separator "\nSpecific options:"
  44
+    opts = OptionParser.new do |opts|
  45
+      # Set a banner, displayed at the top
  46
+      # of the help screen.
  47
+      opts.banner = "Ruby Wake-On-LAN #{Wol::VERSION}\nUsage: wol -i ADDRESS ff:ff:ff:ff:ff:ff"
49 48
 
50  
-        opts.on( '-q', '--quiet', 'No console output' ) do
51  
-          @options[:quiet] = true
52  
-        end
  49
+      opts.separator "\nSpecific options:"
53 50
 
54  
-        opts.on( '-i', '--address 255.255.255.255', 'Set the destination address' ) do |address|
55  
-          @options[:address] = address
56  
-        end
  51
+      opts.on( '-q', '--quiet', 'No console output' ) do
  52
+        @options[:quiet] = true
  53
+      end
57 54
 
58  
-        opts.on( '-p', '--port 9', Integer, 'Set the destination port' ) do |port|
59  
-          @options[:port] = port
60  
-        end
  55
+      opts.on( '-i', '--address 255.255.255.255', 'Set the destination address' ) do |address|
  56
+        @options[:address] = address
  57
+      end
61 58
 
62  
-        opts.on( '-d', '--delay 0.01', Float, 'Delay between sending packets in seconds') do |delay|
63  
-          @options[:delay] = delay
64  
-        end
  59
+      opts.on( '-p', '--port 9', Integer, 'Set the destination port' ) do |port|
  60
+        @options[:port] = port
  61
+      end
65 62
 
66  
-        opts.on( '-c', '--count 3', Integer, 'Number of packets to send. Default 3') do |count|
67  
-          @options[:count] = count
68  
-        end
  63
+      opts.on( '-d', '--delay 0.01', Float, 'Delay between sending packets in seconds') do |delay|
  64
+        @options[:delay] = delay
  65
+      end
69 66
 
70  
-        opts.on( '-f', '--file FILE', 'TODO: Uses a file as a source of addresses') do |file|
71  
-          @options[:file] = file
72  
-        end
  67
+      opts.on( '-c', '--count 3', Integer, 'Number of packets to send. Default 3') do |count|
  68
+        @options[:count] = count
  69
+      end
73 70
 
74  
-        opts.separator "\nCommon options:"
  71
+      opts.on( '-f', '--file FILE', 'TODO: Uses a file as a source of addresses') do |file|
  72
+        @options[:file] = file
  73
+      end
75 74
 
76  
-        opts.on_tail( '-h', '--help', 'Display this message' ) do
77  
-          @options[:nothing_to_wake] = true
78  
-          puts opts
79  
-        end
  75
+      opts.separator "\nCommon options:"
80 76
 
81  
-        opts.on_tail( '-s', '--sample-file', 'Display a sample file') do
82  
-          @options[:nothing_to_wake] = true
83  
-          puts @sample_file
84  
-        end
  77
+      opts.on_tail( '-h', '--help', 'Display this message' ) do
  78
+        @options[:nothing_to_wake] = true
  79
+        puts opts
  80
+      end
85 81
 
86  
-        opts.on_tail( '-v', '--version', 'Show version') do
87  
-          @options[:nothing_to_wake] = true
88  
-          puts "Ruby Wake-On-LAN #{version}"
89  
-        end
  82
+      opts.on_tail( '-s', '--sample-file', 'Display a sample file') do
  83
+        @options[:nothing_to_wake] = true
  84
+        puts @sample_file
90 85
       end
91 86
 
92  
-      begin
93  
-        opts.parse!(args)
  87
+      opts.on_tail( '-v', '--version', 'Show version') do
  88
+        @options[:nothing_to_wake] = true
  89
+        puts "Ruby Wake-On-LAN #{Wol::VERSION}"
  90
+      end
  91
+    end
94 92
 
95  
-        if @options[:file].nil?
96  
-          args.each do |arg|
97  
-            @options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
98  
-          end
  93
+    begin
  94
+      opts.parse!(args)
99 95
 
100  
-          @options[:macs].uniq!
  96
+      if @options[:file].nil?
  97
+        args.each do |arg|
  98
+          @options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
101 99
         end
102 100
 
103  
-        return @options
104  
-      rescue OptionParser::InvalidOption => e
105  
-        STDERR.puts e.message, "\n", opts
106  
-        return -1
  101
+        @options[:macs].uniq!
107 102
       end
108  
-    end
109 103
 
110  
-    # Return the version from the VERSION file if it exists
111  
-    def self.version
112  
-      File.exist?('VERSION') ? File.read('VERSION') : ""
  104
+      return @options
  105
+    rescue OptionParser::InvalidOption => e
  106
+      STDERR.puts e.message, "\n", opts
  107
+      return -1
113 108
     end
  109
+  end
114 110
 
115  
-    # Send WOL MagicPackets based on the parsed options
116  
-    def self.wake!
117  
-      if @options[:file]
118  
-        hosts = ParseFile.read_and_parse_file(@options[:file])
  111
+  # Send WOL MagicPackets based on the parsed options
  112
+  def wake
  113
+    if @options[:file]
  114
+      hosts = ParseFile.read_and_parse_file(@options[:file])
119 115
 
120  
-        hosts.each do |host|
121  
-          @options[:address], @options[:macs], @options[:port] = host[:address], host[:mac], host[:port]
  116
+      hosts.each do |host|
  117
+        @options[:address], @options[:macs], @options[:port] = host[:address], host[:mac], host[:port]
122 118
 
123  
-          message = WakeOnLan.new(@options).wake.to_s
124  
-          puts message unless options[:quiet]
125  
-
126  
-          return 0
127  
-        end
128  
-      elsif !@options[:macs].empty?
129  
-        @options[:macs].each do |mac|
130  
-          @options[:mac] = mac
131  
-          message = WakeOnLan.new(@options).wake.to_s
132  
-          puts message unless @options[:quiet]
133  
-        end
  119
+        message = WakeOnLan.new(@options).wake.to_s
  120
+        puts message unless options[:quiet]
134 121
 
135 122
         return 0
136  
-      elsif @options[:macs].empty? && @options[:file].nil? && !@options[:nothing_to_wake]
137  
-        raise Exception, "You must supply a MAC address or a file"
138  
-      else
139  
-        return -1
140 123
       end
141  
-    end
142  
-
143  
-    # Parse the command line options, then use them to wake up any given hosts.
144  
-    def self.run!(argv)
145  
-      begin
146  
-        parse!(argv)
147  
-
148  
-        return wake!
149  
-      rescue SocketError => e
150  
-        puts e.message
151  
-        return -1
152  
-      rescue Exception => e
153  
-        STDERR.puts e.message
154  
-        return -1
  124
+    elsif !@options[:macs].empty?
  125
+      @options[:macs].each do |mac|
  126
+        @options[:mac] = mac
  127
+        message = WakeOnLan.new(@options).wake.to_s
  128
+        puts message unless @options[:quiet]
155 129
       end
  130
+
  131
+      return 0
  132
+    elsif @options[:macs].empty? && @options[:file].nil? && !@options[:nothing_to_wake]
  133
+      raise Exception, "You must supply a MAC address or a file"
  134
+    else
  135
+      return -1
156 136
     end
157 137
   end
  138
+
  139
+  # Parse the command line options, then use them to wake up any given hosts.
  140
+  def run(argv)
  141
+    begin
  142
+      parse!(argv)
  143
+
  144
+      return wake
  145
+    rescue SocketError => e
  146
+      puts e.message
  147
+      return -1
  148
+    rescue Exception => e
  149
+      STDERR.puts e.message
  150
+      return -1
  151
+    end
  152
+  end    
158 153
 end
3  lib/wol/version.rb
... ...
@@ -0,0 +1,3 @@
  1
+module Wol
  2
+  VERSION = "0.4.0"
  3
+end
2  spec/parsefile_spec.rb
... ...
@@ -1,4 +1,4 @@
1  
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  1
+require "spec_helper"
2 2
 
3 3
 EXPECTED = [{:port=>9, :mac=>"01:02:03:04:05:06", :address=>"192.168.1.255"},
4 4
             {:port=>12, :mac=>"01:02:03:04:05:06", :address=>"192.168.2.230"},
1  spec/spec.opts
... ...
@@ -1 +0,0 @@
1  
---color
15  spec/spec_helper.rb
... ...
@@ -1,9 +1,10 @@
1  
-$LOAD_PATH.unshift(File.dirname(__FILE__))
2  
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3  
-require 'wol'
4  
-require 'spec'
5  
-require 'spec/autorun'
  1
+require "rubygems"
  2
+require "bundler"
  3
+Bundler.setup
6 4
 
7  
-Spec::Runner.configure do |config|
  5
+require "rspec"
  6
+require "wol"
  7
+
  8
+Rspec.configure do |config|
8 9
   
9  
-end
  10
+end
6  spec/wakeonlan_spec.rb
... ...
@@ -1,4 +1,4 @@
1  
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  1
+require "spec_helper"
2 2
 
3 3
 describe "Wake-On-LAN" do
4 4
   it "should send a MagicPacket to the broadcast addresses" do
@@ -24,4 +24,8 @@
24 24
   it "should return nil if quiet is set to true" do
25 25
     Wol::WakeOnLan.new(:quiet => true).wake.should == nil
26 26
   end
  27
+  
  28
+  it "should return the version number as a string" do
  29
+    Wol::VERSION.class.should == String
  30
+  end
27 31
 end
28  wol.gemspec
... ...
@@ -0,0 +1,28 @@
  1
+# -*- encoding: utf-8 -*-
  2
+
  3
+require File.expand_path("../lib/wol/version", __FILE__)
  4
+
  5
+Gem::Specification.new do |s|
  6
+  s.name        = "wol"
  7
+  s.version     = Wol::VERSION
  8
+  s.platform    = Gem::Platform::RUBY
  9
+  s.authors     = ["Matias Korhonen"]
  10
+  s.email       = ["me@matiaskorhonen.fi"]
  11
+  s.homepage    = "http://wol.matiaskorhonen.fi"
  12
+  s.summary     = "Ruby Wake-On-LAN"
  13
+  s.description = "Send Wake-On-LAN magic packets from Ruby or from CLI"
  14
+
  15
+  s.required_rubygems_version = ">= 1.3.6"
  16
+  
  17
+  s.rubyforge_project         = "wol"
  18
+
  19
+  # Files that aren't .rb files
  20
+  s.files        = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "COPYING", "*.rdoc"]
  21
+  s.require_path = 'lib'
  22
+
  23
+  # If you need an executable, add it here
  24
+  s.executables = ["wol"]
  25
+
  26
+  # If you have C extensions, uncomment this line
  27
+  # s.extensions = "ext/extconf.rb"
  28
+end

0 notes on commit cb96626

Please sign in to comment.
Something went wrong with that request. Please try again.