Class: MyRepresentatives::WA::WebShow

Inherits:
Object
  • Object
show all
Includes:
Guessable
Defined in:
lib/my_representatives/wa/web_show.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Guessable

#guess_first, #guess_gender, #guess_last, #guess_preferred, #guess_title

Constructor Details

#initialize(url) ⇒ WebShow

Returns a new instance of WebShow.



8
9
10
11
12
13
14
# File 'lib/my_representatives/wa/web_show.rb', line 8

def initialize(url)
  @logger = Logger.new(STDOUT)
  raise MyRepresentatives::InvalidURLError unless url && url.is_a?(String)
  @url = url
  fetch_document
  raise MyRepresentatives::NokogiriDocumentPropertiesError unless test_document_for_validity
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



6
7
8
# File 'lib/my_representatives/wa/web_show.rb', line 6

def document
  @document
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/my_representatives/wa/web_show.rb', line 6

def logger
  @logger
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/my_representatives/wa/web_show.rb', line 6

def url
  @url
end

Instance Method Details

#electorate_nameObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/my_representatives/wa/web_show.rb', line 60

def electorate_name
  begin
    regex = /Electorate:(.+?)Inaugural/
    alt_regex = /Electorate:(.+?)Speeches/

    text = @document.css("font[size='2']").text.strip
    match = text.match(regex)
    alt_match = text.match(alt_regex)

    if match
      match[1]
    elsif alt_match
      alt_match[1]
    else
      nil
    end
  rescue NoMethodError
    nil
  end
end

#emailObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/my_representatives/wa/web_show.rb', line 102

def email
  begin
    regex = /Email:\s(.+?wa.gov.au)/

    text = @document.css("font[size='2']").text.strip
    match = text.match(regex)

    if match
      match[1]
    else
      nil
    end
  rescue NoMethodError
    nil
  end
end

#first_nameObject



36
37
38
39
40
41
42
# File 'lib/my_representatives/wa/web_show.rb', line 36

def first_name
  begin
    guess_first(formal_name)
  rescue NoMethodError
    nil
  end
end

#formal_nameObject



20
21
22
23
24
25
26
# File 'lib/my_representatives/wa/web_show.rb', line 20

def formal_name
  begin
    @document.css("div#content").css("font")[0].text
  rescue NoMethodError
    nil
  end
end

#homepage_urlObject



16
17
18
# File 'lib/my_representatives/wa/web_show.rb', line 16

def homepage_url
  @url
end

#image_urlObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/my_representatives/wa/web_show.rb', line 119

def image_url
  begin
    uri = nil
    @document.css("div#content").css("img").each do |img|
      link = img.attr("src")
      uri = link if link.include?('(MemberPics)')
      break if link.include?('(MemberPics)')
    end

    if uri
      "http://www.parliament.wa.gov.au/Parliament/Memblist.nsf/#{uri.gsub("../","")}"
    else
      nil
    end

  rescue
    nil
  end
end

#last_nameObject



44
45
46
47
48
49
50
# File 'lib/my_representatives/wa/web_show.rb', line 44

def last_name
  begin
    guess_last(formal_name)
  rescue NoMethodError
    nil
  end
end

#party_nameObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/my_representatives/wa/web_show.rb', line 81

def party_name
  begin
    regex = /Party:(.+?)Electorate/
    alt_regex = /Party:(.+?)Inaugural/

    text = @document.css("font[size='2']").text.strip
    match = text.match(regex)
    alt_match = text.match(alt_regex)

    if match
      match[1]
    elsif alt_match
      alt_match[1]
    else
      nil
    end
  rescue NoMethodError
    nil
  end
end

#phoneObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/my_representatives/wa/web_show.rb', line 139

def phone
  begin
    regex1 = /Ph:\s([0-9]{4} [0-9]{4})Email/
    regex2 = /Ph:\s(08 [0-9]{4} [0-9]{4})Email/
    regex3 = /Ph:\s([0-9]{4} [0-9]{3} [0-9]{3})Email/
    regex4 = /Ph:\s(.+?)$/
    regex5 = /Ph:\s(.+?)Email/m

    text = @document.css("font[size='2']").text.strip
    match1 = text.match(regex1)
    match2 = text.match(regex2)
    match3 = text.match(regex3)
    match4 = text.match(regex4)
    match5 = text.match(regex5)

    if match1
      match = match1[1].gsub(/\D/, '')
      match.start_with?("08") ? match : "08"+match
    elsif match2
      match = match2[1].gsub(/\D/, '')
      match.start_with?("08") ? match : "08"+match
    elsif match3
      match = match1[3].gsub(/\D/, '')
      match3
    elsif match4
      match = match4[1].gsub(/\D/, '')
      match.start_with?("08") ? match : "08"+match
    elsif match5
      match = match5[1].gsub(/\D/, '')
      match.start_with?("08") ? match : "08"+match
    else
      nil
    end
  rescue NoMethodError
    nil
  end
end

#preferred_nameObject



52
53
54
55
56
57
58
# File 'lib/my_representatives/wa/web_show.rb', line 52

def preferred_name
  begin
    guess_preferred(formal_name)
  rescue NoMethodError
    nil
  end
end

#titleObject



28
29
30
31
32
33
34
# File 'lib/my_representatives/wa/web_show.rb', line 28

def title
  begin
    guess_title(formal_name)
  rescue NoMethodError
    nil
  end
end