openaustralia 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +55 -0
- data/CHANGES.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.md +23 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/lib/openaustralia.rb +6 -0
- data/lib/openaustralia/api.rb +248 -0
- data/lib/openaustralia/comments.rb +28 -0
- data/lib/openaustralia/debates.rb +43 -0
- data/lib/openaustralia/divisions.rb +22 -0
- data/lib/openaustralia/hansard.rb +62 -0
- data/lib/openaustralia/representatives.rb +51 -0
- data/lib/openaustralia/search_info.rb +14 -0
- data/lib/openaustralia/senators.rb +51 -0
- data/lib/openaustralia/version.rb +3 -0
- data/openaustralia.gemspec +20 -0
- data/test/comments.xml +282 -0
- data/test/debate_search_reps.xml +181 -0
- data/test/debate_search_senate.xml +652 -0
- data/test/division_search_2077.xml +6 -0
- data/test/hansard_utegate_search.xml +235 -0
- data/test/nsw_senators.xml +75 -0
- data/test/representative_abbott.xml +31 -0
- data/test/representatives_search_andrew.xml +31 -0
- data/test/senator_steve_hutchison.xml +19 -0
- data/test/test_api.rb +78 -0
- data/test/test_helper.rb +4 -0
- data/test/test_parse.rb +45 -0
- data/test/test_res.xml +31 -0
- metadata +113 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
|
7
|
+
environment_id="ruby-1.9.2-p290"
|
8
|
+
|
9
|
+
#
|
10
|
+
# Uncomment following line if you want options to be set only for given project.
|
11
|
+
#
|
12
|
+
# PROJECT_JRUBY_OPTS=( --1.9 )
|
13
|
+
|
14
|
+
#
|
15
|
+
# First we attempt to load the desired environment directly from the environment
|
16
|
+
# file. This is very fast and efficient compared to running through the entire
|
17
|
+
# CLI and selector. If you want feedback on which environment was used then
|
18
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
19
|
+
#
|
20
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
21
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
22
|
+
then
|
23
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
24
|
+
|
25
|
+
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
26
|
+
then
|
27
|
+
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
28
|
+
fi
|
29
|
+
else
|
30
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
31
|
+
if ! rvm --create "$environment_id"
|
32
|
+
then
|
33
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
34
|
+
exit 1
|
35
|
+
fi
|
36
|
+
fi
|
37
|
+
|
38
|
+
#
|
39
|
+
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
|
40
|
+
# it be automatically loaded. Uncomment the following and adjust the filename if
|
41
|
+
# necessary.
|
42
|
+
#
|
43
|
+
# filename=".gems"
|
44
|
+
# if [[ -s "$filename" ]]
|
45
|
+
# then
|
46
|
+
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
|
47
|
+
# fi
|
48
|
+
|
49
|
+
# If you use bundler, this might be useful to you:
|
50
|
+
# if command -v bundle && [[ -s Gemfile ]]
|
51
|
+
# then
|
52
|
+
# bundle install
|
53
|
+
# fi
|
54
|
+
|
55
|
+
|
data/CHANGES.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.0.0 - 2012-07-13
|
4
|
+
|
5
|
+
* Major overhaul to bring gem up to date
|
6
|
+
** Most significant change for users is the gem has been renamed from 'open-australia' to 'openaustralia'
|
7
|
+
** Fixed `get_divisions`
|
8
|
+
|
9
|
+
## 0.0.1 - 2009-12-08
|
10
|
+
|
11
|
+
* Initial release:
|
12
|
+
** API methods wrapped
|
13
|
+
** parses correctly on selective canned data
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2009 Alex Cooper
|
2
|
+
Copyright (c) 2012 Henare Degan
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# OpenAustralia
|
2
|
+
|
3
|
+
A Ruby wrapper for the [OpenAustralia API](http://www.openaustralia.org/api/).
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
* An account on openaustralia.org
|
8
|
+
* An API key obtained from http://openaustralia.org/api
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'openaustralia'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install openaustralia
|
23
|
+
|
24
|
+
## Features/problems
|
25
|
+
|
26
|
+
* Wraps the current API calls
|
27
|
+
* So far no validation on validity of parameters or search terms
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
# spit out excerpts of what's been said
|
32
|
+
# about the emissions trading scheme
|
33
|
+
|
34
|
+
require 'openaustralia'
|
35
|
+
|
36
|
+
api = OpenAustralia::Api.new 'YOUR-API-KEY-HERE'
|
37
|
+
|
38
|
+
search = api.get_hansard :search => 'emissions trading'
|
39
|
+
search.results.each do |result|
|
40
|
+
puts result.body
|
41
|
+
end
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,248 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'cgi'
|
3
|
+
require 'xml/mapping'
|
4
|
+
require 'rexml/document'
|
5
|
+
|
6
|
+
require 'openaustralia/divisions'
|
7
|
+
require 'openaustralia/representatives'
|
8
|
+
require 'openaustralia/senators'
|
9
|
+
require 'openaustralia/debates'
|
10
|
+
require 'openaustralia/hansard'
|
11
|
+
require 'openaustralia/search_info'
|
12
|
+
require 'openaustralia/comments'
|
13
|
+
|
14
|
+
module OpenAustralia
|
15
|
+
|
16
|
+
# Ruby wrapper for the openaustralia.org API
|
17
|
+
#
|
18
|
+
# You'll need an API key: get one at http://openaustralia.org/api
|
19
|
+
#
|
20
|
+
class Api
|
21
|
+
|
22
|
+
# create an API instance with the specified key
|
23
|
+
# Get your key from http://openaustralia.org/api
|
24
|
+
def initialize(key)
|
25
|
+
@key = key
|
26
|
+
@xml_getter = XmlGetter.new #replaced for unit tests
|
27
|
+
end
|
28
|
+
|
29
|
+
# get_divisions returns a list of electoral divisions
|
30
|
+
#
|
31
|
+
# supported search parameters:
|
32
|
+
# postcode (optional)
|
33
|
+
# Fetch the list of electoral divisions that are within the given
|
34
|
+
# postcode (there can be more than one)
|
35
|
+
# date (optional)
|
36
|
+
# Fetch the list of electoral divisions as it was on this date.
|
37
|
+
# search (optional)
|
38
|
+
# Fetch the list of electoral divisions that match this search string.
|
39
|
+
#
|
40
|
+
# example:
|
41
|
+
# get_disivions :postcode => 3022
|
42
|
+
#
|
43
|
+
# NB: at the time of writing, only the postcode parameter seems
|
44
|
+
# to actually work.
|
45
|
+
def get_divisions(search_params = {})
|
46
|
+
xml = do_request('getDivisions', search_params)
|
47
|
+
DivisionResult.load_from_xml xml
|
48
|
+
end
|
49
|
+
|
50
|
+
# Fetch a particular member of the House of Representatives.
|
51
|
+
#
|
52
|
+
# Arguments
|
53
|
+
#
|
54
|
+
# id (optional)
|
55
|
+
# If you know the person ID for the member you want (returned from
|
56
|
+
# getRepresentatives or elsewhere), this will return data for
|
57
|
+
# that person.
|
58
|
+
# division (optional)
|
59
|
+
# The name of an electoral division; we will try and work it out from
|
60
|
+
# whatever you give us. :)
|
61
|
+
# always_return (optional)
|
62
|
+
# For the division option, sets whether to always try and return a
|
63
|
+
# Representative, even if the seat is currently vacant.
|
64
|
+
#
|
65
|
+
# example:
|
66
|
+
# get_representative :id => 10001
|
67
|
+
#
|
68
|
+
def get_representative(search_params = {})
|
69
|
+
xml = do_request('getRepresentative', search_params)
|
70
|
+
result = RepresentativeResult.load_from_xml xml
|
71
|
+
result.matches.first
|
72
|
+
end
|
73
|
+
|
74
|
+
#Fetch a list of members of the House of Representatives.
|
75
|
+
#Arguments
|
76
|
+
#
|
77
|
+
#postcode (optional)
|
78
|
+
# Fetch the list of Representatives whose electoral division lies within
|
79
|
+
# the postcode (there may be more than one)
|
80
|
+
#date (optional)
|
81
|
+
# Fetch the list of members of the House of Representatives as it was on
|
82
|
+
# this date.
|
83
|
+
#party (optional)
|
84
|
+
# Fetch the list of Representatives from the given party.
|
85
|
+
#search (optional)
|
86
|
+
# Fetch the list of Representatives that match this search string in
|
87
|
+
# their name.
|
88
|
+
#
|
89
|
+
#example
|
90
|
+
# api.get_representatives :postcode => 2000
|
91
|
+
#
|
92
|
+
#NB: if more than one representative is returned, then the 'office' array
|
93
|
+
# will be empty.
|
94
|
+
def get_representatives(search_params = {})
|
95
|
+
xml = do_request('getRepresentatives', search_params)
|
96
|
+
result = RepresentativeResult.load_from_xml xml
|
97
|
+
result.matches
|
98
|
+
end
|
99
|
+
|
100
|
+
#Fetch a particular Senator.
|
101
|
+
#Arguments
|
102
|
+
#
|
103
|
+
#id (required)
|
104
|
+
# If you know the person ID for the Senator you want, this will return data for that person.
|
105
|
+
#
|
106
|
+
#example
|
107
|
+
# api.get_senator :id => 10214
|
108
|
+
def get_senator(search_params = {})
|
109
|
+
xml = do_request('getSenator', search_params)
|
110
|
+
result = SenatorResult.load_from_xml xml
|
111
|
+
result.matches.first
|
112
|
+
end
|
113
|
+
|
114
|
+
#Fetch a list of Senators.
|
115
|
+
#Arguments
|
116
|
+
#
|
117
|
+
#date (optional)
|
118
|
+
# Fetch the list of Senators as it was on this date.
|
119
|
+
#party (optional)
|
120
|
+
# Fetch the list of Senators from the given party.
|
121
|
+
#state (optional)
|
122
|
+
# Fetch the list of Senators from the given state.
|
123
|
+
#search (optional)
|
124
|
+
# Fetch the list of Senators that match this search string in their name.
|
125
|
+
#
|
126
|
+
#example
|
127
|
+
# api.get_senators :search => 'brown'
|
128
|
+
#
|
129
|
+
def get_senators(search_params = {})
|
130
|
+
xml = do_request('getSenators', search_params)
|
131
|
+
result = SenatorSearchResult.load_from_xml xml
|
132
|
+
result.matches
|
133
|
+
end
|
134
|
+
|
135
|
+
#Fetch Debates.
|
136
|
+
#
|
137
|
+
#This includes Oral Questions.
|
138
|
+
#Arguments
|
139
|
+
#
|
140
|
+
#Note you can only supply one of the following search terms at present.
|
141
|
+
#
|
142
|
+
#type (required)
|
143
|
+
# One of "representatives" or "senate".
|
144
|
+
#date
|
145
|
+
# Fetch the debates for this date.
|
146
|
+
#search
|
147
|
+
# Fetch the debates that contain this term.
|
148
|
+
#person
|
149
|
+
# Fetch the debates by a particular person ID.
|
150
|
+
#gid
|
151
|
+
# Fetch the speech or debate that matches this GID.
|
152
|
+
#order (optional, when using search or person)
|
153
|
+
# d for date ordering, r for relevance ordering.
|
154
|
+
#page (optional, when using search or person)
|
155
|
+
# Page of results to return.
|
156
|
+
#num (optional, when using search or person)
|
157
|
+
# Number of results to return.
|
158
|
+
def get_debates(search_params = {})
|
159
|
+
xml = do_request('getDebates', search_params)
|
160
|
+
DebateSearch.load_from_xml xml.root
|
161
|
+
end
|
162
|
+
|
163
|
+
#Fetch all Hansard.
|
164
|
+
#Arguments
|
165
|
+
#
|
166
|
+
#Note you can only supply one of the following at present.
|
167
|
+
#
|
168
|
+
#search
|
169
|
+
# Fetch the data that contain this term.
|
170
|
+
#person
|
171
|
+
# Fetch the data by a particular person ID.
|
172
|
+
#order (optional, when using search or person, defaults to date)
|
173
|
+
# d for date ordering, r for relevance ordering, p for use by person.
|
174
|
+
#page (optional, when using search or person)
|
175
|
+
# Page of results to return.
|
176
|
+
#num (optional, when using search or person)
|
177
|
+
# Number of results to return.
|
178
|
+
#
|
179
|
+
def get_hansard(search_params = {})
|
180
|
+
xml = do_request('getHansard', search_params)
|
181
|
+
HansardSearch.load_from_xml xml.root
|
182
|
+
end
|
183
|
+
|
184
|
+
#Fetch comments left on OpenAustralia.
|
185
|
+
#
|
186
|
+
#With no arguments, returns most recent comments in reverse date order.
|
187
|
+
#Arguments
|
188
|
+
#
|
189
|
+
#date (optional)
|
190
|
+
# Fetch the comments for this date.
|
191
|
+
#search (optional)
|
192
|
+
# Fetch the comments that contain this term.
|
193
|
+
#user_id (optional)
|
194
|
+
# Fetch the comments by a particular user ID.
|
195
|
+
#pid
|
196
|
+
# Fetch the comments made on a particular person ID (Representative/Senator).
|
197
|
+
#page (optional)
|
198
|
+
# Page of results to return.
|
199
|
+
#num (optional)
|
200
|
+
# Number of results to return.
|
201
|
+
def get_comments(search_params = {})
|
202
|
+
xml = do_request('getComments', search_params)
|
203
|
+
CommentsSearch.load_from_xml xml
|
204
|
+
end
|
205
|
+
|
206
|
+
private
|
207
|
+
|
208
|
+
# process the specified API request,
|
209
|
+
# returning XML
|
210
|
+
def do_request(command, params)
|
211
|
+
url = request_url(command, params)
|
212
|
+
@xml_getter.fetch url
|
213
|
+
end
|
214
|
+
|
215
|
+
# replace the XML getter with a different one
|
216
|
+
# (used by unit tests
|
217
|
+
def xml_getter=(getter)
|
218
|
+
@xml_getter = getter
|
219
|
+
end
|
220
|
+
|
221
|
+
# the URL for the given request
|
222
|
+
def request_url(command, params)
|
223
|
+
url = "http://www.openaustralia.org/api/#{command}?key=#{@key}&output=xml"
|
224
|
+
params.each do |k,v|
|
225
|
+
key = CGI::escape(k.to_s)
|
226
|
+
value = CGI::escape(v.to_s)
|
227
|
+
url += "&#{key}=#{value}"
|
228
|
+
end
|
229
|
+
url
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
# XmlGetter is a dependency for fetching a URL
|
234
|
+
# and parsing it as XML. We're using a separate
|
235
|
+
# dependency so we can inject a different one
|
236
|
+
# for unit testing
|
237
|
+
class XmlGetter
|
238
|
+
|
239
|
+
# fetch the specified URL and parse
|
240
|
+
# into a REXML document
|
241
|
+
def fetch(url)
|
242
|
+
open(url) do |f|
|
243
|
+
return REXML::Document.new f
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'xml/mapping'
|
2
|
+
|
3
|
+
module OpenAustralia
|
4
|
+
|
5
|
+
# comments
|
6
|
+
class Comments
|
7
|
+
include XML::Mapping
|
8
|
+
|
9
|
+
numeric_node :comment_id, 'comment_id'
|
10
|
+
numeric_node :user_id, 'user_id'
|
11
|
+
text_node :body, 'body'
|
12
|
+
text_node :posted, 'posted'
|
13
|
+
numeric_node :major, 'major'
|
14
|
+
text_node :gid, 'gid'
|
15
|
+
text_node :firstname, 'firstname'
|
16
|
+
text_node :lastname, 'lastname'
|
17
|
+
text_node :url, 'url'
|
18
|
+
end
|
19
|
+
|
20
|
+
# search results from a comments search
|
21
|
+
class CommentsSearch # :nodoc:
|
22
|
+
include XML::Mapping
|
23
|
+
|
24
|
+
array_node :results, 'comments', 'match', :class => Comments,
|
25
|
+
:default_value => []
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|