-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from alphagov/add-constituency-to-api
Add constituency/country signature counts to API
- Loading branch information
Showing
51 changed files
with
1,209 additions
and
637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require_dependency 'constituency/api_client' | ||
require_dependency 'constituency/api_query' | ||
|
||
class Constituency < ActiveRecord::Base | ||
MP_URL = "http://www.parliament.uk/biographies/commons" | ||
|
||
has_many :signatures, primary_key: :external_id | ||
has_many :petitions, through: :signatures | ||
|
||
validates :name, presence: true, length: { maximum: 100 } | ||
validates :external_id, presence: true, length: { maximum: 30 } | ||
validates :ons_code, presence: true, format: %r[\A(?:E|W|S|N)\d{8}\z] | ||
validates :mp_id, length: { maximum: 30 } | ||
validates :mp_name, length: { maximum: 100 } | ||
|
||
before_validation if: :name_changed? do | ||
self.slug = name.parameterize | ||
end | ||
|
||
class << self | ||
def find_by_postcode(postcode) | ||
results = query.fetch(postcode) | ||
|
||
if attributes = results.first | ||
find_or_initialize_by(external_id: attributes[:external_id]) do |constituency| | ||
constituency.attributes = attributes | ||
|
||
if constituency.changed? || constituency.new_record? | ||
constituency.save! | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def query | ||
ApiQuery.new | ||
end | ||
end | ||
|
||
def mp_url | ||
"#{MP_URL}/#{mp_name.parameterize}/#{mp_id}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'faraday' | ||
require 'postcode_sanitizer' | ||
|
||
class Constituency < ActiveRecord::Base | ||
class ApiClient | ||
HOST = 'http://data.parliament.uk' | ||
ENDPOINT = '/membersdataplatform/services/mnis/Constituencies/%{postcode}/' | ||
TIMEOUT = 5 | ||
|
||
def call(postcode) | ||
faraday.get(path(postcode)) do |request| | ||
request.options[:timeout] = TIMEOUT | ||
request.options[:open_timeout] = TIMEOUT | ||
end | ||
end | ||
|
||
private | ||
|
||
def faraday | ||
@faraday ||= Faraday.new(HOST) do |f| | ||
f.response :follow_redirects | ||
f.response :raise_error | ||
f.adapter Faraday.default_adapter | ||
end | ||
end | ||
|
||
def path(postcode) | ||
ENDPOINT % { postcode: escape_path(postcode) } | ||
end | ||
|
||
def escape_path(value) | ||
Rack::Utils.escape_path(sanitize(value)) | ||
end | ||
|
||
def sanitize(value) | ||
PostcodeSanitizer.call(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'nokogiri' | ||
|
||
class Constituency < ActiveRecord::Base | ||
class ApiQuery | ||
CONSTITUENCIES = '//Constituencies/Constituency' | ||
CONSTITUENCY_ID = './Constituency_Id' | ||
CONSTITUENCY_NAME = './Name' | ||
CONSTITUENCY_CODE = './ONSCode' | ||
|
||
CURRENT_MP = './RepresentingMembers/RepresentingMember[1]' | ||
MP_ID = './Member_Id' | ||
MP_NAME = './Member' | ||
MP_DATE = './StartDate' | ||
|
||
def fetch(postcode) | ||
response = client.call(postcode) | ||
|
||
if response.success? | ||
parse(response.body) | ||
else | ||
[] | ||
end | ||
rescue Faraday::ResourceNotFound, Faraday::ClientError => e | ||
return [] | ||
rescue Faraday::Error => e | ||
Appsignal.send_exception(e) if defined?(Appsignal) | ||
return [] | ||
end | ||
|
||
private | ||
|
||
def client | ||
@client ||= ApiClient.new | ||
end | ||
|
||
def parse(body) | ||
xml = Nokogiri::XML(body) | ||
|
||
xml.xpath(CONSTITUENCIES).map do |node| | ||
{}.tap do |attrs| | ||
attrs[:name] = node.xpath(CONSTITUENCY_NAME).text | ||
attrs[:external_id] = node.xpath(CONSTITUENCY_ID).text | ||
attrs[:ons_code] = node.xpath(CONSTITUENCY_CODE).text | ||
|
||
if mp = node.at_xpath(CURRENT_MP) | ||
attrs[:mp_id] = mp.xpath(MP_ID).text | ||
attrs[:mp_name] = mp.xpath(MP_NAME).text | ||
attrs[:mp_date] = mp.xpath(MP_DATE).text | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.