Class: SuperStamper::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/super_stamper.rb

Overview

a class

Constant Summary collapse

BEGIN_HEADER =
"# -- begin header --"
END_HEADER =
"# -- end header --"
DEFAULT_HEADER_FILE_NAME =
"header.txt"
NEWLINE =
"\n"
QUOTEFEED =
"http://feeds.sydv.net/top200-bash-quotes"

Class Method Summary collapse

Class Method Details

.stamp_recursively(options = {}) ⇒ Object



59
60
61
62
63
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/super_stamper.rb', line 59

def self.stamp_recursively(options = {})
  #puts options.inspect
  extension = options[:extension] || "rb"

  count = 0

  ##########################################################
  # Optional: add quote from IRC.
  # First make an inventory of all the quotes.
  ##########################################################
  quote_database = nil
  # Add quote from IRC
  if defined?(SimpleRSS) && defined?(Nokogiri) && options[:quote]
    puts "super_stamper: You ordered some quotes. Let me see if I can arrange that for you ..."
    # +_+ #
    quotefeed = options[:quotefeed] || QUOTEFEED
    # +_+ #
    begin
      puts "super_stamper: Getting quotes from #{quotefeed} ..."
      # +_+ #
      rss = SimpleRSS.parse open(quotefeed)
      # get all descriptions.
      # needs double escaping.
      quote_database = rss.entries.collect { |e| "##{CGI.unescapeHTML(CGI.unescapeHTML(e[:description])).gsub(/\<br\s?\/\>/, "\n#").gsub("&nbsp;", "")}" }
      puts "super_stamper: Successfully gathed quote database. Putting them into your source files like a smooth operator ..."
    rescue => e
      puts "super_stamper: Sorry, couldn't add quotes to your files."
      puts "super_stamper: Error was: #{e}"
      puts "super_stamper: Proceeding with normal operation."
    end
  end
  ##########################################################

  Dir["**/*.#{extension}"].each do |f|
    ########################################################
    le_stamp_options = {:filename => f, :header_file_name => options[:header_file_name]}
    ########################################################
    # Quote?
    ########################################################
    le_stamp_options[:quote] = quote_database.sample unless quote_database.nil?
    ########################################################
    stamp_single le_stamp_options
    ########################################################
    count += 1
    ########################################################
  end

  # Informative!
  puts "super_stamper: I inserted a header into #{count} files with extension .#{extension} (recursively)."

  count
end

.stamp_single(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/super_stamper.rb', line 112

def self.stamp_single(options = {})
  ret = nil

  file = nil
  header = nil

  filename = options[:filename]
  header_file_name = options[:header_file_name] || "header.txt"

  raise "I need a filename" if filename.nil?
  raise "Please provide a valid filename (-f) or create a header.txt in your working directory." unless File.exists?(header_file_name)

  file = File.new(filename)
  header = File.new(header_file_name).read

  unless file.nil? || header.nil?
    str = file.read
    # Let's remove the magic_encoding at the start, if any.
    str.gsub!(/^# -\*-.+-\*-(\r)?#{NEWLINE}/, '')
    # quote is optional.
    quote = options[:quote] || ""
    quotestring = ""
    quotestring = "#{NEWLINE}#{quote}#{NEWLINE}#{NEWLINE}" unless quote.eql?("")
    # and now .. do this.
    contents_array = [BEGIN_HEADER, NEWLINE, header, quotestring, END_HEADER, NEWLINE]
    # behavior of to_s changed from Ruby 1.8.7 to 1.9.3,
    # used to be [ "foo", "bar" ].to_s # => "foobar" <--- 1.8.7
    # now it's => "[\"foo\", \"bar\"]" # 1.9.3
    #contents_str = contents_array.to_s
    contents_str = contents_array.join
    # Remove the header that was already in place, if any.
    str.gsub!(/#{BEGIN_HEADER}.*#{END_HEADER}#{NEWLINE}/m, '')
    # concat it
    #contents = contents_str + str
    contents = "#{contents_str}#{str}"
    # Re-open file and write to it
    file.close
    file = File.new(filename, 'w')
    # write
    file.write(contents)
    # close again
    file.close
    # Return it
    ret = file
  end

  ret
end