parliament-ruby is a gem created by the Parliamentary Digital Service to allow easy communication with the internal parliament data API.
NOTE: This gem is in active development and is likely to change at short notice. It is not recommended that you use this in any production environment.
parliament-ruby requires the following:
- Ruby - click here for the exact version
- Bundler
gem 'parliament-ruby'
This gem's main function is fetching an n-triples from a remote server and converting it into linked ruby objects.
Note: Comprehensive class documentation can be found on rubydocs.
In order to connect to a remote server, we first need to set a base_url value, from which we will build an 'endpoint'. The base_url should be the beginning of a url without the trailing slash. For example http://example.com
instead of http://example.com/
.
parliament = Parliament::Request.new(base_url: 'http://test.com')
Within code you can set a global base URL using the following snippet.
Parliament::Request.base_url = 'http://test.com'
# The base_url should be set for all new objects
Parliament::Request.new.base_url #=> 'http://test.com'
# You can still override the base_url on an instance by instance basis
Parliament::Request.new(base_url: 'http://example.com').base_url #=> 'http://example.com'
Alternatively, you can set the environment variable PARLIAMENT_BASE_URL
on your machine and we will automatically use that.
ENV['PARLIAMENT_BASE_URL'] #=> 'http://example.com'
Parliament::Request.base_url #=> nil
Parliament::Request.new.base_url #=> 'http://example.com'
Now that we have a base_url
set, we will want to build an 'endpoint' such as: http://test.com/parties/current
.
An endpoint is effectively just a full URL to an n-triple file on a remote server.
Building an endpoint is simple, once you have a Parliament::Request object, you do something like this:
parliament = Parliament::Request.new(base_url: 'http://test.com') #=> #<Parliament::Request [...]>
# Target endpoint: 'http://test.com/parties/current'
parliament.parties.current
# Target endpoint: 'http://test.com/parties/123/people/current'
parliament.parties('123').people.current
# Target endpoint: 'http://test.com/people/123/letters/456'
parliament.people('123').letters('456')
Within the code you can set global headers using the following snippet.
Parliament::Request.headers = { 'Accept' => 'Test' }
# The headers should be set for all new objects
Parliament::Request.new.headers #=> { 'Accept' => 'Test' }
# You can still override the headers on an instance by instance basis
Parliament::Request.new(headers: { 'Accept' => 'Test2' }).headers #=> { 'Accept' => 'Test2' }
To clone the repository and set up the dependencies, run the following:
git clone https://github.com/ukparliament/parliament-ruby.git
cd parliament-ruby
bundle install
We use RSpec as our testing framework and tests can be run using:
bundle exec rake
If you wish to submit a bug fix or feature, you can create a pull request and it will be merged pending a code review.
- Fork the repository
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Ensure your changes are tested using Rspec
- Create a new Pull Request
parliament-ruby is licensed under the Open Parliament Licence.