-
Notifications
You must be signed in to change notification settings - Fork 30
WIP GobiertoData add dcat catalog endpoint #3846
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
e4201cf
4efaad6
57d273c
55d3919
8e23033
034e24f
dd8abc9
c3b7307
dcd63d8
7a689c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| module GobiertoData | ||
| class DatasetPresenter | ||
| include ActionView::Helpers::UrlHelper | ||
|
|
||
| attr_reader :site | ||
|
|
||
| def initialize(site) | ||
| @site = site | ||
| end | ||
|
|
||
| def build_catalog | ||
| catalog = { | ||
| identifier_uri: url_helpers.gobierto_data_root_url(host: site.domain), | ||
| title: "dcat catalog for #{site}", | ||
| description: "this is catalog published by #{@catalog_identifier_uri} which contains datasets", | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| issued: site.created_at, | ||
| modified: GobiertoData::Dataset.where(site_id: site.id).maximum(:created_at) || site.created_at, | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| languages: site["configuration_data"]["available_locales"], | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| homepage: url_helpers.gobierto_data_root_url(host: site.domain), | ||
| license_url: "https://opendatacommons.org/licenses/odbl/", | ||
|
||
| datasets: build_datasets_for_catalog | ||
| } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_datasets_for_catalog | ||
| datasets = [] | ||
| Dataset.where(site_id: site.id).visibles.each do |dataset| | ||
| datasets << build_dataset_for_catalog(dataset) | ||
| end | ||
| datasets | ||
stbnrivas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def build_dataset_for_catalog(dataset) | ||
| { | ||
| url: url_helpers.gobierto_data_datasets_url(host: site.domain, id: dataset.slug), | ||
| title: dataset.name, | ||
| description: description_custom_field_record(dataset), | ||
| keywords: [], | ||
| issued: dataset.created_at, | ||
| modified: dataset.updated_at, | ||
| languages: [site_locale], | ||
| license_url: "https://opendatacommons.org/licenses/odbl/", | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| publisher: site.name, | ||
| publisher_mbox: site.reply_to_email, | ||
| distributions: build_distribution_for_catalog(dataset) | ||
| } | ||
| end | ||
|
|
||
| def build_distribution_for_catalog(dataset) | ||
| [ | ||
| { | ||
| format: 'application/csv', | ||
| download_url: url_helpers.download_gobierto_data_api_v1_dataset_url(dataset, host: site.domain) | ||
| } | ||
| ] | ||
| end | ||
|
|
||
| def url_helpers | ||
| Rails.application.routes.url_helpers | ||
| end | ||
|
|
||
| def site_locale | ||
| site.configuration_data["default_locale"] | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def description_custom_field_record(dataset) | ||
| if dataset.custom_field_record_with_uid("description") | ||
| dataset.custom_field_record_with_uid("description").payload["description"][site_locale] | ||
| else | ||
| "" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <rdf:RDF | ||
| xmlns:time="http://www.w3.org/2006/time#" | ||
| xmlns:dct="http://purl.org/dc/terms/" | ||
| xmlns:dcat="http://www.w3.org/ns/dcat#" | ||
| xmlns:foaf="http://xmlns.com/foaf/0.1/" | ||
| xmlns:xsd="http://www.w3.org/2001/XMLSchema#" | ||
| xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" | ||
| xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><% cache @catalog do %> | ||
| <dcat:Catalog rdf:about="https://data.some.org/catalog"> | ||
| <dct:identifier><%= @catalog[:identifier_uri] %></dct:identifier> | ||
| <dct:title><%= @catalog[:title] %></dct:title> | ||
| <dct:description><%= @catalog[:description] %></dct:description> | ||
| <dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"><%= @catalog[:issued] %></dct:issued> | ||
| <dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"><%= @catalog[:modified] %></dct:modified> | ||
| <%= @catalog[:languages].map { |locale| "<dct:language>#{locale}</dct:language>" }.join.html_safe %> | ||
| <foaf:homepage rdf:resource="<%= @catalog[:homepage] %>"/> | ||
| <dct:license rdf:resource="<%= @catalog[:license_url] %>"/> | ||
| <% @catalog[:datasets].each do |dataset| %><dcat:dataset> | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <dcat:Dataset rdf:about="<%= dataset[:url]%>"> | ||
| <dct:identifier><%= dataset[:url] %></dct:identifier> | ||
| <dct:title><%= dataset[:title] %></dct:title> | ||
| <dct:description><%= dataset[:description] %></dct:description> | ||
| <%= dataset[:keywords].map { |keyword| "<dct:keyword>#{locale}</dct:keyword>" }.join.html_safe %> | ||
| <dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"><%= dataset[:issued] %></dct:issued> | ||
| <dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"><%= dataset[:modified] %></dct:modified> | ||
| <%= dataset[:languages].map { |lang| "<dct:language>#{lang}</dct:language>" }.join.html_safe %> | ||
| <dct:license rdf:resource="<%= dataset[:title] %>"/> | ||
| <dct:publisher> | ||
| <foaf:Organization> | ||
| <foaf:name><%= dataset[:publisher] %></foaf:name> | ||
| <foaf:mbox><%= dataset[:publisher_mbox] %></foaf:mbox> | ||
| </foaf:Organization> | ||
| </dct:publisher> | ||
| <% dataset[:distributions].each do |distribution| %> | ||
| <dcat:distribution> | ||
| <dcat:Distribution> | ||
| <dct:identifier><%= @identifier_uri %></dct:identifier> | ||
| <dct:title><%= dataset[:title] %> in CSV format</dct:title> | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <dct:description><%= dataset[:description] %></dct:description> | ||
| <dcat:downloadURL rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI"><%= distribution[:download_url] %></dcat:downloadURL> | ||
| <dcat:mediaType><%= distribution[:format] %></dcat:mediaType> | ||
| <dct:license rdf:resource="<%= dataset[:license_url] %>"/> | ||
| </dcat:Distribution> | ||
| </dcat:distribution> | ||
| <% end %> | ||
| </dcat:Dataset> | ||
| </dcat:dataset><% end %> | ||
ferblape marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </dcat:Catalog><% end %> | ||
| </rdf:RDF> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| module GobiertoData | ||
| class DatasetPresenterTest < ActiveSupport::TestCase | ||
|
|
||
| def setup | ||
| super | ||
| @site = sites(:madrid) | ||
| @subject = DatasetPresenter.new(@site) | ||
| @other_site = sites(:huesca) | ||
| @other_subject = DatasetPresenter.new(@other_site) | ||
| end | ||
|
|
||
| def test_structure_catalog_building_do_not_show_draft_datasets | ||
| catalog = @subject.build_catalog | ||
| datasets_published = GobiertoData::Dataset.by_site(@site.id).visibles.size | ||
|
||
| assert_equal datasets_published, catalog[:datasets].size | ||
| end | ||
|
|
||
| def test_structure_catalog_building_using_site_with_datasets | ||
| catalog = @subject.build_catalog | ||
| assert catalog.has_key? :identifier_uri | ||
| assert catalog.has_key? :title | ||
| assert catalog.has_key? :description | ||
| assert catalog.has_key? :issued | ||
| assert catalog.has_key? :modified | ||
| assert catalog.has_key? :languages | ||
| assert catalog.has_key? :homepage | ||
| assert catalog.has_key? :license_url | ||
| assert catalog.has_key? :datasets | ||
| catalog[:datasets].each do |dataset| | ||
| assert dataset.has_key? :url | ||
| assert dataset.has_key? :title | ||
| assert dataset.has_key? :description | ||
| assert dataset.has_key? :keywords | ||
| assert dataset.has_key? :issued | ||
| assert dataset.has_key? :modified | ||
| assert dataset.has_key? :languages | ||
| assert dataset.has_key? :license_url | ||
| assert dataset.has_key? :publisher | ||
| assert dataset.has_key? :publisher_mbox | ||
| assert dataset.has_key? :distributions | ||
| dataset[:distributions].each do |distribution| | ||
| assert distribution.has_key? :format | ||
| assert distribution.has_key? :download_url | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_structure_catalog_building_using_site_without_datasets | ||
| catalog = @other_subject.build_catalog | ||
| assert_equal 0, catalog[:datasets].size | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.