Skip to content

Commit

Permalink
Generate dates based on underlying precision
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtmtmtm committed Feb 27, 2019
1 parent c9a2589 commit e250a3c
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions lib/cabinet.rb
@@ -1,9 +1,11 @@
# TODO: extending Scraped::Scraper with ability to add Strategies # frozen_string_literal: true

# TODO: extend Scraped::Scraper with ability to add Strategies
class Scraped::Request::Strategy::LiveRequest class Scraped::Request::Strategy::LiveRequest
require 'rest-client' require 'rest-client'


def url def url
SPARQL_URL % CGI.escape(QUERY % @url) SPARQL_URL % CGI.escape(QUERY % [@url, @url])
end end


private private
Expand All @@ -18,16 +20,23 @@ def sparql(query)
SPARQL_URL = 'https://query.wikidata.org/sparql?format=json&query=%s' SPARQL_URL = 'https://query.wikidata.org/sparql?format=json&query=%s'


QUERY = <<~SPARQL QUERY = <<~SPARQL
SELECT DISTINCT ?ps ?item ?itemLabel ?minister ?ministerLabel ?ordinal ?start ?end ?cabinet ?cabinetLabel SELECT DISTINCT ?ps ?item ?itemLabel ?minister ?ministerLabel ?ordinal ?start ?startprecision ?end ?endprecision ?cabinet ?cabinetLabel {
WHERE { {
?item p:P39/ps:P39/wdt:P279* wd:%s . SELECT DISTINCT ?ps ?item ?minister ?ordinal ?start ?startprecision ?end ?endprecision ?cabinet {
?item p:P39 ?ps . ?item p:P39/ps:P39 wd:%s .
?ps ps:P39 ?minister . ?item p:P39 ?ps .
?minister wdt:P279* wd:Q83307 . ?ps ps:P39 ?minister .
OPTIONAL { ?ps pq:P1545 ?ordinal } ?minister wdt:P279* wd:Q83307 .
OPTIONAL { ?ps pq:P580 ?start } OPTIONAL { ?ps pq:P1545 ?ordinal }
OPTIONAL { ?ps pq:P582 ?end } OPTIONAL { ?ps pqv:P580 [wikibase:timeValue ?start ; wikibase:timePrecision ?startprecision ] }
OPTIONAL { ?ps pq:P5054 ?cabinet } OPTIONAL { ?ps pqv:P582 [wikibase:timeValue ?end ; wikibase:timePrecision ?endprecision ] }
# Ignore anything with a different jurisdiction
OPTIONAL { wd:%s wdt:P1001 ?legislative_jurisdiction }
OPTIONAL { ?minister wdt:P1001 ?executive_jurisdiction }
FILTER (!BOUND(?legislative_jurisdiction) || !BOUND(?executive_jurisdiction) || (?legislative_jurisdiction = ?executive_jurisdiction))
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} }
SPARQL SPARQL
Expand All @@ -38,6 +47,35 @@ class CabinetScraper < Scraped::JSON
json[:results][:bindings].map { |result| fragment(result => Membership).to_h } json[:results][:bindings].map { |result| fragment(result => Membership).to_h }
end end


class Wikidate
def initialize(date, precision)
@date = date
@precision = precision
end

# not to_s, as this can return 'nil'
def as_string
return unless date && precision
return unless slice_point

date.slice(0, slice_point)
end

private

attr_reader :date, :precision

PRECISION_LENGTH = {
'9' => 4, # year
'10' => 7, # month
'11' => 10, # day
}.freeze

def slice_point
PRECISION_LENGTH[precision]
end
end

class Membership < Scraped::JSON class Membership < Scraped::JSON
field :id do field :id do
json.dig(:item, :value).to_s.split('/').last json.dig(:item, :value).to_s.split('/').last
Expand All @@ -60,11 +98,11 @@ class Membership < Scraped::JSON
end end


field :start_date do field :start_date do
json.dig(:start, :value).to_s[0..9] Wikidate.new(json.dig(:start, :value), json.dig(:startprecision, :value)).as_string
end end


field :end_date do field :end_date do
json.dig(:end, :value).to_s[0..9] Wikidate.new(json.dig(:end, :value), json.dig(:endprecision, :value)).as_string
end end


field :ordinal do field :ordinal do
Expand Down

0 comments on commit e250a3c

Please sign in to comment.