rcl 0.0.2 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,5 +1,5 @@
1
1
  == What
2
- Ramsey's Common Library version 0.0.3
2
+ Ramsey's Common Library version 0.0.5
3
3
 
4
4
  This is simply a collection of Ruby modules that perform useful tasks. There
5
5
  are methods that do simple things like check permissions on files and
@@ -34,6 +34,8 @@ Shell#unregister
34
34
  Shell#run
35
35
  Shell#history
36
36
 
37
+ SMTP#send
38
+
37
39
  Timestamp#new
38
40
  Timestamp#create
39
41
  Timestamp#modify
@@ -72,7 +74,7 @@ I added a Rakefile and a gem spec. To build the gem, just type:
72
74
 
73
75
  To install the gem, just type:
74
76
 
75
- sudo gem install rcl-0.0.3.gem
77
+ sudo gem install rcl-0.0.5.gem
76
78
 
77
79
  == Notes
78
80
  I haven't tested this code with YARV, so YMMV.
data/lib/rcl.rb CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  module RCL
7
7
  RCL::NAME = 'rcl'
8
- RCL::VERSION = '0.0.2'
8
+ RCL::VERSION = '0.0.5'
9
9
  RCL::COPYRIGHT = 'Copyright (c) 2008 Ramsey Dow'
10
10
  def self.copyright() RCL::COPYRIGHT end
11
11
  def self.version() RCL::VERSION end
@@ -21,8 +21,10 @@ require 'yaml_helper'
21
21
  $LOAD_PATH.unshift(File.dirname(__FILE__)+'/rcl')
22
22
 
23
23
  require 'base'
24
+ require 'http'
24
25
  require 'perms'
25
26
  require 'shell'
27
+ require 'smtp'
26
28
  require 'timestamp'
27
29
  require 'token'
28
30
  require 'url'
@@ -0,0 +1,126 @@
1
+ =begin
2
+
3
+ = Name
4
+ Term::ANSIColour v0.01 (unstable)
5
+
6
+ = Synopsis
7
+ require 'term/ansicolour'
8
+
9
+ print "Some ordinary text\n"
10
+ Term::ANSIColour.colour = ('red')
11
+ print "some red text..\n"
12
+ print "some more red text..\n"
13
+ Term::ANSIColour.colour = ('green')
14
+ print "some green text..\n"
15
+ Term::ANSIColour.colour = ('default')
16
+ puts "some ordinary text.."
17
+ puts "some magenta text".colour("magenta")
18
+ puts "some funky text".colour("cyan", "bold", "on_white", "blink")
19
+ puts "more ordinary text"
20
+
21
+ = Description
22
+ prints out text in funny colours
23
+
24
+ = Class Methods
25
+ --- Term::ANSIColour.colour
26
+ Gets and sets the default output text colour (none by default)
27
+
28
+ = Overridden Methods
29
+ --- print
30
+ output text in the colour specified by Term::ANSIColour.colour
31
+ --- String.colour( colour )
32
+ returns a string of the colour given in the argument (overriding Term::ANSIColour.colour).
33
+
34
+ = To Do
35
+ --- Get puts, printf, putc and write to output coloured text.
36
+ --- Write some proper documentation.
37
+ --- Make the interface a bit cleaner.
38
+ --- Allow people to use string modifiers, constants, or overridden output
39
+ methods depending on their taste.
40
+ --- Plug in a few enhancements such as regexp colouring, syntax highlighting
41
+ and the like.
42
+ --- Alias @@colour to @color
43
+ --- Learn how to code ruby before I break something.
44
+
45
+ = Credits
46
+ First version by Robert Ryan (rjr@rollmop.org) borrowing extensively from
47
+ the perl version by Russ Allbery and Zenin. Additional input by Mark Hulme-Jones.
48
+
49
+ = Copyright
50
+
51
+ Copyright 2002
52
+
53
+ This program is free software. You can redistribute it and/or modify
54
+ it under the same terms as Ruby itself.
55
+
56
+ =cut
57
+
58
+ =end
59
+
60
+ module Term
61
+ module ANSIColour
62
+ # XXX - why don't attr_reader and attr_writer work?
63
+ @@colour = ['reset']
64
+
65
+ def ANSIColour.colour
66
+ @@colour
67
+ end
68
+
69
+ def ANSIColour.colour=(*string)
70
+ # why does this seem to work?
71
+ @@colour = string[0]
72
+
73
+ # why won't his work?
74
+ # @@colour = string
75
+
76
+ return @@colour
77
+ end
78
+
79
+ def ANSIColour.attributes
80
+ { 'default' => '0',
81
+ 'clear' => '0',
82
+ 'reset' => '0',
83
+ 'bold' => '1',
84
+ 'dark' => '2',
85
+ 'underline' => '4',
86
+ 'blink' => '5',
87
+ 'reverse' => '7',
88
+ 'concealed' => '8',
89
+
90
+ 'black' => '30', 'on_black' => '40',
91
+ 'red' => '31', 'on_red' => '41',
92
+ 'green' => '32', 'on_green' => '42',
93
+ 'yellow' => '33', 'on_yellow' => '43',
94
+ 'blue' => '34', 'on_blue' => '44',
95
+ 'magenta' => '35', 'on_magenta' => '45',
96
+ 'cyan' => '36', 'on_cyan' => '46',
97
+ 'white' => '37', 'on_white' => '47'
98
+ }
99
+ end
100
+ end
101
+ end
102
+
103
+ class String
104
+ def colour(newcolour, *colours)
105
+ attr = Term::ANSIColour.attributes
106
+ [newcolour, colours].flatten.map {|c| "\e\[#{attr[c]}m"}.join + self.to_s + "\e\[#{attr['reset']}m"
107
+ end
108
+
109
+ alias color colour
110
+ end
111
+
112
+ module Kernel
113
+ include Term::ANSIColour
114
+
115
+ alias_method :orig_print, :print
116
+ #alias_method :orig_puts, :puts # XXX - still has issues passing a list
117
+
118
+ def print(str)
119
+ colours = Term::ANSIColour.colour
120
+ attr = Term::ANSIColour.attributes
121
+ orig_print ( attr[colours[0]] == 0 ? str :
122
+ colours.map{|c| "\e\[#{attr[c]}m"}.join + str + "\e\[#{attr['reset']}m"
123
+ )
124
+ end
125
+ # XXX - todo - get puts, printf and putc to ouput in colour.
126
+ end
@@ -0,0 +1,14 @@
1
+ # dir.rb
2
+ # ara.t.howard@noaa.gov
3
+
4
+ class Dir
5
+ def self.ls dir, glob = File.join('**','**'), &block
6
+ ret = [] unless block
7
+ Dir.glob(File.join(dir, glob)) do |entry|
8
+ block ? block.call(entry) : ret.push(entry)
9
+ end
10
+ ret
11
+ end
12
+ end
13
+
14
+ raise RuntimeError, 'This library is for require only' if $0 == __FILE__
@@ -0,0 +1,97 @@
1
+ require 'yaml'
2
+ require 'set'
3
+
4
+ class Class
5
+ def persist
6
+ @persist = [] if !@persist
7
+ @persist
8
+ end
9
+
10
+ def persist= p
11
+ @persist = p if p.kind_of?(Array)
12
+ end
13
+
14
+ def persist_with_parent
15
+ p = []
16
+ klass = self;
17
+ while klass
18
+ p.concat(klass.persist)
19
+ klass = klass.superclass
20
+ end
21
+ p.uniq
22
+ end
23
+ end
24
+
25
+ class Object
26
+ def self.persistent *var
27
+ for i in (0..var.length-1)
28
+ var[i] = var[i].to_s
29
+ end
30
+ self.persist.concat(var)
31
+ self.persist.uniq!
32
+ end
33
+
34
+ alias_method :old_to_yaml, :to_yaml
35
+
36
+ def to_yaml ( opts = {} )
37
+ p = self.class.persist_with_parent
38
+
39
+ if p && p.size > 0
40
+ yaml_emit opts do |map|
41
+ p.each do |m|
42
+ map.add( m, instance_variable_get( '@' + m ) )
43
+ end
44
+ end
45
+ else
46
+ old_to_yaml opts
47
+ end
48
+ end
49
+ private
50
+ def yaml_emit opts
51
+ YAML::quick_emit( object_id, opts ) do |out|
52
+ out.map( taguri, to_yaml_style ) do |map|
53
+ yield map
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ module RHNH
60
+ module EnumerablePostDeserializeHelper
61
+ def post_deserialize
62
+ self.each do |e|
63
+ YAML.call_post_deserialize(e) if e
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ class Array
70
+ include RHNH::EnumerablePostDeserializeHelper
71
+ end
72
+
73
+ class Hash
74
+ include RHNH::EnumerablePostDeserializeHelper
75
+ end
76
+
77
+ module YAML
78
+ def YAML.call_post_deserialize obj, object_map = ::Set.new
79
+ if !object_map.include?(obj.object_id)
80
+ object_map.add(obj.object_id)
81
+
82
+ obj.instance_variables.each do |v|
83
+ call_post_deserialize obj.instance_variable_get(v), object_map
84
+ end
85
+
86
+ obj.post_deserialize if obj.respond_to?('post_deserialize')
87
+ end
88
+ end
89
+
90
+ def YAML.load( io )
91
+ yp = parser.load( io )
92
+ call_post_deserialize yp
93
+ yp
94
+ end
95
+ end
96
+
97
+ raise RuntimeError, 'This library is for require only' if $0 == __FILE__
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # http.rb
4
+ # yesmar@speakeasy.net
5
+
6
+ require 'net/http'
7
+
8
+ module RCL
9
+ module HTTP
10
+ def self.fetch_uri(uri, max_redirects=10)
11
+ raise ArgumentError, 'nil uri' if uri.nil?
12
+ raise ArgumentError, 'invalid uri class' if uri.class != String
13
+ raise ArgumentError, 'empty uri' if uri.empty?
14
+ raise ArgumentError, 'nil max_redirects' if max_redirects.nil?
15
+ raise ArgumentError, 'max_redirects too deep' if max_redirects == 0
16
+
17
+ url = URI.parse(uri)
18
+ count = -1
19
+ found = false
20
+
21
+ until found
22
+ host, port = url.host, url.port if url.host && url.port
23
+ req = Net::HTTP::Get.new(url.path)
24
+ res = Net::HTTP.start(host, port) { |http| http.request(req) }
25
+ res.header['location'] \
26
+ ? url = URI.parse(res.header['location']) : found = true
27
+ count += 1
28
+ raise 'too many redirects' if count >= max_redirects
29
+ end
30
+
31
+ return res, count
32
+ end
33
+
34
+ def self.status_message(res, count=0)
35
+ raise ArgumentError, 'nil res' if res.nil?
36
+
37
+ status = []
38
+
39
+ status << "HTTP/#{res.http_version} #{res.code} #{res.message}"
40
+ status << " [#{count} redirec#{count!= 1 ? "ts" : "t"}]" if count > 0
41
+
42
+ status.join
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # smtp.rb
4
+ # yesmar@speakeasy.net
5
+
6
+ module RCL
7
+ module SMTP
8
+ def self.send(mta, from, from_alias, to, to_alias, subject, message)
9
+ raise ArgumentError, 'nil mta' if mta.nil?
10
+ raise ArgumentError, 'invalid mta class' if mta.class != String
11
+ raise ArgumentError, 'empty mta' if mta.empty?
12
+ raise ArgumentError, 'nil from' if from.nil?
13
+ raise ArgumentError, 'invalid from class' if from.class != String
14
+ raise ArgumentError, 'empty from' if from.empty?
15
+ raise ArgumentError, 'nil from_alias' if from_alias.nil?
16
+ raise ArgumentError, 'invalid from_alias class' if from_alias.class \
17
+ != String
18
+ raise ArgumentError, 'empty from' if from_alias.empty?
19
+ raise ArgumentError, 'nil to' if to.nil?
20
+ raise ArgumentError, 'invalid to class' if to.class != String
21
+ raise ArgumentError, 'empty from' if to.empty?
22
+ raise ArgumentError, 'nil to_alias' if to_alias.nil?
23
+ raise ArgumentError, 'invalid to_alias class' if to_alias.class != \
24
+ String
25
+ raise ArgumentError, 'empty from' if to_alias.empty?
26
+ raise ArgumentError, 'nil subject' if subject.nil?
27
+ raise ArgumentError, 'invalid subject class' if subject.class != String
28
+ raise ArgumentError, 'nil message' if message.nil?
29
+ raise ArgumentError, 'invalid message class' if message.class != String
30
+
31
+ msg = <<EOM
32
+ From: #{from} <#{from_alias}>
33
+ To: #{to_alias} <#{to}>
34
+ Subject: #{subject}
35
+
36
+ #{message}
37
+ EOM
38
+
39
+ Net::SMTP.start(mta) do |smtp|
40
+ smtp.send_message(msg, from, to)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -87,6 +87,41 @@ module RCL
87
87
  return str.join
88
88
 
89
89
  end
90
+
91
+ def self.datestamp(ts)
92
+ raise ArgumentError, 'nil ts' if ts.nil?
93
+ raise ArgumentError, 'invalid ts class' if ts.class != Time
94
+
95
+ str = []
96
+
97
+ str << ts.year
98
+ str << '0' if ts.month < 10
99
+ str << ts.month
100
+ str << '0' if ts.day < 10
101
+ str << ts.day
102
+
103
+ str.join
104
+ end
105
+
106
+ def self.timestamp(ts)
107
+ raise ArgumentError, 'nil ts' if ts.nil?
108
+ raise ArgumentError, 'invalid ts class' if ts.class != Time
109
+
110
+ str = []
111
+
112
+ str << '0' if ts.hour < 10
113
+ str << ts.hour
114
+ str << '0' if ts.min < 10
115
+ str << ts.min
116
+ str << '0' if ts.sec < 10
117
+ str << ts.sec
118
+
119
+ str.join
120
+ end
121
+
122
+ def self.datetimestamp(ts)
123
+ "#{datestamp(ts)}T#{timestamp(ts)}"
124
+ end
90
125
  end
91
126
 
92
127
  raise RuntimeError, 'This library is for require only' if $0 == __FILE__
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramsey Dow
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-27 00:00:00 -08:00
12
+ date: 2008-02-28 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,12 +24,17 @@ extra_rdoc_files:
24
24
  files:
25
25
  - lib/rcl.rb
26
26
  - lib/rcl/base.rb
27
+ - lib/rcl/http.rb
27
28
  - lib/rcl/perms.rb
28
29
  - lib/rcl/shell.rb
30
+ - lib/rcl/smtp.rb
29
31
  - lib/rcl/timestamp.rb
30
32
  - lib/rcl/token.rb
31
33
  - lib/rcl/url.rb
32
34
  - lib/rcl/xml.rb
35
+ - lib/rcl/ext/ansicolour.rb
36
+ - lib/rcl/ext/dir.rb
37
+ - lib/rcl/ext/yaml_helper.rb
33
38
  - README
34
39
  has_rdoc: true
35
40
  homepage: http://rcl.rubyforge.org/