Last active
November 8, 2016 23:25
-
-
Save h-lame/1f032a1f8181fe220d6f1c2c4d98f64e to your computer and use it in GitHub Desktop.
My First Ruby - code snippets from slides - see: http://h-lame.com/talks/my-first-ruby/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ -24,8 +24,8 @@ class Config < Hash | |
} | |
end | |
- def initialize(name) | |
- self[Config::Name] = name | |
+ def initialize(name=nil) | |
+ self[Config::Name] = name if name != nil | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Config(String name) { | |
if (name != null) { | |
this.put(Config.NAME, name) | |
} | |
} | |
public Config() { | |
super(null); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initialize(name=nil) | |
self[Config::Name] = name if name != nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def handleMessage(message) | |
sender = @users.findByEmail(message.header.from[0]) | |
if (sender == nil) | |
return | |
end | |
message.header.delete('From') | |
message.header['From'] = sender.preferredFromAddress | |
processSubject(message) # modify the subject | |
addListHeaders(message) # add the list headers | |
archive(message) # archive the mail | |
processAttachments(message) # modify attachments | |
sendToMembers(message) # send it out. | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def processSubject(message) | |
sub = message.header.subject.dup | |
prefix = "[" + @config.listHeaders["List"] + "]" | |
i = sub.index(prefix) | |
sub.slice!(i..(i+prefix.length)) if (i != nil) | |
found = 0 | |
re_str = "re: " | |
i = sub.downcase().index(re_str) | |
while (i != nil) | |
sub.slice!(i,re_str.length) | |
i = sub.downcase().index(re_str) | |
found = 1 | |
end | |
prefix = "Re: "+prefix if (found == 1) | |
message.header.subject = prefix + " " + sub | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def processSubject(message) | |
sub = message.header.subject.dup | |
prefix = "[" + @config.listHeaders["List"] + "]" | |
sub.gsub!(/#{Regexp.escape(prefix)}/,'') | |
if sub =~ /re\:\ /i | |
sub.gsub!(/re\:\ /i, '') | |
prefix = "Re: "+prefix | |
end | |
message.header.subject = prefix + " " + sub | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is a loosely extended Hash with some little | |
# things to make it's use more convenient | |
class ListConfiguration < Hash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this method is a bit overkill, but by caching | |
# the actual list header object instead of having | |
# to get it from the main config hash each time | |
# we should save some overhead. the ListHeaders | |
# part is the most frequently accessed, because | |
# it contains a lot of varied list info. | |
def listHeaders | |
if @listHeaders.id != self[ListHeaders].id | |
puts "recaching" | |
@listHeaders = self[ListHeaders] | |
end | |
@listHeaders | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@config[ListHeaders] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@config[ListHeaders] | |
def listHeaders | |
self[ListHeaders] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def listHeaders | |
if @listHeaders.id != self[ListHeaders].id | |
puts "recaching" | |
@listHeaders = self[ListHeaders] | |
end | |
@listHeaders | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def listHeaders | |
if @listHeaders.id != self[ListHeaders].id | |
puts "recaching" | |
@listHeaders = self[ListHeaders] | |
end | |
@listHeaders | |
end | |
# these just resolve to unique strings, | |
# used as keys within the config hash | |
Name = "name" | |
ListHeaders = "listheaders" | |
# this is the root directory | |
BaseDir = "basedir" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log("looking at \"#{part.header.media_type}\"") | |
if !TypesToKeep.include?(part.header.media_type) | |
md = name_regex.match(part.header["Content-Type"]) | |
filename = "null" | |
if md != nil | |
filename = | |
newUniqueFile(AttachmentDir, md[3]) { |f| | |
f.write(part.decode()) | |
} | |
saveAttachmentMetaData(filename, message) | |
else | |
log("error getting filename from \"#{part.header["Content-Type"]}\"") | |
end | |
size = File.size(File.join( | |
base_dir, @config[AttachmentDir], filename | |
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/home/room206/bin/narf | |
fucknut_home = '/home/room206/fucknut' | |
$: << fucknut_home # add our library path to the search path | |
require 'web' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddHandler cgi-script .rb | |
RewriteEngine on | |
RewriteRule ^(.*)/archives/(.*)$ /handler.rb?↩ listname=$1&mode=archive&$2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listname = Web['listname'] | |
if listname != nil and listname != "" | |
# main processing when we have a listname param | |
else | |
lists = Config.possible | |
lists = lists.collect { |i| | |
{'name' => i, 'value' => i} | |
} | |
if lists != [] | |
Web.print_template("what_list.html", {'listname' => listname, 'listnamevalues' => lists}) | |
else | |
Web.print_template("generalerror.html", {'error' => "No lists defined, get a better admin"}) | |
end | |
end | |
Web.flush |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Login to {$listname}</title> | |
<link href="/arse.css" rel="stylesheet" type="text/css" /> | |
</head> | |
<body> | |
<h1>Error during login to {$listname}</h1> | |
<p>{$error}</p> | |
<narf:form formname="tryagain" method="POST"> | |
<narf:hidden name="listname"/> | |
<input type="submit" value="Try Again"/> | |
</narf:form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginHandler | |
LoginTemplate = "login.html" | |
LogoutTemplate = "login.html" | |
LoginCommand = "login" | |
LogoutCommand = "logout" | |
def showLogin() | |
@web.print_template( *loginTemplateArgs() ) | |
end | |
def loginTemplateArgs() | |
[LoginTemplate, {"listname" => @web['listname'], | |
"cmd" => LoginCommand}] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def showLogin() | |
@web.print_template(LoginTemplate, | |
loginTemplateParams() ) | |
end | |
def loginTemplateParams() | |
{"listname" => @web['listname'], | |
"cmd" => LoginCommand} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def showLogin() | |
@web.print_template(LoginTemplate, | |
{"listname" => @web['listname'], | |
"cmd" => LoginCommand} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
errors = "" | |
commalist = @web['addresses'].gsub(/\n/, ",") | |
list = RMail::Address.parse(commalist) | |
if list.empty? | |
errors += "<br><h2>no valid email addresses given in list</h2>" | |
end | |
user.addresses = list | |
sendTo = RMail::Address.parse(@web['sendto'])[0] | |
sendToString = @web['sendto'] | |
if nil == sendTo | |
errors += "<br><h2>Send To Address is not valid</h2>" | |
sendToString = "INVALID: " + sendToString | |
else | |
sendToString = sendTo.format.to_s | |
user.sendTo = sendTo | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if @config[StartYear] < lastyear.year | |
lastyear = "<a href=\"#{baseurl}year=#{lastyear.year}&month=#{lastyear.month}\"><<</a>" | |
elsif @config[StartYear] == lastyear.year | |
if @config[StartMonth] <= lastyear.month | |
lastyear = "<a href=\"#{baseurl}year=#{lastyear.year}&month=#{lastyear.month}\"><<</a>" | |
else | |
lastyear = "<<" | |
end | |
else | |
lastyear = "<<" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment