Class: Act

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/act.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_name(name) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/act.rb', line 51

def from_name name
  name = normalize_name(name) if name
  if act = find_by_name(name)
    act.save if act.opsi_url.blank?
    act
  elsif act = find_by_title(name)
    act.save if act.opsi_url.blank?
    act
  elsif legislation = get_legislation_from_name(name)
    act = find_by_legislation_url(legislation.legislation_uri)
    if act
      act
    else
      warn "creating from name: #{name}" unless RAILS_ENV == "test"
      create! :name => name
    end
  else
    warn "creating from name: #{name}" unless RAILS_ENV == "test"
    create! :name => name
  end
end

.get_legislation(title, number = nil) ⇒ Object


34
35
36
37
38
39
40
41
42
43
# File 'app/models/act.rb', line 34

def get_legislation title, number=nil
  return nil # while legislation.gov.uk is down
  if number
    puts "calling Legislation API with title and number - searching for #{title} / #{number}" unless RAILS_ENV == "test"
    Legislation::UK.find(title, number)
  else
    puts "calling Legislation API with title - searching for #{title}" unless RAILS_ENV == "test"
    Legislation::UK.find(title)
  end
end

.get_legislation_from_name(name) ⇒ Object


30
31
32
# File 'app/models/act.rb', line 30

def get_legislation_from_name name
  get_legislation(get_title(name), get_number(name))
end

.get_number(name) ⇒ Object


26
27
28
# File 'app/models/act.rb', line 26

def get_number name
  name[/\(c\.\s?(\d+)/, 1]
end

.get_title(name) ⇒ Object


18
19
20
21
22
23
24
# File 'app/models/act.rb', line 18

def get_title name
  if name[/^(.+)\s\(c\.\s?\d+.+$/]
    $1
  else
    name
  end
end

.normalize_name(name) ⇒ Object


45
46
47
48
49
# File 'app/models/act.rb', line 45

def normalize_name name
  name = name.squeeze(' ')
  name.sub!(/\(c\.(\d)/, '(c. \1')
  name
end

Instance Method Details

#convert_to_hamlObject


78
79
80
81
82
83
84
85
86
87
# File 'app/models/act.rb', line 78

def convert_to_haml
  haml = ActToHtmlParser.new.parse_xml_file path, :format => :haml, :body_only => true

  results_dir = RAILS_ROOT + '/app/views/results'
  Dir.mkdir results_dir unless File.exist?(results_dir)
  template = "#{results_dir}/#{path.gsub('/','_').gsub('.','_')}.haml"

  File.open(template,'w+') {|f| f.write(haml) }
  template
end

#create_act_part(part) ⇒ Object


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
160
# File 'app/models/act.rb', line 127

def create_act_part part
  unless part.respond_to?(:title)
    logger.warn "title not present on: #{part.inspect}"
  else
    begin
      logger.info "creating #{part.title}"
    rescue
      logger.warn "act part is nil" if part.nil?
      return
    end
  end
  act_part = act_parts.build :name => part.number,
      :title => part.title,
      :legislation_url => part.legislation_uri,
      :statutelaw_url => part.statutelaw_uri

  part.sections.each do |section|
    unless section.respond_to?(:title)
      logger.warn "title not present on: #{section.inspect}"
    else
      begin
        act_sections.build :number => section.section_number,
            :title => section.title,
            :act_part => act_part,
            :legislation_url => section.legislation_uri,
            :opsi_url => section.opsi_uri,
            :statutelaw_url => section.statutelaw_uri
      rescue
        logger.warn "act section is nil" if section.nil?
        return
      end
    end
  end
end

#create_act_section(section) ⇒ Object


162
163
164
165
166
167
168
169
170
171
# File 'app/models/act.rb', line 162

def create_act_section section
  if section
    opsi_uri = section.opsi_uri
    act_sections.build :number => section.section_number,
        :title => section.title,
        :legislation_url => section.legislation_uri,
        :opsi_url => opsi_uri,
        :statutelaw_url => section.statutelaw_uri
  end
end

#find_section_by_number(section_number) ⇒ Object


74
75
76
# File 'app/models/act.rb', line 74

def find_section_by_number section_number
  act_sections.find_by_section_number section_number
end

#get_legislationObject


107
108
109
110
111
112
113
# File 'app/models/act.rb', line 107

def get_legislation
  if @legislation
    @legislation
  else
    @legislation = Act.get_legislation title, number
  end
end

#normalize_nameObject


89
90
91
# File 'app/models/act.rb', line 89

def normalize_name
  self.name = Act.normalize_name(name) if name
end

#opsi_search_url(name) ⇒ Object


185
186
187
# File 'app/models/act.rb', line 185

def opsi_search_url name
  "http://search.opsi.gov.uk/search?q=#{URI.escape(name)}&output=xml_no_dtd&client=opsisearch_semaphore&site=opsi_collection"
end

#populate_act_sectionsObject


173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/act.rb', line 173

def populate_act_sections
  if act_sections.empty?
    if legislation = get_legislation
      if legislation.parts.empty?
        legislation.sections.each { |section| create_act_section section }
      else
        legislation.parts.each { |part| create_act_part part if part }
      end
    end
  end
end

#populate_act_sections_from_opsi_urlObject


223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/models/act.rb', line 223

def populate_act_sections_from_opsi_url
  if act_sections.size == 0 && opsi_url && legislation_url
    doc = Hpricot open(opsi_url)
    (doc/'span[@class="LegDS LegContentsNo"]').each do |span|
      section_number = span.inner_text.chomp('.')
      if span.at('a')
        path = span.at('a')['href']
        base = opsi_url[/^(.+\/)[^\/]+$/,1]
        section_title = span.next_sibling.inner_text

        act_sections.build :section_number => section_number, :title => section_title,
            :opsi_url => "#{base}#{path}",
            :legislation_url => "#{legislation_url}/section/#{section_number}"
      else
        warn "cannot find opsi url for section #{section_number} of #{name}"
      end
    end
  end
end

#populate_legislation_urlsObject


115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/act.rb', line 115

def populate_legislation_urls
  if legislation_url.blank?
    if legislation = get_legislation
      self.legislation_url = legislation.legislation_uri
      self.opsi_url = legislation.opsi_uri
      self.statutelaw_url = legislation.statutelaw_uri
    else
      populate_legislation_urls_via_opsi
    end
  end
end

#populate_legislation_urls_via_opsiObject


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/act.rb', line 200

def populate_legislation_urls_via_opsi
  if doc = search_opsi
    url = nil
    (doc/'R/T').each do |result|
      unless url
        term = result.inner_text.gsub(/<[^>]+>/,'').strip
        title_re = title.gsub('(','\(').gsub(')','\)')
        url = result.at('../U/text()').to_s if(name == term || term[/^#{title_re}/i] )
      end
    end

    if url
      self.opsi_url = url
      if opsi_url[/ukpga/]
        self.legislation_url = "http://www.legislation.gov.uk/ukpga/#{year}/#{number}"
      end
      populate_act_sections_from_opsi_url
    else
      warn "cannot find an opsi url for '#{name}' / '#{title}': #{opsi_search_url(name)}"
    end
  end
end

#populate_numberObject


99
100
101
# File 'app/models/act.rb', line 99

def populate_number
  self.number = Act.get_number(name) if number.blank?
end

#populate_titleObject


103
104
105
# File 'app/models/act.rb', line 103

def populate_title
  self.title = Act.get_title(name) if title.blank?
end

#populate_yearObject


93
94
95
96
97
# File 'app/models/act.rb', line 93

def populate_year
  if year.blank? && name[/Act\s(\d\d\d\d)/]
    self.year = $1
  end
end

#search_opsiObject


189
190
191
192
193
194
195
196
197
198
# File 'app/models/act.rb', line 189

def search_opsi
  begin
    doc = Hpricot.XML open(opsi_search_url(name))
  rescue Exception => e
    warn e.class.name
    warn 'error retrieving: ' + opsi_search_url(name)
    warn e.to_s
    warn e.backtrace.join("\n")
  end
end