Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings proof of concept with a yaml file #163

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/design-decisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ this app:
is obscure, that is a code smell and it is better to refactor
the code and make it more readable (and hence maintainable).

* The site specific settings are separated from the site using
a hash whose keys are converted to methods for ease of use. this
hash is passed to the classes in their constructor so that we
don't keep adding arguments to it, but instead the class takes
care of what it needs from the hash.


## Code structure

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'rake'
gem 'rdiscount'
gem 'sass'
gem 'sinatra'
gem 'sinatra-contrib'

group :test do
gem 'minitest'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ GEM
autoprefixer-rails (6.7.0)
execjs
babosa (1.0.2)
backports (3.7.0)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
Expand All @@ -36,6 +37,7 @@ GEM
method_source (0.8.2)
mini_portile2 (2.1.0)
minitest (5.10.1)
multi_json (1.12.1)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
parser (2.4.0.0)
Expand Down Expand Up @@ -73,6 +75,13 @@ GEM
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sinatra-contrib (1.4.7)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (>= 1.3, < 3)
slop (3.6.0)
term-ansicolor (1.4.0)
tins (~> 1.0)
Expand Down Expand Up @@ -102,6 +111,7 @@ DEPENDENCIES
rubocop
sass
sinatra
sinatra-contrib
webmock

RUBY VERSION
Expand Down
10 changes: 4 additions & 6 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'bootstrap-sass'
require 'everypolitician'
require 'sinatra'
require 'sinatra/config_file'

require_relative 'lib/featured_person'
require_relative 'lib/document/finder'
Expand All @@ -23,13 +24,12 @@
require_relative 'lib/page/post'
require_relative 'lib/page/posts'

set :contact_email, '[email protected]'
config_file 'config/general.yml'

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'
set :mapit_user_agent, ENV.fetch('MAPIT_USER_AGENT', nil)
set :twitter_user, 'NGShineYourEye'

# Create a wrapper for the mappings between the various IDs we have
# to use for areas / places.
Expand All @@ -49,9 +49,7 @@
# Create a wrapper that caches MapIt and EveryPolitician area data:
mapit = Mapit::Wrapper.new(
mapit_mappings: mapit_mappings,
baseurl: '/place/',
area_types: %w(FED SEN STA),
data_directory: 'mapit'
mapit_settings: settings.mapit_settings
)

person_factory = Factory::Person.new(mapit: mapit, baseurl: '/person/', identifier_scheme: 'shineyoureye')
Expand Down
10 changes: 10 additions & 0 deletions config/general.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
contact_email: '[email protected]'
mapit_url: 'http://nigeria.mapit.mysociety.org'
twitter_user: 'NGShineYourEye'
mapit_settings:
baseurl: '/place/'
area_types:
- FED
- SEN
- STA
data_directory: 'mapit'
8 changes: 4 additions & 4 deletions lib/mapit/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

module Mapit
class Wrapper
def initialize(mapit_mappings:, baseurl:, area_types:, data_directory:)
@baseurl = baseurl
@area_types = area_types
def initialize(mapit_mappings:, mapit_settings:)
@baseurl = mapit_settings.baseurl
@area_types = mapit_settings.area_types
@mapit_mappings = mapit_mappings
@data_directory = data_directory
@data_directory = mapit_settings.data_directory
cache_mapit_data
set_up_parent_child_relationships
end
Expand Down
12 changes: 5 additions & 7 deletions tests/mapit/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
let(:mapit) do
Mapit::Wrapper.new(
mapit_mappings: FakeMappings.new,
baseurl: '/baseurl/',
area_types: %w(FED SEN STA),
data_directory: 'mapit'
mapit_settings: settings.mapit_settings
)
end

Expand All @@ -27,7 +25,7 @@
end

it 'has states that use the baseurl in their url' do
mapit.places_of_type('STA').first.url.must_equal('/baseurl/abia/')
mapit.places_of_type('STA').first.url.must_equal('/place/abia/')
end

it 'does not have parent data for the states' do
Expand All @@ -45,15 +43,15 @@
end

it 'has federal constituencies that use the baseurl' do
mapit.places_of_type('FED').first.url.must_equal('/baseurl/gwagwaladakuje/')
mapit.places_of_type('FED').first.url.must_equal('/place/gwagwaladakuje/')
end

it 'has federal constituencies with a parent name' do
mapit.places_of_type('FED').first.parent.name.must_equal('Federal Capital Territory')
end

it 'has federal constituencies with a parent url' do
mapit.places_of_type('FED').first.parent.url.must_equal('/baseurl/federal-capital-territory/')
mapit.places_of_type('FED').first.parent.url.must_equal('/place/federal-capital-territory/')
end
end

Expand All @@ -67,7 +65,7 @@
end

it 'has senatorial districts that use the baseurl' do
mapit.places_of_type('SEN').first.url.must_equal('/baseurl/abia-central/')
mapit.places_of_type('SEN').first.url.must_equal('/place/abia-central/')
end

it 'has senatorial districts with a parent name' do
Expand Down
9 changes: 9 additions & 0 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
ENV['RACK_ENV'] = 'test'

require 'everypolitician'
require 'json'
require 'minitest/autorun'
require 'nokogiri'
require 'ostruct'
require 'pry'
require 'rack/test'
require 'webmock/minitest'
require 'yaml'

require_relative '../lib/document/markdown_with_frontmatter'
require_relative './test_doubles'
Expand Down Expand Up @@ -68,6 +71,11 @@ def parsed_mapit_data_for_area_type(area_type)
JSON.parse(mapit_data_for_area_type(area_type))
end

def settings
site_specific_settings = YAML.safe_load(SETTINGS_PATH) || {}
JSON.parse(site_specific_settings.to_json, object_class: OpenStruct)
end

private

DATASOURCE = 'https://github.com/everypolitician/everypolitician-data/raw/master/countries.json'
Expand All @@ -76,6 +84,7 @@ def parsed_mapit_data_for_area_type(area_type)

EP_DISK_PATH = 'tests/fixtures/ep_data'
MAPIT_DISK_PATH = 'tests/fixtures/mapit_data'
SETTINGS_PATH = 'config/general.yml'

COUNTRIES_COMMIT = 'd96d2be'
REPS_COMMIT = '1e00ca8'
Expand Down