Skip to content

Commit

Permalink
Switch to using the local copy of the MapIt Area data
Browse files Browse the repository at this point in the history
To avoid having to fetch data from MapIt every time the server is
started, we can use the area JSON in the repository. (The MapIt
data changes very occasionally, and usually as a result of manual
intervention to update it or make a fix, so not getting a live
copy every time shouldn't cause problems.)

This also enables us to simplify the code quite significantly.
  • Loading branch information
mhl committed Mar 3, 2017
1 parent a53b196 commit fdeb31c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
5 changes: 2 additions & 3 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
set :content_dir, File.join(__dir__, 'prose')
set :datasource, ENV.fetch('DATASOURCE', 'https://github.com/everypolitician/everypolitician-data/raw/master/countries.json')
set :index, EveryPolitician::Index.new(index_url: settings.datasource)
set :mapit_url, 'http://nigeria.mapit.mysociety.org/areas/'
set :twitter_user, 'NGShineyoureye'

# Create a wrapper for the mappings between the various IDs we have
Expand All @@ -42,10 +41,10 @@

# Create a wrapper that caches MapIt and EveryPolitician area data:
mapit = Mapit::Wrapper.new(
mapit_url: settings.mapit_url,
mapit_mappings: mapit_mappings,
baseurl: '/place/',
area_types: %w(FED SEN STA)
area_types: %w(FED SEN STA),
data_directory: 'mapit'
)

# Assemble data on the members of the various legislatures we support:
Expand Down
22 changes: 14 additions & 8 deletions lib/mapit/wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# frozen_string_literal: true
require 'net/http'
require_relative 'place'

module Mapit
class Wrapper

def initialize(mapit_url:, mapit_mappings:, baseurl:, area_types:)
@mapit_url = mapit_url
def initialize(mapit_mappings:, baseurl:, area_types:, data_directory:)
@baseurl = baseurl
@area_types = area_types
@mapit_mappings = mapit_mappings
@data_directory = data_directory
cache_mapit_data
set_up_parent_child_relationships
end
Expand All @@ -29,20 +28,27 @@ def places_of_type(area_type)

private

attr_reader :mapit_url,
:mapit_mappings,
attr_reader :mapit_mappings,
:baseurl,
:id_to_place,
:area_types,
:type_to_places
:type_to_places,
:data_directory

def places(area_type)
areas_data(area_type).map { |a| create_place(a) }
end

def mapit_area_cache_filename(area_type)
File.join(data_directory, "#{area_type}.json")
end

def parse_json_file(filename)
JSON.parse(open(filename, &:read))
end

def areas_data(area_type)
uri = URI(mapit_url + area_type)
JSON.parse(Net::HTTP.get(uri)).values
parse_json_file(mapit_area_cache_filename(area_type)).values
end

def cache_mapit_data
Expand Down
4 changes: 2 additions & 2 deletions tests/mapit/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

describe 'Mappit::Wrapper' do
let(:mapit) { Mapit::Wrapper.new(
mapit_url: mapit_url,
mapit_mappings: FakeMappings.new,
baseurl: '/baseurl/',
area_types: %w(FED SEN STA)
area_types: %w(FED SEN STA),
data_directory: 'mapit'
) }

describe 'when getting the states' do
Expand Down
7 changes: 0 additions & 7 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def app
get_from_disk(DATASOURCE, countries_json)
get_from_disk(reps_json_url, reps_json)
get_from_disk(senate_json_url, senate_json)
get_from_disk("#{mapit_url}STA", mapit_data_for_area_type('STA'))
get_from_disk("#{mapit_url}FED", mapit_data_for_area_type('FED'))
get_from_disk("#{mapit_url}SEN", mapit_data_for_area_type('SEN'))
end

def new_tempfile(contents, filename = 'sye-tests')
Expand All @@ -48,10 +45,6 @@ def basic_document(filename)
Document::MarkdownWithFrontmatter.new(filename: filename, baseurl: 'irrelevant')
end

def mapit_url
'http://nigeria.mapit.mysociety.org/areas/'
end

def nigeria_at_known_revision
@ng ||= EveryPolitician::Index.new(index_url: DATASOURCE).country('Nigeria')
end
Expand Down

0 comments on commit fdeb31c

Please sign in to comment.