legislation-uk 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/CHANGELOG +1 -0
  2. data/README +29 -9
  3. data/lib/legislation_uk.rb +126 -27
  4. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1,2 +1,3 @@
1
+ v0.0.3 now returns opsi uri and legislation uri for a section
1
2
  v0.0.2 now returns opsi url, statutelaw url and legislation url
2
3
  v0.0.1 initial alpha release
data/README CHANGED
@@ -28,18 +28,38 @@ Can be used from command line if you run irb:
28
28
 
29
29
  legislation = Legislation::UK.find('Channel Tunnel Rail Link Act 1996')
30
30
 
31
- legislation.title #=> "Channel Tunnel Rail Link Act 1996"
31
+ legislation.title
32
+ #=> "Channel Tunnel Rail Link Act 1996"
32
33
 
33
- legislation.legislation_url #=> "http://www.legislation.gov.uk/ukpga/1996/61"
34
+ legislation.legislation_uri
35
+ #=> "http://www.legislation.gov.uk/ukpga/1996/61"
34
36
 
35
- legislation.opsi_url #=> "http://www.opsi.gov.uk/acts/acts1996/ukpga_19960061_en_1"
37
+ legislation.opsi_uri
38
+ #=> "http://www.opsi.gov.uk/acts/acts1996/ukpga_19960061_en_1"
36
39
 
37
- legislation.statutelaw_url #=> "http://www.statutelaw.gov.uk/documents/1996/61/ukpga/c61"
40
+ legislation.statutelaw_uri
41
+ #=> "http://www.statutelaw.gov.uk/documents/1996/61/ukpga/c61"
38
42
 
39
- legislation.parts.size #=> 3
43
+ legislation.parts.size
44
+ #=> 3
40
45
 
41
- legislation.parts.collect(&:number) #=> ["Part I", "Part II", "Part III"]
46
+ legislation.parts.collect(&:number)
47
+ #=> ["Part I", "Part II", "Part III"]
42
48
 
43
- legislation.parts.collect(&:title) #=> ["The Channel Tunnel Rail Link",
44
- # "The A2 and M2 Improvement Works",
45
- # "Miscellaneous and General"]
49
+ legislation.parts.collect(&:title)
50
+ #=> ["The Channel Tunnel Rail Link", "The A2 and M2 Improvement Works",
51
+ # "Miscellaneous and General"]
52
+
53
+ section = legislation.sections.first
54
+
55
+ section.title
56
+ #=> "Construction and maintenance of scheduled works"
57
+
58
+ section.number
59
+ #=> "1"
60
+
61
+ section.legislation_uri
62
+ #=> "http://www.legislation.gov.uk/ukpga/1996/61/section/1"
63
+
64
+ section.opsi_uri
65
+ #=> "http://www.opsi.gov.uk/acts/acts1996/ukpga_19960061_en_2#pt1-pb1-l1g1"
@@ -4,31 +4,74 @@ require 'open-uri'
4
4
 
5
5
  module LegislationUK
6
6
 
7
- module Title
7
+ module TitleHelper
8
8
  def title
9
9
  contents_title.is_a?(String) ? contents_title.strip : contents_title.title.strip
10
10
  end
11
11
  end
12
12
 
13
- module ItemNumber
13
+ module ItemNumberHelper
14
14
  def number
15
15
  contents_number
16
16
  end
17
17
  end
18
18
 
19
+ module LegislationUriHelper
20
+ def legislation_uri
21
+ document_uri
22
+ end
23
+ end
24
+
25
+ module Helper
26
+ def return_values many
27
+ if respond_to?(many)
28
+ send(many) ? send(many) : []
29
+ else
30
+ one = many.to_s.singularize.to_sym
31
+ if respond_to?(one)
32
+ send(one) ? [send(one)] : []
33
+ else
34
+ []
35
+ end
36
+ end
37
+ end
38
+ end
39
+
19
40
  class Legislation
20
41
  include Morph
42
+ include LegislationUriHelper
21
43
 
22
44
  def self.open_uri uri
23
45
  open(uri).read
24
46
  end
25
47
 
26
- def legislation_url
27
- document_uri
48
+ def title
49
+ metadata.title
50
+ end
51
+
52
+ def populate
53
+ parts.each {|x| x.legislation= self}
54
+ sections.each {|x| x.legislation= self}
55
+ end
56
+
57
+ def sections
58
+ if parts
59
+ parts.collect(&:sections).flatten
60
+ else
61
+ []
62
+ end
63
+ end
64
+
65
+ def parts
66
+ if respond_to?(:contents) && contents
67
+ contents.parts
68
+ else
69
+ []
70
+ end
28
71
  end
29
72
 
30
- def statutelaw_url
31
- if legislation_url[%r|http://www.legislation.gov.uk/(.+)/(\d\d\d\d)/(\d+)|]
73
+ def statutelaw_uri
74
+ if legislation_uri[%r|http://www.legislation.gov.uk/(.+)/(\d\d\d\d)/(\d+)|]
32
75
  type = $1
33
76
  year = $2
34
77
  chapter = $3
@@ -38,16 +81,8 @@ module LegislationUK
38
81
  end
39
82
  end
40
83
 
41
- def title
42
- metadata.title
43
- end
44
-
45
- def parts
46
- contents ? contents.contents_parts : []
47
- end
48
-
49
- def opsi_url
50
- unless @opsi_url
84
+ def opsi_uri
85
+ unless @opsi_uri
51
86
  search_url = "http://search.opsi.gov.uk/search?q=#{URI.escape(title)}&output=xml_no_dtd&client=opsisearch_semaphore&site=opsi_collection"
52
87
  begin
53
88
  doc = Hpricot.XML Legislation.open_uri(search_url)
@@ -60,39 +95,101 @@ module LegislationUK
60
95
  end
61
96
  end
62
97
 
63
- @opsi_url = url
98
+ @opsi_uri = url
64
99
  rescue Exception => e
65
100
  puts 'error retrieving: ' + search_url
66
101
  puts e.class.name
67
102
  puts e.to_s
68
103
  end
69
104
  end
70
- @opsi_url
105
+ @opsi_uri
106
+ end
107
+
108
+ def opsi_uri_for_section section_number
109
+ if opsi_uri && !@opsi_sections
110
+ section_number = section_number.to_s
111
+ doc = Hpricot Legislation.open_uri(opsi_uri)
112
+
113
+ (doc/'span[@class="LegDS LegContentsNo"]').each do |span|
114
+ number_of_section = span.inner_text.chomp('.')
115
+ if span.at('a')
116
+ path = span.at('a')['href']
117
+ base = opsi_uri[/^(.+\/)[^\/]+$/,1]
118
+ section_title = span.next_sibling.inner_text
119
+
120
+ @opsi_sections ||= {}
121
+ @opsi_sections[number_of_section] = { :title => section_title, :opsi_uri => "#{base}#{path}"}
122
+ else
123
+ puts "cannot find opsi url for section #{number_of_section} of #{name}"
124
+ end
125
+ end
126
+ @opsi_sections[section_number][:opsi_uri]
127
+ elsif @opsi_sections
128
+ if @opsi_sections[section_number]
129
+ @opsi_sections[section_number][:opsi_uri]
130
+ else
131
+ puts "no opsi url for #{section_number}\n" + @opsi_sections.inspect + "\n\nno opsi url for #{section_number}\n"
132
+ nil
133
+ end
134
+ else
135
+ nil
136
+ end
137
+ end
138
+ end
139
+
140
+ class Contents
141
+ include Morph
142
+ include Helper
143
+
144
+ def parts
145
+ return_values :contents_parts
71
146
  end
72
147
  end
73
148
 
74
149
  class ContentsPart
75
150
  include Morph
76
- include Title
77
- include ItemNumber
151
+ include Helper
152
+ include TitleHelper
153
+ include ItemNumberHelper
154
+ include LegislationUriHelper
155
+
156
+ attr_accessor :legislation
157
+
78
158
  def blocks
79
- contents_pblocks ? contents_pblocks : []
159
+ return_values :contents_pblocks
160
+ end
161
+
162
+ def sections
163
+ if blocks.empty?
164
+ return_values :contents_items
165
+ else
166
+ blocks.collect(&:sections).flatten
167
+ end
80
168
  end
81
169
  end
82
170
 
83
171
  class ContentsPblock
84
172
  include Morph
85
- include Title
173
+ include Helper
174
+ include TitleHelper
175
+ include LegislationUriHelper
86
176
 
87
177
  def sections
88
- contents_items
178
+ return_values :contents_items
89
179
  end
90
180
  end
91
181
 
92
182
  class ContentsItem
93
183
  include Morph
94
- include Title
95
- include ItemNumber
184
+ include TitleHelper
185
+ include ItemNumberHelper
186
+ include LegislationUriHelper
187
+
188
+ attr_accessor :legislation
189
+
190
+ def opsi_uri
191
+ @legislation.opsi_uri_for_section(number)
192
+ end
96
193
  end
97
194
  end
98
195
 
@@ -100,7 +197,7 @@ end
100
197
  # See README for usage documentation.
101
198
  module Legislation
102
199
  module UK
103
- VERSION = "0.0.2"
200
+ VERSION = "0.0.3"
104
201
 
105
202
  def self.open_uri uri
106
203
  open(uri).read
@@ -111,7 +208,9 @@ module Legislation
111
208
  xml.gsub!('dc:type','dc:the_type')
112
209
  hash = Hash.from_xml(xml)
113
210
  namespace = LegislationUK
114
- Morph.from_hash(hash, namespace)
211
+ legislation = Morph.from_hash(hash, namespace)
212
+ legislation.populate
213
+ legislation
115
214
  end
116
215
 
117
216
  def self.find title, number=nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legislation-uk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob McKinnon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-29 00:00:00 +01:00
12
+ date: 2009-10-02 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency