parliament-utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.hound.yml +4 -0
- data/.rspec +2 -0
- data/.rubocop.yml +55 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Instructions - Steps to include in app.md +95 -0
- data/LICENSE +7 -0
- data/Makefile +16 -0
- data/README.md +177 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +387 -0
- data/config/puma.rb +20 -0
- data/lib/parliament/utils/config/initializers/airbrake.rb +76 -0
- data/lib/parliament/utils/config/initializers/assets.rb +11 -0
- data/lib/parliament/utils/config/initializers/backtrace_silencers.rb +7 -0
- data/lib/parliament/utils/config/initializers/bandiera.rb +3 -0
- data/lib/parliament/utils/config/initializers/cookies_serializer.rb +3 -0
- data/lib/parliament/utils/config/initializers/filter_parameter_logging.rb +4 -0
- data/lib/parliament/utils/config/initializers/inflections.rb +16 -0
- data/lib/parliament/utils/config/initializers/mime_types.rb +7 -0
- data/lib/parliament/utils/config/initializers/session_store.rb +3 -0
- data/lib/parliament/utils/config/initializers/timeout.rb +1 -0
- data/lib/parliament/utils/config/initializers/wrap_parameters.rb +9 -0
- data/lib/parliament/utils/config/initializers.rb +16 -0
- data/lib/parliament/utils/helpers/application_helper.rb +68 -0
- data/lib/parliament/utils/helpers/flag_helper.rb +29 -0
- data/lib/parliament/utils/helpers/format_helper.rb +18 -0
- data/lib/parliament/utils/helpers/houses_helper.rb +80 -0
- data/lib/parliament/utils/helpers/parliament_helper.rb +22 -0
- data/lib/parliament/utils/helpers/postcode_helper.rb +72 -0
- data/lib/parliament/utils/helpers/request_helper.rb +30 -0
- data/lib/parliament/utils/helpers/translation_helper.rb +12 -0
- data/lib/parliament/utils/helpers/v_card_helper.rb +57 -0
- data/lib/parliament/utils/helpers.rb +18 -0
- data/lib/parliament/utils/railtie.rb +12 -0
- data/lib/parliament/utils/test_helpers/bandiera_helper.rb +19 -0
- data/lib/parliament/utils/test_helpers/rails_helper.rb +42 -0
- data/lib/parliament/utils/test_helpers/rspec_helper.rb +76 -0
- data/lib/parliament/utils/test_helpers/simplecov_helper.rb +20 -0
- data/lib/parliament/utils/test_helpers/vcr_helper.rb +83 -0
- data/lib/parliament/utils/test_helpers/webmock_helper.rb +13 -0
- data/lib/parliament/utils/test_helpers.rb +19 -0
- data/lib/parliament/utils/version.rb +5 -0
- data/lib/parliament/utils.rb +10 -0
- data/log/test.log +0 -0
- data/parliament-utils.gemspec +41 -0
- metadata +318 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module Helpers
|
4
|
+
module FlagHelper
|
5
|
+
def self.dissolution?
|
6
|
+
# When in the dissolution period
|
7
|
+
BANDIERA_CLIENT.enabled?('parliament', 'show-dissolution')
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.register_to_vote?
|
11
|
+
# When in dissolution but still in the registration period
|
12
|
+
BANDIERA_CLIENT.enabled?('parliament', 'show-register')
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.election?
|
16
|
+
BANDIERA_CLIENT.enabled?('parliament', 'show-election')
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.post_election?
|
20
|
+
BANDIERA_CLIENT.enabled?('parliament', 'show-post-election')
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.election_period?
|
24
|
+
(election? == true || post_election? == true)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module Helpers
|
4
|
+
module FormatHelper
|
5
|
+
DATA_FORMATS = ['text/csv',
|
6
|
+
'text/vnd.graphviz',
|
7
|
+
'text/n3',
|
8
|
+
'application/json',
|
9
|
+
'application/n-triples',
|
10
|
+
'application/rdf+xml',
|
11
|
+
'application/rdf+json',
|
12
|
+
'application/xml',
|
13
|
+
'text/tab-separated-values',
|
14
|
+
'text/turtle'].freeze
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module Helpers
|
4
|
+
module HousesHelper
|
5
|
+
# Checks if house is the House of Commons and sets @commons_id and @lords_id if not already set.
|
6
|
+
#
|
7
|
+
# @param [Grom::Node] house a Grom::Node with type http://id.ukpds.org/schema/House.
|
8
|
+
# @return [Boolean] boolean depending on whether the house has the same id as the House of Commons.
|
9
|
+
def self.commons?(house)
|
10
|
+
set_ids
|
11
|
+
|
12
|
+
house.graph_id == @commons_id
|
13
|
+
end
|
14
|
+
|
15
|
+
# Checks if house is the House of Lords and sets @commons_id and @lords_id if not already set.
|
16
|
+
#
|
17
|
+
# @param [Grom::Node] house a Grom::Node with type http://id.ukpds.org/schema/House.
|
18
|
+
# @return [Boolean] boolean depending on whether the house has the same id as the House of Lords.
|
19
|
+
def self.lords?(house)
|
20
|
+
!commons?(house)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Sets and returns the id for the House of Commons.
|
24
|
+
#
|
25
|
+
# @return [String] @commons_id the id for the House of Commons.
|
26
|
+
def self.commons_id
|
27
|
+
set_ids
|
28
|
+
|
29
|
+
@commons_id
|
30
|
+
end
|
31
|
+
|
32
|
+
# Sets and returns the id for the House of Lords.
|
33
|
+
#
|
34
|
+
# @return [String] @lords_id the id for the House of Lords.
|
35
|
+
def self.lords_id
|
36
|
+
set_ids
|
37
|
+
|
38
|
+
@lords_id
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the current house id for the page alongside the other house id.
|
42
|
+
#
|
43
|
+
# @param [Grom::Node] house The house that a particular person is linked to.
|
44
|
+
# @return [Array<String, String>] The current house id and other house id.
|
45
|
+
def self.house_id_string(house)
|
46
|
+
is_commons = self.commons?(house)
|
47
|
+
|
48
|
+
house_ids = [commons_id, lords_id]
|
49
|
+
return house_ids if is_commons
|
50
|
+
|
51
|
+
house_ids.reverse
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns the current
|
55
|
+
#
|
56
|
+
# @param [Grom::Node] house The house that a particular person is linked to.
|
57
|
+
# @return [Array<String, String>] The current person type and other person type.
|
58
|
+
def self.person_type_string(house)
|
59
|
+
is_mp = self.commons?(house)
|
60
|
+
|
61
|
+
types = [I18n.t('mp_plural'), I18n.t('lord_plural')]
|
62
|
+
|
63
|
+
return types if is_mp
|
64
|
+
|
65
|
+
types.reverse
|
66
|
+
end
|
67
|
+
|
68
|
+
private_class_method
|
69
|
+
|
70
|
+
def self.set_ids
|
71
|
+
return if @commons_id && @lords_id
|
72
|
+
houses = Parliament::Utils::Helpers::ParliamentHelper.parliament_request.houses.get.filter('http://id.ukpds.org/schema/House').sort_by(:name)
|
73
|
+
|
74
|
+
@commons_id = houses.first.graph_id
|
75
|
+
@lords_id = houses.last.graph_id
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'parliament'
|
2
|
+
require 'parliament/ntriple'
|
3
|
+
|
4
|
+
module Parliament
|
5
|
+
module Utils
|
6
|
+
module Helpers
|
7
|
+
module ParliamentHelper
|
8
|
+
def self.parliament_request
|
9
|
+
Parliament::Request::UrlRequest.new(
|
10
|
+
builder: Parliament::Builder::NTripleResponseBuilder,
|
11
|
+
headers: { 'Ocp-Apim-Subscription-Key': ENV['PARLIAMENT_AUTH_TOKEN'] },
|
12
|
+
decorators: Parliament::Grom::Decorator
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def parliament_request
|
17
|
+
Parliament::Utils::Helpers::ParliamentHelper.parliament_request
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require 'parliament/utils/helpers/translation_helper' unless defined?(::Rails)
|
3
|
+
|
4
|
+
# attr [String] previous_path the path to redirect back to in the case of an error.
|
5
|
+
module Parliament
|
6
|
+
module Utils
|
7
|
+
module Helpers
|
8
|
+
module PostcodeHelper
|
9
|
+
|
10
|
+
include Parliament::Utils::Helpers::TranslationHelper unless defined?(::Rails)
|
11
|
+
|
12
|
+
attr_accessor :previous_path
|
13
|
+
|
14
|
+
# Makes requests to the postcode_lookup endpoint.
|
15
|
+
# @param [String] raw_input the data inputted by the user.
|
16
|
+
# @return [Parliament::Response::NTripleResponse, Parliament::ClientError, Parliament::ServerError] if successful, a response is returned, otherwise an error is returned.
|
17
|
+
def self.lookup(raw_input)
|
18
|
+
postcode = clean_input(raw_input)
|
19
|
+
|
20
|
+
begin
|
21
|
+
Parliament::Utils::Helpers::ParliamentHelper.parliament_request.constituencies.postcode_lookup(postcode).get
|
22
|
+
rescue Parliament::ClientError
|
23
|
+
raise(PostcodeError, I18n.t('error.no_constituency').capitalize)
|
24
|
+
rescue Parliament::ServerError
|
25
|
+
raise(PostcodeError, I18n.t('error.lookup_unavailable').capitalize)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Replaces whitespace with a hyphen.
|
30
|
+
# @param [String] postcode the string to be hyphenated.
|
31
|
+
# @return [String] a hyphenated string.
|
32
|
+
def self.hyphenate(postcode)
|
33
|
+
postcode.strip.gsub(/\s+/, '-')
|
34
|
+
end
|
35
|
+
|
36
|
+
# Replaces a hyphen with whitespace, restoring a postcode to its usual format.
|
37
|
+
# @param [String] postcode the string to be processed.
|
38
|
+
# @return [String] the restored postcode with hyphen replaced by whitespace.
|
39
|
+
def self.unhyphenate(postcode)
|
40
|
+
postcode.tr('-', ' ')
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the previous path variable.
|
44
|
+
# @return [String] previous_path the path to redirect back to in the case of an error.
|
45
|
+
def self.previous_path
|
46
|
+
@previous_path
|
47
|
+
end
|
48
|
+
|
49
|
+
# Sets the previous path variable.
|
50
|
+
# @return [String] previous_path the path to redirect back to in the case of an error.
|
51
|
+
def self.previous_path=(path)
|
52
|
+
@previous_path = path
|
53
|
+
end
|
54
|
+
|
55
|
+
private_class_method
|
56
|
+
|
57
|
+
# We decided to use a loose regex - this matches the different postcode formats, but doesn't validate against invalid letters and numbers, only their positions.
|
58
|
+
def self.clean_input(raw_input)
|
59
|
+
postcode = raw_input.gsub(/\s+/, '').upcase
|
60
|
+
|
61
|
+
postcode.match(/[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/).nil? ? raise(PostcodeError, I18n.t('error.postcode_invalid').capitalize) : postcode
|
62
|
+
end
|
63
|
+
|
64
|
+
class PostcodeError < StandardError
|
65
|
+
def initialize(message)
|
66
|
+
super(message)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Namespace for Parliament::Request helper methods.
|
2
|
+
module Parliament
|
3
|
+
module Utils
|
4
|
+
module Helpers
|
5
|
+
module RequestHelper
|
6
|
+
# Takes a Parliament::Request and calls the #get method.
|
7
|
+
# Then maps the #value method on the resulting response.
|
8
|
+
#
|
9
|
+
# @param [Parliament::Request] request a built Parliament::Request object that can just be called with #get
|
10
|
+
#
|
11
|
+
# @return [Array<String>]
|
12
|
+
def self.process_available_letters(request)
|
13
|
+
response = request.get
|
14
|
+
response.map(&:value)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Takes a Parliament::Request and a optional amount of filters and calls the #get method on on the request.
|
18
|
+
# Then calls Parliament::Response#filter with the filters as the parameters on the resulting response.
|
19
|
+
#
|
20
|
+
# @param [Parliament::Request] request a built Parliament::Request object that can just be called with #get
|
21
|
+
#
|
22
|
+
# @return [Parliament::Response]
|
23
|
+
def self.filter_response_data(request, *filters)
|
24
|
+
response = request.get
|
25
|
+
response.filter(*filters)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Namespace for helper methods to generate a vcard.
|
2
|
+
module Parliament
|
3
|
+
module Utils
|
4
|
+
module Helpers
|
5
|
+
module VCardHelper
|
6
|
+
# Generates a vcard for the given contact point.
|
7
|
+
#
|
8
|
+
# @param [Grom::Node] contact_point a Grom::Node with type: http://id.ukpds.org/schema/ContactPoint.
|
9
|
+
# @return [Vcard::Vcard] a vcard with person, postal address and contact details set.
|
10
|
+
def create_vcard(contact_point)
|
11
|
+
Vcard::Vcard::Maker.make2 do |maker|
|
12
|
+
maker.add_name do |name|
|
13
|
+
person_set(contact_point, name)
|
14
|
+
postal_address_set(contact_point, maker)
|
15
|
+
contacts_set(contact_point, maker)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Sets the contact details of a vcard.
|
21
|
+
#
|
22
|
+
# @param [Grom::Node] contact_point a Grom::Node with type: http://id.ukpds.org/schema/ContactPoint.
|
23
|
+
# @param [Vcard::Vcard::Maker] maker a Vcard builder.
|
24
|
+
# @return [Vcard::Vcard::Maker] a Vcard maker with contact details set.
|
25
|
+
def contacts_set(contact_point, maker)
|
26
|
+
maker.add_email(contact_point.email) unless contact_point.email == ''
|
27
|
+
maker.add_tel(contact_point.phone_number) unless contact_point.phone_number == ''
|
28
|
+
maker.add_tel(contact_point.fax_number) { |f| f.location = 'fax' } unless contact_point.fax_number == ''
|
29
|
+
end
|
30
|
+
|
31
|
+
# Sets the postal address of a vcard.
|
32
|
+
#
|
33
|
+
# @param [Grom::Node] contact_point a Grom::Node with type: http://id.ukpds.org/schema/ContactPoint.
|
34
|
+
# @param [Vcard::Vcard::Maker] maker a Vcard builder.
|
35
|
+
# @return [Vcard::Vcard::Maker, Boolean] a Vcard maker with postal address set or false if the contact point has no postal addresses.
|
36
|
+
def postal_address_set(contact_point, maker)
|
37
|
+
return false if contact_point.postal_addresses.empty?
|
38
|
+
address = contact_point.postal_addresses.first.full_address
|
39
|
+
maker.add_addr do |addr|
|
40
|
+
addr.street = address unless address == ''
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Sets the person details of a vcard.
|
45
|
+
#
|
46
|
+
# @param [Grom::Node] contact_point a Grom::Node with type: http://id.ukpds.org/schema/ContactPoint.
|
47
|
+
# @param [Object] name a Vcard::Name.
|
48
|
+
# @return [Vcard::Vcard::Maker, Boolean] a Vcard maker with person details set or false if the contact point is not associated with a person.
|
49
|
+
def person_set(contact_point, name)
|
50
|
+
return false if contact_point.incumbency.nil? || contact_point.incumbency.member.nil?
|
51
|
+
name.given = contact_point.incumbency.member.given_name
|
52
|
+
name.family = contact_point.incumbency.member.family_name
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative './helpers/application_helper'
|
2
|
+
require_relative './helpers/flag_helper'
|
3
|
+
require_relative './helpers/format_helper'
|
4
|
+
require_relative './helpers/houses_helper'
|
5
|
+
require_relative './helpers/parliament_helper'
|
6
|
+
require_relative './helpers/postcode_helper'
|
7
|
+
require_relative './helpers/request_helper'
|
8
|
+
require_relative './helpers/v_card_helper'
|
9
|
+
|
10
|
+
require 'parliament/grom/decorator'
|
11
|
+
|
12
|
+
module Parliament
|
13
|
+
module Utils
|
14
|
+
module Helpers
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative './helpers/application_helper'
|
2
|
+
|
3
|
+
module Parliament
|
4
|
+
module Utils
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer 'parliament-utils.view_helpers' do
|
7
|
+
require 'parliament/utils/config/initializers'
|
8
|
+
ActionView::Base.send(:include, Helpers::ApplicationHelper)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bandiera/client'
|
2
|
+
require 'pugin/bandiera'
|
3
|
+
|
4
|
+
module Parliament
|
5
|
+
module Utils
|
6
|
+
module TestHelpers
|
7
|
+
module BandieraHelper
|
8
|
+
def self.load_rspec_config(config)
|
9
|
+
#Stubs Bandiera::Client methods enabled? and get_features_for_group to clean up logs
|
10
|
+
#and streamline cassettes
|
11
|
+
config.before(:each) do
|
12
|
+
allow(BANDIERA_CLIENT).to receive(:enabled?).and_return(false)
|
13
|
+
allow(Pugin::PuginBandieraClient::BANDIERA_CLIENT).to receive(:get_features_for_group).and_return({})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module TestHelpers
|
4
|
+
module RailsHelper
|
5
|
+
def self.load_rspec_config(config)
|
6
|
+
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
|
7
|
+
# # RSpec Rails can automatically mix in different behaviours to your tests
|
8
|
+
# # based on their file location, for example enabling you to call `get` and
|
9
|
+
# # `post` in specs under `spec/controllers`.
|
10
|
+
# #
|
11
|
+
# # You can disable this behaviour by removing the line below, and instead
|
12
|
+
# # explicitly tag your specs with their type, e.g.:
|
13
|
+
# #
|
14
|
+
# # RSpec.describe UsersController, :type => :controller do
|
15
|
+
# # # ...
|
16
|
+
# # end
|
17
|
+
# #
|
18
|
+
# # The different available types are documented in the features, such as in
|
19
|
+
# # https://relishapp.com/rspec/rspec-rails/docs
|
20
|
+
config.infer_spec_type_from_file_location!
|
21
|
+
#
|
22
|
+
# # Filter lines from Rails gems in backtraces.
|
23
|
+
config.filter_rails_from_backtrace!
|
24
|
+
# # arbitrary gems may also be filtered via:
|
25
|
+
# # config.filter_gems_from_backtrace("gem name")
|
26
|
+
config.include Parliament::Utils::Helpers::ApplicationHelper
|
27
|
+
#
|
28
|
+
# # Set Parliament::Utils::Helpers::HousesHelper#set_ids instance variables to nil after each spec.
|
29
|
+
# # Calling certain Parliament::Utils::Helpers::HousesHelper methods (e.g. Parliament::Utils::Helpers::HousesHelper#commons?) causes
|
30
|
+
# # Parliament::Utils::Helpers::HousesHelper#set_ids to be called which sets @commons_id and @lords_id.
|
31
|
+
# # Setting these to nil causes each spec that requires them to make another
|
32
|
+
# # Parliament::Utils::Helpers::ParliamentHelper request and generate a VCR cassette and stops any RSpec
|
33
|
+
# # ordering issues where they may or may not have been set by the previous spec.
|
34
|
+
config.after(:each) do
|
35
|
+
Parliament::Utils::Helpers::HousesHelper.instance_variable_set(:@commons_id, nil)
|
36
|
+
Parliament::Utils::Helpers::HousesHelper.instance_variable_set(:@lords_id, nil)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module TestHelpers
|
4
|
+
module RSpecHelper
|
5
|
+
|
6
|
+
def self.load_rspec_config(config)
|
7
|
+
# RSpec.configure do |config|
|
8
|
+
# rspec-expectations config goes here. You can use an alternate
|
9
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
10
|
+
# assertions if you prefer.
|
11
|
+
config.expect_with :rspec do |expectations|
|
12
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
13
|
+
# and `failure_message` of custom matchers include text for helper methods
|
14
|
+
# defined using `chain`, e.g.:
|
15
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
16
|
+
# # => "be bigger than 2 and smaller than 4"
|
17
|
+
# ...rather than:
|
18
|
+
# # => "be bigger than 2"
|
19
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
20
|
+
end
|
21
|
+
|
22
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
23
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
24
|
+
config.mock_with :rspec do |mocks|
|
25
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
26
|
+
# a real object. This is generally recommended, and will default to
|
27
|
+
# `true` in RSpec 4.
|
28
|
+
mocks.verify_partial_doubles = true
|
29
|
+
end
|
30
|
+
|
31
|
+
# These two settings work together to allow you to limit a spec run
|
32
|
+
# to individual examples or groups you care about by tagging them with
|
33
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
34
|
+
# get run.
|
35
|
+
config.filter_run :focus
|
36
|
+
config.run_all_when_everything_filtered = true
|
37
|
+
|
38
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
39
|
+
config.disable_monkey_patching!
|
40
|
+
|
41
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
42
|
+
# file, and it's useful to allow more verbose output when running an
|
43
|
+
# individual spec file.
|
44
|
+
if config.files_to_run.one?
|
45
|
+
# Use the documentation formatter for detailed output,
|
46
|
+
# unless a formatter has already been configured
|
47
|
+
# (e.g. via a command-line flag).
|
48
|
+
config.default_formatter = 'doc'
|
49
|
+
end
|
50
|
+
|
51
|
+
# Print the 10 slowest examples and example groups at the
|
52
|
+
# end of the spec run, to help surface which specs are running
|
53
|
+
# particularly slow.
|
54
|
+
config.profile_examples = 10
|
55
|
+
|
56
|
+
config.expect_with :rspec do |c|
|
57
|
+
c.syntax = :expect
|
58
|
+
end
|
59
|
+
|
60
|
+
# Run specs in random order to surface order dependencies. If you find an
|
61
|
+
# order dependency and want to debug it, you can fix the order by providing
|
62
|
+
# the seed, which is printed after each run.
|
63
|
+
# --seed 1234
|
64
|
+
config.order = :random
|
65
|
+
|
66
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
67
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
68
|
+
# test failures related to randomization by passing the same `--seed` value
|
69
|
+
# as the one that triggered the failure.
|
70
|
+
Kernel.srand config.seed
|
71
|
+
# end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module TestHelpers
|
4
|
+
module SimpleCovHelper
|
5
|
+
|
6
|
+
require 'coveralls'
|
7
|
+
require 'simplecov'
|
8
|
+
|
9
|
+
def self.load_rspec_config(config)
|
10
|
+
|
11
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
12
|
+
Coveralls::SimpleCov::Formatter,
|
13
|
+
SimpleCov::Formatter::HTMLFormatter
|
14
|
+
])
|
15
|
+
SimpleCov.start
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Parliament
|
2
|
+
module Utils
|
3
|
+
module TestHelpers
|
4
|
+
module VCRHelper
|
5
|
+
require 'vcr'
|
6
|
+
|
7
|
+
def self.load_rspec_config(config)
|
8
|
+
|
9
|
+
VCR.configure do |config|
|
10
|
+
config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
|
11
|
+
config.hook_into :webmock
|
12
|
+
config.configure_rspec_metadata!
|
13
|
+
|
14
|
+
config.default_cassette_options = {
|
15
|
+
# record: :new_episodes
|
16
|
+
record: :once
|
17
|
+
}
|
18
|
+
|
19
|
+
# Create a simple matcher which will 'filter' any request URIs on the fly
|
20
|
+
config.register_request_matcher :filtered_uri do |request_1, request_2|
|
21
|
+
parliament_match = request_1.uri.sub(ENV['PARLIAMENT_BASE_URL'], 'http://localhost:3030') == request_2.uri.sub(ENV['PARLIAMENT_BASE_URL'], 'http://localhost:3030')
|
22
|
+
bandiera_match = request_1.uri.sub(ENV['BANDIERA_URL'], 'http://localhost:5000') == request_2.uri.sub(ENV['BANDIERA_URL'], 'http://localhost:5000')
|
23
|
+
|
24
|
+
parliament_match || bandiera_match
|
25
|
+
end
|
26
|
+
|
27
|
+
config.default_cassette_options = { match_requests_on: [:method, :filtered_uri] }
|
28
|
+
|
29
|
+
# Dynamically filter our sensitive information
|
30
|
+
config.filter_sensitive_data('<AUTH_TOKEN>') { ENV['PARLIAMENT_AUTH_TOKEN'] }
|
31
|
+
config.filter_sensitive_data('http://localhost:3030') { ENV['PARLIAMENT_BASE_URL'] }
|
32
|
+
config.filter_sensitive_data('http://localhost:5000') { ENV['BANDIERA_URL'] }
|
33
|
+
|
34
|
+
# Dynamically filter n-triple data
|
35
|
+
config.before_record do |interaction|
|
36
|
+
should_ignore = ['_:node', '^^<http://www.w3.org/2001/XMLSchema#date>', '^^<http://www.w3.org/2001/XMLSchema#integer>']
|
37
|
+
|
38
|
+
# Check if content type header exists and if it includes application/n-triples
|
39
|
+
if interaction.response.headers['Content-Type'] && interaction.response.headers['Content-Type'].include?('application/n-triples')
|
40
|
+
# Split our data by line
|
41
|
+
lines = interaction.response.body.split("\n")
|
42
|
+
|
43
|
+
# How many times have we seen a predicate?
|
44
|
+
predicate_occurrances = Hash.new(1)
|
45
|
+
|
46
|
+
# Iterate over each line, decide if we need to filter it.
|
47
|
+
lines.each do |line|
|
48
|
+
next if should_ignore.any? { |condition| line.include?(condition) }
|
49
|
+
next unless line.include?('"')
|
50
|
+
|
51
|
+
# require 'pry'; binding.pry
|
52
|
+
# Split on '> <' to get a Subject and Predicate+Object split
|
53
|
+
subject, predicate_and_object = line.split('> <')
|
54
|
+
|
55
|
+
# Get the actual object
|
56
|
+
predicate, object = predicate_and_object.split('> "')
|
57
|
+
|
58
|
+
# Get the last part of a predicate URI
|
59
|
+
predicate_type = predicate.split('/').last
|
60
|
+
|
61
|
+
# Get the number of times we've seen this predicate
|
62
|
+
occurrance = predicate_occurrances[predicate_type]
|
63
|
+
predicate_occurrances[predicate_type] = predicate_occurrances[predicate_type] + 1
|
64
|
+
|
65
|
+
# Try and build a new object value based on the predicate
|
66
|
+
new_object = "#{predicate_type} - #{occurrance}\""
|
67
|
+
|
68
|
+
# Replace the object value
|
69
|
+
index = object.index('"')
|
70
|
+
|
71
|
+
object[0..index] = new_object if index
|
72
|
+
|
73
|
+
new_line = "#{subject}> <#{predicate}> \"#{object}"
|
74
|
+
config.filter_sensitive_data(new_line) { line }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative './test_helpers/rails_helper' if defined?(::Rails)
|
2
|
+
require_relative './test_helpers/rspec_helper'
|
3
|
+
require_relative './test_helpers/simplecov_helper'
|
4
|
+
require_relative './test_helpers/vcr_helper'
|
5
|
+
require_relative './test_helpers/webmock_helper'
|
6
|
+
require_relative './test_helpers/bandiera_helper'
|
7
|
+
|
8
|
+
module Parliament
|
9
|
+
module Utils
|
10
|
+
module TestHelpers
|
11
|
+
include Parliament::Utils::TestHelpers::RailsHelper if defined?(::Rails)
|
12
|
+
include Parliament::Utils::TestHelpers::RSpecHelper
|
13
|
+
include Parliament::Utils::TestHelpers::SimpleCovHelper
|
14
|
+
include Parliament::Utils::TestHelpers::VCRHelper
|
15
|
+
include Parliament::Utils::TestHelpers::WebmockHelper
|
16
|
+
include Parliament::Utils::TestHelpers::BandieraHelper
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'parliament/utils/version'
|
2
|
+
require 'parliament/utils/helpers'
|
3
|
+
require 'parliament/utils/config/initializers/bandiera'
|
4
|
+
require 'parliament/utils/test_helpers'
|
5
|
+
require 'parliament/utils/railtie' if defined?(Rails)
|
6
|
+
|
7
|
+
module Parliament
|
8
|
+
module Utils
|
9
|
+
end
|
10
|
+
end
|
data/log/test.log
ADDED
File without changes
|