parliament-utils 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64ce1d55fb093dc97a517a40072fb71ba279c4bb
4
- data.tar.gz: c629dabd75388bc880432c2d361f64097460ffff
3
+ metadata.gz: 871a37fca2da71b9bfc844d0f0bfe43d4aadd852
4
+ data.tar.gz: 66d8d17514fef3ad370fd90a1008b0e6f1dcc63a
5
5
  SHA512:
6
- metadata.gz: ead9e70107217a9d1adeec2228335369ea410337edcd699c77fcc90add5e7e592765b21f4e11e044884d62639a7789855698713fc2f438d39b019910cc1a87a7
7
- data.tar.gz: 531a3c67da969c45518b89b3c004daaa79229282af8381bfe267f19c5ab18af2e1264888394e07cbdf0cc8a25ea91bad55af8ac65841acff8c12e6ea2e379e27
6
+ metadata.gz: a58fc72377b9b4fb8988c549c3cf3f716b14e00b47d9a356c0e72d677587ca888b2dd4bf324237335ced4926a80e18ed691abe36a49cdf0a7876bb31a3bb6bef
7
+ data.tar.gz: d9cfa7367b95145edf62f4ea9048075a333b11c74ae52fa245b3374b6f1b9f6b8bf27131a6815e55fa138b5bcbfab7766f4be5e2ae2030c0d0ed35632a04fa5c
@@ -3,12 +3,9 @@
3
3
  # Add new mime types for use in respond_to blocks:
4
4
  # Mime::Type.register "text/richtext", :rtf
5
5
 
6
- Mime::Type.register 'application/n-triples', :nt
7
- Mime::Type.register 'text/turtle', :ttl
8
- Mime::Type.register 'application/sparql-results+xml', :srx
9
- Mime::Type.register 'application/sparql-results+json', :srj
10
- Mime::Type.register 'text/tab-separated-values', :tsv
11
- Mime::Type.register 'text/vnd.graphviz', :gv, [], [:dot]
12
- Mime::Type.register 'application/rdf+xml', :rdf, [], [:rdfxml]
13
- Mime::Type.register 'application/rdf+ld', :jsonld
6
+ Parliament::Utils::Helpers::ApplicationHelper::API_MIME_TYPE_CONFIG.each do |mime_type|
7
+ primary = mime_type.shift
8
+ alternatives = mime_type
14
9
 
10
+ Mime::Type.register primary[1], primary[0], alternatives.values, alternatives.keys
11
+ end
@@ -2,6 +2,54 @@ module Parliament
2
2
  module Utils
3
3
  module Helpers
4
4
  module ApplicationHelper
5
+ # What MIME types does the API accept?
6
+ #
7
+ # Note: All of the below are used to generate MIME types that our application will answer to, but NOT the
8
+ # alternatives shown in our header. See ALTERNATIVE_MIME_TYPE_CONFIG.
9
+ API_MIME_TYPE_CONFIG = [
10
+ {
11
+ nt: 'application/n-triples'
12
+ },
13
+ {
14
+ ttl: 'text/turtle'
15
+ },
16
+ {
17
+ tsv: 'text/tab-separated-values'
18
+ },
19
+ {
20
+ csv: 'text/csv'
21
+ },
22
+ {
23
+ rj: 'application/json+rdf'
24
+ },
25
+ {
26
+ jsonld: 'application/json+ld',
27
+ json: 'application/json'
28
+ },
29
+ {
30
+ rdfxml: 'application/rdf+xml',
31
+ rdf: 'application/xml',
32
+ xml: 'text/xml'
33
+ }
34
+ ].freeze
35
+
36
+ # Use the above, minus the last two entries (json & xml), to build an alternative URL list.
37
+ # Then re-create JSON and XML with the correct alternatives
38
+ ALTERNATIVE_MIME_TYPE_CONFIG = API_MIME_TYPE_CONFIG.take(API_MIME_TYPE_CONFIG.size-2).concat(
39
+ [
40
+ {
41
+ json: 'application/json+ld'
42
+ },
43
+ {
44
+ xml: 'application/rdf+xml'
45
+ }
46
+ ]
47
+ )
48
+
49
+ API_MIME_TYPES = Parliament::Utils::Helpers::ApplicationHelper::API_MIME_TYPE_CONFIG.map { |mime_type| mime_type.values }.flatten.freeze
50
+ API_FILE_EXTENSIONS = Parliament::Utils::Helpers::ApplicationHelper::API_MIME_TYPE_CONFIG.map { |mime_type| mime_type.keys }.flatten.freeze
51
+ ALTERNATIVE_MIME_TYPES_FLATTENED = Parliament::Utils::Helpers::ApplicationHelper::ALTERNATIVE_MIME_TYPE_CONFIG.reduce(:merge)
52
+
5
53
  # Sets the title for a page.
6
54
  #
7
55
  # @param [String] page_title the title of the page.
@@ -15,7 +63,7 @@ module Parliament
15
63
  def data_check
16
64
  # Check format to see if it is available from the data API
17
65
  # We DO NOT offer data formats for constituency maps
18
- return if !Parliament::Utils::Helpers::FormatHelper::DATA_FORMATS.include?(request.formats.first) || (params[:controller] == 'constituencies' && params[:action] == 'map')
66
+ return if !API_MIME_TYPES.include?(request.formats.first) || (params[:controller] == 'constituencies' && params[:action] == 'map')
19
67
 
20
68
  # Find the current controller/action's API url
21
69
  @data_url = data_url
@@ -58,12 +106,10 @@ module Parliament
58
106
  def populate_alternates(url)
59
107
  alternates = []
60
108
 
61
- Parliament::Utils::Helpers::FormatHelper::DATA_FORMATS.each do |format|
109
+ ALTERNATIVE_MIME_TYPES_FLATTENED.each do |extension, format| # (key, value)
62
110
  uri = URI.parse(url)
111
+ uri.path = "#{uri.path}.#{extension}"
63
112
 
64
- uri_form = URI.decode_www_form(String(uri.query)) << ['format',format]
65
-
66
- uri.query = URI.encode_www_form(uri_form)
67
113
  alternates << { type: format, href: uri.to_s }
68
114
  end
69
115
 
@@ -1,6 +1,5 @@
1
1
  require_relative './helpers/application_helper'
2
2
  require_relative './helpers/flag_helper'
3
- require_relative './helpers/format_helper'
4
3
  require_relative './helpers/houses_helper'
5
4
  require_relative './helpers/parliament_helper'
6
5
  require_relative './helpers/postcode_helper'
@@ -1,5 +1,5 @@
1
1
  module Parliament
2
2
  module Utils
3
- VERSION = '0.7.0'.freeze
3
+ VERSION = '0.7.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebecca Appleyard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-02 00:00:00.000000000 Z
11
+ date: 2018-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parliament-ruby
@@ -292,7 +292,6 @@ files:
292
292
  - lib/parliament/utils/helpers/application_helper.rb
293
293
  - lib/parliament/utils/helpers/filter_helper.rb
294
294
  - lib/parliament/utils/helpers/flag_helper.rb
295
- - lib/parliament/utils/helpers/format_helper.rb
296
295
  - lib/parliament/utils/helpers/houses_helper.rb
297
296
  - lib/parliament/utils/helpers/parliament_helper.rb
298
297
  - lib/parliament/utils/helpers/postcode_helper.rb
@@ -1,21 +0,0 @@
1
- module Parliament
2
- module Utils
3
- module Helpers
4
- module FormatHelper
5
- DATA_FORMATS = %w(
6
- application/n-triples
7
- text/turtle
8
- application/sparql-results+xml
9
- application/sparql-results+json
10
- text/csv
11
- text/tab-separated-values
12
- text/vnd.graphviz
13
- application/rdf+xml
14
- application/xml
15
- application/json+ld
16
- application/json
17
- ).freeze
18
- end
19
- end
20
- end
21
- end