Class: LegislationUK::Legislation

Inherits:
Object
  • Object
show all
Includes:
LegislationUriHelper, Morph
Defined in:
lib/legislation_uk.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LegislationUriHelper

#legislation_uri

Class Method Details

.open_uri(uri) ⇒ Object



44
45
46
# File 'lib/legislation_uk.rb', line 44

def self.open_uri uri
  open(uri).read
end

Instance Method Details

#opsi_uriObject



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

def opsi_uri
  unless @opsi_uri
    search_url = "http://search.opsi.gov.uk/search?q=#{URI.escape(title)}&output=xml_no_dtd&client=opsisearch_semaphore&site=opsi_collection"
    begin
      doc = Hpricot.XML Legislation.open_uri(search_url)
      url = nil

      (doc/'R/T').each do |result|
        unless url
          term = result.inner_text.gsub(/<[^>]+>/,'').strip
          url = result.at('../U/text()').to_s if(title == term || term.starts_with?(title))
        end
      end

      @opsi_uri = url
    rescue Exception => e
      puts 'error retrieving: ' + search_url
      puts e.class.name
      puts e.to_s
    end
  end
  @opsi_uri
end

#opsi_uri_for_section(section_number) ⇒ Object



108
109
110
111
112
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
# File 'lib/legislation_uk.rb', line 108

def opsi_uri_for_section section_number
  if opsi_uri && !@opsi_sections
    section_number = section_number.to_s
    doc = Hpricot Legislation.open_uri(opsi_uri)

    (doc/'span[@class="LegDS LegContentsNo"]').each do |span|
      number_of_section = span.inner_text.chomp('.')
      if span.at('a')
        path = span.at('a')['href']
        base = opsi_uri[/^(.+\/)[^\/]+$/,1]
        section_title = span.next_sibling.inner_text

        @opsi_sections ||= {}
        @opsi_sections[number_of_section] = { :title => section_title, :opsi_uri => "#{base}#{path}"}
      else
        puts "cannot find opsi url for section #{number_of_section} of #{name}"
      end
    end
    @opsi_sections[section_number][:opsi_uri]
  elsif @opsi_sections
    if @opsi_sections[section_number]
      @opsi_sections[section_number][:opsi_uri]
    else
      puts "no opsi url for #{section_number}\n" + @opsi_sections.inspect + "\n\nno opsi url for #{section_number}\n"
      nil
    end
  else
    nil
  end
end

#partsObject



65
66
67
68
69
70
71
# File 'lib/legislation_uk.rb', line 65

def parts
  if respond_to?(:contents) && contents
    contents.parts
  else
    []
  end
end

#populateObject



52
53
54
55
# File 'lib/legislation_uk.rb', line 52

def populate
  parts.each {|x| x.legislation= self}
  sections.each {|x| x.legislation= self}
end

#sectionsObject



57
58
59
60
61
62
63
# File 'lib/legislation_uk.rb', line 57

def sections
  if parts
    parts.collect(&:sections).flatten
  else
    []
  end
end

#statutelaw_uriObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/legislation_uk.rb', line 73

def statutelaw_uri
  if legislation_uri[%r|http://www.legislation.gov.uk/(.+)/(\d\d\d\d)/(\d+)|]
    type = $1
    year = $2
    chapter = $3
    "http://www.statutelaw.gov.uk/documents/#{year}/#{chapter}/#{type}/c#{chapter}"
  else
    nil
  end
end

#titleObject



48
49
50
# File 'lib/legislation_uk.rb', line 48

def title
  .title
end