-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathana-class.rb
More file actions
100 lines (76 loc) · 2.7 KB
/
Copy pathana-class.rb
File metadata and controls
100 lines (76 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
require 'socket'
class RubyGram
#initilizer method... initialized with anagram
def initialize(anagram)
# Server setup stuff, setup as instance variables so they are global to that particular
# object upon creation
@anagram = anagram
@host = 'wordsmith.org'
@port = 80
@path = "/anagram/anagram.cgi?anagram=#{anagram}"
end
def getAnagram(anagram)
#formated GET request to the webserver.. it expects this stuff
request = "GET #{@path} HTTP/1.0\r\n\r\n"
#opens connection to host on the port
s = TCPSocket.open(@host, @port)
#sends GET stuff to the server
s.print(request)
#reads what the GET returns (what data it's GETing)
response = s.read
#split divides the result where the pattrn matches
# returns 2 fields in the array.. \r\n is CRLF line termination
headers,body = response.split("\r\n\r\n", 2)
#close socket
s.close
#return body back to caller
return body
end
def parseAnagram(anagramPage)
pat = /^<b>(\d+) found\. Displaying all:\s*<\/b><br>|\G(?!\A)\s*([\w ]+)<br>/
#takes the arguement and scans through it capturing the patterns we find
#count goes to the first capturing group if it exists
# anagram represents all other capturing groups in this looping/iteration
anagramPage.scan(pat) { |count,anagram|
if count
puts
puts "Results: #{count}"
puts
else
puts anagram
end
}
puts
end
def getAndParse()
#Go and fetch the anagram and store it
myAnagram = getAnagram(@anagram)
#Parse and print anagram
parseAnagram(myAnagram)
end
def mainMenu()
puts
puts "----------------------------------"
puts "\tWelcome to RubyGram!"
puts "----------------------------------"
puts
puts "Enter one of the menu choices"
puts "\t1. Find Anagrams"
puts "\t2. Exit"
print "Please enter your selection: "
choice = gets.chomp
case choice
when "1"
#get anagram, parse it, and print it
getAndParse()
when "2"
puts "\nExiting!\n"
exit
else
puts "\nInvalid Selection!\n"
# using some recursion that i don't fully grasp at this state, we call the menu structure again
# because on the stack and in mmemory it's already created.
mainMenu()
end
end
end