Class: ParseMember

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

Constant Summary collapse

MEMBER_QUERY_URL =
'http://www.rusa.org/cgi-bin/membersearch_PF.pl'
PARMS =

match parm key with parameter we will pass

{'clubs' => 'club',
'ids' => 'mid',
'lnames' => 'sname'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParseMember

Returns a new instance of ParseMember.



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
# File 'lib/parse_member.rb', line 35

def initialize
  @members = Members.new
  @url = MEMBER_QUERY_URL
  @parse_result = ParseResult.new

  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  @options = {}
  @options['status'] = 'current'
  @options['year'] = [Time.now.year]

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: parse_member.rb [options]"

    # include these clubs
    opts.on("--club seattle,oregon,boston", Array, "include the following clubs") do |list|
      @options['clubs'] = list
    end
    # include membership ids
    opts.on("--id 137,2245,2157", Array, "include the following membership ids") do |list|
      @options['ids'] = list
    end

    # include these last names
    opts.on("--lname smith,jones,brown", Array, "Include members with the following last names") do |list|
      @options['lnames'] = list
    end

    # get results for these years
    opts.on("--year ", Array, "get results for the following years. Defaults to current year") do |list|
      @options['year'] = list
    end

    # Boolean switch. default is current members (no expired memberships)
    opts.on("-a",  "Current and past members") do |a|
      @options['status'] = a ? 'all' : 'current'
    end

    # No argument, shows at tail.  This will print an options summary.
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  opts.parse!(ARGV)

end

Instance Attribute Details

#membersObject

Returns the value of attribute members.



19
20
21
# File 'lib/parse_member.rb', line 19

def members
  @members
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/parse_member.rb', line 19

def options
  @options
end

#parse_resultObject

Returns the value of attribute parse_result.



19
20
21
# File 'lib/parse_member.rb', line 19

def parse_result
  @parse_result
end

#urlObject

Returns the value of attribute url.



19
20
21
# File 'lib/parse_member.rb', line 19

def url
  @url
end

Class Method Details

.runObject



28
29
30
31
32
33
# File 'lib/parse_member.rb', line 28

def self.run
  pm = ParseMember.new
  pm.run
  pm.report

end

Instance Method Details

#fetch(url, parms = nil) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/parse_member.rb', line 104

def fetch url, parms=nil
  if block_given? then
    f = yield url
  else
    f = Net::HTTP.post_form(URI.parse(url), parms).body
  end
  Nokogiri::HTML(f)
end

#parse(doc) ⇒ Object



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
# File 'lib/parse_member.rb', line 113

def parse doc
  ##
  # the data we care about starts in <td> that lists count of matches found
  doc.css('td').each do |link|
    if ( link.text =~ /(\d+) Matches Found\n/im ) then
      # member element is in a <p>, instantiate Person with relevant data
      link.css('p').each do |member|
        member.text =~ /(.*), (.*) \| RUSA No\. (\d+) \|.*\n(.*) \|.*(\d{4}\/\d{2}\/\d{2})/im
        
        member = Person.new({
          :last_name => $1.strip,
          :first_name => $2.strip,
          :id => $3,
          :club => $4.strip,
          :member_expiration_date => Time.parse($5)
        } )

        # don't run if block given (such as during test)
        unless block_given? then
          @options['year'].each do |year|
            @parse_result.results member, year
          end
        end
        
        @members<< member
      end
    end
  end
end

#reportObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/parse_member.rb', line 143

def report
  @members.sort{|a,b| a[1].last_name + a[1].first_name <=> b[1].last_name + b[1].first_name}.each do |id, member|
    puts "%25s\t%7s\t%12s\t%30s\n" % ["#{member.first_name} #{member.last_name}",
                                      member.id,
                                      member.member_expiration_date,
                                      member.club]
    member.results.sort{|a,b| a.date <=> b.date}.each do |result|
      puts "   Result: %12s\t%10s\t%10s\t%8s\n" % [result.date, result.distance,
                                                      result.certificate, result.type]
     end

    member.awards.each do |year, awards|
      awards.each do |award|
        puts "   Award: %8s\t%10s\n" % [award.year, award.type]
      end
     end
  end
end

#run(&block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/parse_member.rb', line 84

def run &block
  if block_given? then
    doc = fetch(@url, {}, &block)
    parse(doc, &block)

  else
    # get requested parms  and fetch url with them
    parms = {'status' => @options['status']}
    PARMS.each do |key, parm|
      next if @options[key].nil?
      @options[key].each do |value|
        parms[parm] = value
        doc = fetch(@url, parms, &block)
        parse(doc)
      end
      parms.delete parm
    end
  end
end