wikidatum 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -2
- data/README.md +24 -2
- data/bin/console +0 -4
- data/lib/wikidatum/client.rb +395 -327
- data/lib/wikidatum/data_type/base.rb +61 -0
- data/lib/wikidatum/{data_value_type → data_type}/globe_coordinate.rb +21 -20
- data/lib/wikidatum/{data_value_type → data_type}/monolingual_text.rb +19 -18
- data/lib/wikidatum/{data_value_type → data_type}/no_value.rb +3 -11
- data/lib/wikidatum/data_type/quantity.rb +75 -0
- data/lib/wikidatum/{data_value_type → data_type}/some_value.rb +3 -11
- data/lib/wikidatum/{data_value_type → data_type}/time.rb +20 -37
- data/lib/wikidatum/data_type/wikibase_item.rb +64 -0
- data/lib/wikidatum/{data_value_type → data_type}/wikibase_string.rb +16 -15
- data/lib/wikidatum/data_type.rb +11 -0
- data/lib/wikidatum/item.rb +2 -2
- data/lib/wikidatum/qualifier.rb +52 -4
- data/lib/wikidatum/reference.rb +9 -9
- data/lib/wikidatum/reference_part.rb +56 -0
- data/lib/wikidatum/statement.rb +21 -11
- data/lib/wikidatum/utils.rb +25 -0
- data/lib/wikidatum/version.rb +1 -1
- data/lib/wikidatum.rb +31 -20
- data/wikidatum.gemspec +1 -1
- metadata +15 -17
- data/Gemfile +0 -13
- data/Gemfile.lock +0 -73
- data/Rakefile +0 -16
- data/lib/wikidatum/data_value_type/base.rb +0 -61
- data/lib/wikidatum/data_value_type/quantity.rb +0 -92
- data/lib/wikidatum/data_value_type/wikibase_entity_id.rb +0 -83
- data/lib/wikidatum/data_value_type.rb +0 -11
- data/lib/wikidatum/snak.rb +0 -79
data/lib/wikidatum/reference.rb
CHANGED
@@ -4,28 +4,28 @@ class Wikidatum::Reference
|
|
4
4
|
# @return [String] Hash of the reference (a cryptographic hash, not a Ruby hash).
|
5
5
|
attr_reader :hash
|
6
6
|
|
7
|
-
# @return [Array<Wikidatum::
|
8
|
-
attr_reader :
|
7
|
+
# @return [Array<Wikidatum::ReferencePart>]
|
8
|
+
attr_reader :parts
|
9
9
|
|
10
10
|
# @!visibility private
|
11
11
|
# @param hash [String] Hash of the reference (a cryptographic hash, not a Ruby hash).
|
12
|
-
# @param
|
13
|
-
def initialize(hash:,
|
12
|
+
# @param parts [Array<Wikidatum::ReferencePart>]
|
13
|
+
def initialize(hash:, parts:)
|
14
14
|
@hash = hash
|
15
|
-
@
|
15
|
+
@parts = parts
|
16
16
|
end
|
17
17
|
|
18
18
|
# @return [Hash]
|
19
19
|
def to_h
|
20
20
|
{
|
21
21
|
hash: @hash,
|
22
|
-
|
22
|
+
parts: @parts.map(&:to_h)
|
23
23
|
}
|
24
24
|
end
|
25
25
|
|
26
26
|
# @return [String]
|
27
27
|
def inspect
|
28
|
-
"<Wikidatum::Reference hash=#{@hash.inspect}
|
28
|
+
"<Wikidatum::Reference hash=#{@hash.inspect} parts=#{@parts.inspect}>"
|
29
29
|
end
|
30
30
|
|
31
31
|
# @!visibility private
|
@@ -37,11 +37,11 @@ class Wikidatum::Reference
|
|
37
37
|
# @param ref_json [Hash]
|
38
38
|
# @return [Wikidatum::Reference]
|
39
39
|
def self.marshal_load(ref_json)
|
40
|
-
|
40
|
+
parts = ref_json['parts'].map { |part| Wikidatum::ReferencePart.marshal_load(part) }
|
41
41
|
|
42
42
|
Wikidatum::Reference.new(
|
43
43
|
hash: ref_json['hash'],
|
44
|
-
|
44
|
+
parts: parts
|
45
45
|
)
|
46
46
|
end
|
47
47
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Wikidatum::ReferencePart
|
4
|
+
# @return [String] ID of the property used, in the format "P123".
|
5
|
+
attr_reader :property_id
|
6
|
+
|
7
|
+
# @return [String]
|
8
|
+
attr_reader :data_type
|
9
|
+
|
10
|
+
# For more information on the possible types that can be returned by
|
11
|
+
# datavalues, see the official documentation:
|
12
|
+
# https://doc.wikimedia.org/Wikibase/master/php/md_docs_topics_json.html#json_datavalues
|
13
|
+
#
|
14
|
+
# @return [Wikidatum::DataType::Base] the value of the statement, can take various forms
|
15
|
+
attr_reader :value
|
16
|
+
|
17
|
+
# @!visibility private
|
18
|
+
# @param property_id [String] ID of the property used, in the format "P123".
|
19
|
+
# @param data_type [String]
|
20
|
+
# @param value [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::WikibaseString, DataType::Time, DataType::WikibaseItem, DataType::NoValue, DataType::SomeValue]
|
21
|
+
def initialize(property_id:, data_type:, value:)
|
22
|
+
@property_id = property_id
|
23
|
+
@data_type = data_type
|
24
|
+
@value = value
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Hash]
|
28
|
+
def to_h
|
29
|
+
{
|
30
|
+
property_id: @property_id,
|
31
|
+
data_type: @data_type,
|
32
|
+
value: @value.to_h
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [String]
|
37
|
+
def inspect
|
38
|
+
"<Wikidatum::ReferencePart property_id=#{@property_id.inspect} data_type=#{@data_type.inspect} value=#{@value.inspect}>"
|
39
|
+
end
|
40
|
+
|
41
|
+
# @!visibility private
|
42
|
+
#
|
43
|
+
# This takes in the JSON blob (as a hash) that is output for a given
|
44
|
+
# reference part in the API and turns it into an actual instance of a
|
45
|
+
# ReferencePart.
|
46
|
+
#
|
47
|
+
# @param part_json [Hash]
|
48
|
+
# @return [Wikidatum::ReferencePart]
|
49
|
+
def self.marshal_load(part_json)
|
50
|
+
Wikidatum::ReferencePart.new(
|
51
|
+
property_id: part_json.dig('property', 'id'),
|
52
|
+
data_type: part_json.dig('property', 'data-type'),
|
53
|
+
value: Wikidatum::Utils.ingest_snak(part_json)
|
54
|
+
)
|
55
|
+
end
|
56
|
+
end
|
data/lib/wikidatum/statement.rb
CHANGED
@@ -7,8 +7,11 @@ class Wikidatum::Statement
|
|
7
7
|
# @return [String] property ID, in the format of 'P123'.
|
8
8
|
attr_accessor :property_id
|
9
9
|
|
10
|
-
# @return [
|
11
|
-
attr_accessor :
|
10
|
+
# @return [String]
|
11
|
+
attr_accessor :data_type
|
12
|
+
|
13
|
+
# @return [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::WikibaseString, DataType::Time, DataType::WikibaseItem, nil]
|
14
|
+
attr_accessor :data_value
|
12
15
|
|
13
16
|
# @return [Array<Wikidatum::Qualifier>]
|
14
17
|
attr_accessor :qualifiers
|
@@ -22,15 +25,17 @@ class Wikidatum::Statement
|
|
22
25
|
|
23
26
|
# @param id [String]
|
24
27
|
# @param property_id [String] The 'P123' ID of the property that this statement represents.
|
25
|
-
# @param
|
28
|
+
# @param data_type [String]
|
29
|
+
# @param data_value [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::WikibaseString, DataType::Time, DataType::WikibaseItem, nil]
|
26
30
|
# @param qualifiers [Array<Wikidatum::Qualifier>]
|
27
31
|
# @param references [Array<Wikidatum::Reference>]
|
28
32
|
# @param rank [String] The rank of the given statement.
|
29
33
|
# Can have the values "preferred", "normal", or "deprecated". Defaults to "normal".
|
30
|
-
def initialize(id:, property_id:,
|
34
|
+
def initialize(id:, property_id:, data_type:, data_value:, qualifiers:, references:, rank: 'normal')
|
31
35
|
@id = id
|
32
36
|
@property_id = property_id
|
33
|
-
@
|
37
|
+
@data_type = data_type
|
38
|
+
@data_value = data_value
|
34
39
|
@qualifiers = qualifiers
|
35
40
|
@references = references
|
36
41
|
@rank = rank
|
@@ -41,7 +46,8 @@ class Wikidatum::Statement
|
|
41
46
|
{
|
42
47
|
id: @id,
|
43
48
|
property_id: @property_id,
|
44
|
-
|
49
|
+
data_type: @data_type,
|
50
|
+
data_value: @data_value.to_h,
|
45
51
|
qualifiers: @qualifiers.map(&:to_h),
|
46
52
|
references: @references.map(&:to_h),
|
47
53
|
rank: @rank
|
@@ -56,10 +62,13 @@ class Wikidatum::Statement
|
|
56
62
|
# @param statement_json [Hash]
|
57
63
|
# @return [Wikidatum::Statement]
|
58
64
|
def self.marshal_load(statement_json)
|
59
|
-
|
65
|
+
data_type = Wikidatum::Utils.symbolized_name_for_data_type(statement_json['property']['data-type'])
|
66
|
+
data_value = Wikidatum::Utils.ingest_snak(statement_json)
|
67
|
+
|
68
|
+
property_id = statement_json['property']['id']
|
60
69
|
|
61
|
-
qualifiers = statement_json['qualifiers'].to_a.flat_map do |
|
62
|
-
|
70
|
+
qualifiers = statement_json['qualifiers'].to_a.flat_map do |qualifier|
|
71
|
+
Wikidatum::Qualifier.marshal_load(qualifier)
|
63
72
|
end
|
64
73
|
references = statement_json['references'].flat_map do |reference|
|
65
74
|
Wikidatum::Reference.marshal_load(reference)
|
@@ -67,8 +76,9 @@ class Wikidatum::Statement
|
|
67
76
|
|
68
77
|
Wikidatum::Statement.new(
|
69
78
|
id: statement_json['id'],
|
70
|
-
property_id:
|
71
|
-
|
79
|
+
property_id: property_id,
|
80
|
+
data_type: data_type,
|
81
|
+
data_value: data_value,
|
72
82
|
qualifiers: qualifiers,
|
73
83
|
references: references,
|
74
84
|
rank: statement_json['rank']
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Wikidatum::Utils
|
4
|
+
# Yum!
|
5
|
+
def self.ingest_snak(json)
|
6
|
+
# the type can be 'novalue' (no value) or 'somevalue' (unknown), so we handle those as somewhat special cases
|
7
|
+
case json['value']['type']
|
8
|
+
when 'novalue'
|
9
|
+
Wikidatum::DataType::Base.marshal_load('novalue', nil)
|
10
|
+
when 'somevalue'
|
11
|
+
Wikidatum::DataType::Base.marshal_load('somevalue', nil)
|
12
|
+
when 'value'
|
13
|
+
Wikidatum::DataType::Base.marshal_load(json['property']['data-type'], json['value']['content'])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.symbolized_name_for_data_type(data_type)
|
18
|
+
unless Wikidatum::DataType::DATA_TYPES.keys.include?(data_type.to_sym)
|
19
|
+
puts "WARNING: Unsupported data type (#{data_type})"
|
20
|
+
return nil
|
21
|
+
end
|
22
|
+
|
23
|
+
Object.const_get(Wikidatum::DataType::DATA_TYPES[data_type.to_sym]).symbolized_name
|
24
|
+
end
|
25
|
+
end
|
data/lib/wikidatum/version.rb
CHANGED
data/lib/wikidatum.rb
CHANGED
@@ -1,19 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
require 'wikidatum/client'
|
4
|
+
require 'wikidatum/data_type'
|
5
|
+
require 'wikidatum/item'
|
6
|
+
require 'wikidatum/qualifier'
|
7
|
+
require 'wikidatum/reference'
|
8
|
+
require 'wikidatum/reference_part'
|
9
|
+
require 'wikidatum/sitelink'
|
10
|
+
require 'wikidatum/statement'
|
11
|
+
require 'wikidatum/term'
|
12
|
+
require 'wikidatum/utils'
|
13
|
+
require 'wikidatum/version'
|
13
14
|
|
14
15
|
module Wikidatum
|
15
16
|
class Error < StandardError; end
|
16
17
|
|
18
|
+
# If the Wikidatum::Client is set to disallow IP Edits (the default) and no
|
19
|
+
# authentication has been provided, this error will be raised when performing
|
20
|
+
# any non-GET requests.
|
21
|
+
class DisallowedIpEditError < Error
|
22
|
+
def message
|
23
|
+
'No authentication provided. If you want to perform unauthenticated edits and are comfortable exposing your IP address publicly, set `allow_ip_edits: true` when instantiating your client with `Wikidatum::Client.new`.'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
17
27
|
# rubocop:disable Naming/ConstantName
|
18
28
|
|
19
29
|
# These language codes are not enforced, you can pass whatever language code
|
@@ -45,17 +55,18 @@ module Wikidatum
|
|
45
55
|
# rubocop:enable Naming/ConstantName
|
46
56
|
end
|
47
57
|
|
48
|
-
module
|
58
|
+
module DataType
|
59
|
+
# TODO: Add commonsMedia and url to the possible types.
|
49
60
|
# rubocop:disable Lint/SymbolConversion
|
50
|
-
|
51
|
-
'
|
52
|
-
'monolingualtext': 'Wikidatum::
|
53
|
-
'novalue': 'Wikidatum::
|
54
|
-
'quantity': 'Wikidatum::
|
55
|
-
'somevalue': 'Wikidatum::
|
56
|
-
'string': 'Wikidatum::
|
57
|
-
'time': 'Wikidatum::
|
58
|
-
'wikibase-
|
61
|
+
DATA_TYPES = {
|
62
|
+
'globe-coordinate': 'Wikidatum::DataType::GlobeCoordinate',
|
63
|
+
'monolingualtext': 'Wikidatum::DataType::MonolingualText',
|
64
|
+
'novalue': 'Wikidatum::DataType::NoValue',
|
65
|
+
'quantity': 'Wikidatum::DataType::Quantity',
|
66
|
+
'somevalue': 'Wikidatum::DataType::SomeValue',
|
67
|
+
'string': 'Wikidatum::DataType::WikibaseString',
|
68
|
+
'time': 'Wikidatum::DataType::Time',
|
69
|
+
'wikibase-item': 'Wikidatum::DataType::WikibaseItem'
|
59
70
|
}.freeze
|
60
71
|
# rubocop:enable Lint/SymbolConversion
|
61
72
|
end
|
data/wikidatum.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
25
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
26
|
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|))})
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|))}) || f.match(/\A(?:Gemfile|Rakefile)/)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
spec.bindir = "exe"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wikidatum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Connor Shea
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -32,32 +32,30 @@ extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
34
|
- CHANGELOG.md
|
35
|
-
- Gemfile
|
36
|
-
- Gemfile.lock
|
37
35
|
- LICENSE
|
38
36
|
- README.md
|
39
|
-
- Rakefile
|
40
37
|
- bin/console
|
41
38
|
- bin/setup
|
42
39
|
- lib/wikidatum.rb
|
43
40
|
- lib/wikidatum/client.rb
|
44
|
-
- lib/wikidatum/
|
45
|
-
- lib/wikidatum/
|
46
|
-
- lib/wikidatum/
|
47
|
-
- lib/wikidatum/
|
48
|
-
- lib/wikidatum/
|
49
|
-
- lib/wikidatum/
|
50
|
-
- lib/wikidatum/
|
51
|
-
- lib/wikidatum/
|
52
|
-
- lib/wikidatum/
|
53
|
-
- lib/wikidatum/
|
41
|
+
- lib/wikidatum/data_type.rb
|
42
|
+
- lib/wikidatum/data_type/base.rb
|
43
|
+
- lib/wikidatum/data_type/globe_coordinate.rb
|
44
|
+
- lib/wikidatum/data_type/monolingual_text.rb
|
45
|
+
- lib/wikidatum/data_type/no_value.rb
|
46
|
+
- lib/wikidatum/data_type/quantity.rb
|
47
|
+
- lib/wikidatum/data_type/some_value.rb
|
48
|
+
- lib/wikidatum/data_type/time.rb
|
49
|
+
- lib/wikidatum/data_type/wikibase_item.rb
|
50
|
+
- lib/wikidatum/data_type/wikibase_string.rb
|
54
51
|
- lib/wikidatum/item.rb
|
55
52
|
- lib/wikidatum/qualifier.rb
|
56
53
|
- lib/wikidatum/reference.rb
|
54
|
+
- lib/wikidatum/reference_part.rb
|
57
55
|
- lib/wikidatum/sitelink.rb
|
58
|
-
- lib/wikidatum/snak.rb
|
59
56
|
- lib/wikidatum/statement.rb
|
60
57
|
- lib/wikidatum/term.rb
|
58
|
+
- lib/wikidatum/utils.rb
|
61
59
|
- lib/wikidatum/version.rb
|
62
60
|
- wikidatum.gemspec
|
63
61
|
homepage:
|
@@ -84,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
82
|
- !ruby/object:Gem::Version
|
85
83
|
version: '0'
|
86
84
|
requirements: []
|
87
|
-
rubygems_version: 3.
|
85
|
+
rubygems_version: 3.4.1
|
88
86
|
signing_key:
|
89
87
|
specification_version: 4
|
90
88
|
summary: Ruby gem for the new Wikidata REST API.
|
data/Gemfile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in wikidatum.gemspec
|
6
|
-
gemspec
|
7
|
-
|
8
|
-
gem 'rake', '~> 13.0'
|
9
|
-
gem 'minitest', '~> 5.16'
|
10
|
-
gem 'simplecov', '~> 0.21', require: false
|
11
|
-
gem 'rubocop', '~> 1.30'
|
12
|
-
gem 'yard', '~> 0.9'
|
13
|
-
gem 'webmock', '~> 3.17', require: false
|
data/Gemfile.lock
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
wikidatum (0.2.0)
|
5
|
-
faraday (~> 2.4)
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
addressable (2.8.0)
|
11
|
-
public_suffix (>= 2.0.2, < 5.0)
|
12
|
-
ast (2.4.2)
|
13
|
-
crack (0.4.5)
|
14
|
-
rexml
|
15
|
-
docile (1.4.0)
|
16
|
-
faraday (2.4.0)
|
17
|
-
faraday-net_http (~> 2.0)
|
18
|
-
ruby2_keywords (>= 0.0.4)
|
19
|
-
faraday-net_http (2.1.0)
|
20
|
-
hashdiff (1.0.1)
|
21
|
-
minitest (5.16.0)
|
22
|
-
parallel (1.22.1)
|
23
|
-
parser (3.1.2.0)
|
24
|
-
ast (~> 2.4.1)
|
25
|
-
public_suffix (4.0.7)
|
26
|
-
rainbow (3.1.1)
|
27
|
-
rake (13.0.6)
|
28
|
-
regexp_parser (2.5.0)
|
29
|
-
rexml (3.2.5)
|
30
|
-
rubocop (1.30.1)
|
31
|
-
parallel (~> 1.10)
|
32
|
-
parser (>= 3.1.0.0)
|
33
|
-
rainbow (>= 2.2.2, < 4.0)
|
34
|
-
regexp_parser (>= 1.8, < 3.0)
|
35
|
-
rexml (>= 3.2.5, < 4.0)
|
36
|
-
rubocop-ast (>= 1.18.0, < 2.0)
|
37
|
-
ruby-progressbar (~> 1.7)
|
38
|
-
unicode-display_width (>= 1.4.0, < 3.0)
|
39
|
-
rubocop-ast (1.18.0)
|
40
|
-
parser (>= 3.1.1.0)
|
41
|
-
ruby-progressbar (1.11.0)
|
42
|
-
ruby2_keywords (0.0.5)
|
43
|
-
simplecov (0.21.2)
|
44
|
-
docile (~> 1.1)
|
45
|
-
simplecov-html (~> 0.11)
|
46
|
-
simplecov_json_formatter (~> 0.1)
|
47
|
-
simplecov-html (0.12.3)
|
48
|
-
simplecov_json_formatter (0.1.4)
|
49
|
-
unicode-display_width (2.1.0)
|
50
|
-
webmock (3.17.1)
|
51
|
-
addressable (>= 2.8.0)
|
52
|
-
crack (>= 0.3.2)
|
53
|
-
hashdiff (>= 0.4.0, < 2.0.0)
|
54
|
-
webrick (1.7.0)
|
55
|
-
yard (0.9.28)
|
56
|
-
webrick (~> 1.7.0)
|
57
|
-
|
58
|
-
PLATFORMS
|
59
|
-
arm64-darwin-21
|
60
|
-
x86_64-darwin-21
|
61
|
-
x86_64-linux
|
62
|
-
|
63
|
-
DEPENDENCIES
|
64
|
-
minitest (~> 5.16)
|
65
|
-
rake (~> 13.0)
|
66
|
-
rubocop (~> 1.30)
|
67
|
-
simplecov (~> 0.21)
|
68
|
-
webmock (~> 3.17)
|
69
|
-
wikidatum!
|
70
|
-
yard (~> 0.9)
|
71
|
-
|
72
|
-
BUNDLED WITH
|
73
|
-
2.3.3
|
data/Rakefile
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "bundler/gem_tasks"
|
4
|
-
require "rake/testtask"
|
5
|
-
|
6
|
-
Rake::TestTask.new(:test) do |t|
|
7
|
-
t.libs << "test"
|
8
|
-
t.libs << "lib"
|
9
|
-
t.test_files = FileList["test/**/*_spec.rb"]
|
10
|
-
end
|
11
|
-
|
12
|
-
require "rubocop/rake_task"
|
13
|
-
|
14
|
-
RuboCop::RakeTask.new
|
15
|
-
|
16
|
-
task default: %i[test rubocop]
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# For more information on the possible types that can be returned by
|
4
|
-
# datavalues, see the official documentation:
|
5
|
-
# https://doc.wikimedia.org/Wikibase/master/php/md_docs_topics_json.html#json_datavalues
|
6
|
-
module Wikidatum::DataValueType
|
7
|
-
class Base
|
8
|
-
# Represents the type for this datavalue instance.
|
9
|
-
#
|
10
|
-
# Possible values for the `type` attribute are:
|
11
|
-
#
|
12
|
-
# - `:no_value`: No value
|
13
|
-
# - `:some_value`: Unknown value
|
14
|
-
# - `:globe_coordinate`: {DataValueType::GlobeCoordinate}
|
15
|
-
# - `:monolingual_text`: {DataValueType::MonolingualText}
|
16
|
-
# - `:quantity`: {DataValueType::Quantity}
|
17
|
-
# - `:string`: {DataValueType::WikibaseString}
|
18
|
-
# - `:time`: {DataValueType::Time}
|
19
|
-
# - `:wikibase_entity_id`: {DataValueType::WikibaseEntityId}
|
20
|
-
#
|
21
|
-
# @return [Symbol]
|
22
|
-
attr_reader :type
|
23
|
-
|
24
|
-
# The value of the datavalue object in the response.
|
25
|
-
#
|
26
|
-
# If the `type` is `novalue` or `somevalue`, this returns `nil`.
|
27
|
-
#
|
28
|
-
# @return [DataValueType::GlobeCoordinate, DataValueType::MonolingualText, DataValueType::Quantity, DataValueType::WikibaseString, DataValueType::Time, DataValueType::WikibaseEntityId, nil]
|
29
|
-
attr_reader :value
|
30
|
-
|
31
|
-
# @param type [Symbol]
|
32
|
-
# @param value [DataValueType::GlobeCoordinate, DataValueType::MonolingualText, DataValueType::Quantity, DataValueType::WikibaseString, DataValueType::Time, DataValueType::WikibaseEntityId, nil] nil if type is no_value or some_value
|
33
|
-
# @return [void]
|
34
|
-
def initialize(type:, value:)
|
35
|
-
@type = type
|
36
|
-
@value = value
|
37
|
-
end
|
38
|
-
|
39
|
-
# @return [Hash]
|
40
|
-
def to_h
|
41
|
-
{
|
42
|
-
type: @type,
|
43
|
-
value: @value&.to_h
|
44
|
-
}
|
45
|
-
end
|
46
|
-
|
47
|
-
# @!visibility private
|
48
|
-
#
|
49
|
-
# @param data_value_type [String] The value of `type` for the given Snak's datavalue.
|
50
|
-
# @param data_value_json [Hash] The `value` part of datavalue.
|
51
|
-
# @return [Wikidatum::DataValueType::Base] An instance of Base.
|
52
|
-
def self.marshal_load(data_value_type, data_value_json)
|
53
|
-
unless Wikidatum::DataValueType::DATA_VALUE_TYPES.keys.include?(data_value_type.to_sym)
|
54
|
-
puts 'WARNING: Unsupported datavalue type.'
|
55
|
-
return nil
|
56
|
-
end
|
57
|
-
|
58
|
-
Object.const_get(Wikidatum::DataValueType::DATA_VALUE_TYPES[data_value_type.to_sym]).marshal_load(data_value_json)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,92 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'wikidatum/data_value_type/base'
|
4
|
-
|
5
|
-
# The Quantity type datavalue JSON looks like this:
|
6
|
-
#
|
7
|
-
# ```json
|
8
|
-
# {
|
9
|
-
# "datavalue": {
|
10
|
-
# "value": {
|
11
|
-
# "amount": "+10.38",
|
12
|
-
# "upperBound": "+10.375",
|
13
|
-
# "lowerBound": "+10.385",
|
14
|
-
# "unit": "http://www.wikidata.org/entity/Q712226"
|
15
|
-
# },
|
16
|
-
# "type": "quantity"
|
17
|
-
# }
|
18
|
-
# }
|
19
|
-
# ```
|
20
|
-
class Wikidatum::DataValueType::Quantity
|
21
|
-
# @return [String] A string value like "+2", usually an integer but not always.
|
22
|
-
attr_reader :amount
|
23
|
-
|
24
|
-
# @return [String, nil] upper bound, if one is defined.
|
25
|
-
attr_reader :upper_bound
|
26
|
-
|
27
|
-
# @return [String, nil] lower bound, if one is defined.
|
28
|
-
attr_reader :lower_bound
|
29
|
-
|
30
|
-
# @return [String] a URL describing the unit for this quantity, e.g. "meter", "kilometer", "pound", "chapter", "section", etc.
|
31
|
-
attr_reader :unit
|
32
|
-
|
33
|
-
# @param amount [String]
|
34
|
-
# @param upper_bound [String]
|
35
|
-
# @param lower_bound [String]
|
36
|
-
# @param unit [String]
|
37
|
-
# @return [void]
|
38
|
-
def initialize(amount:, upper_bound:, lower_bound:, unit:)
|
39
|
-
@amount = amount
|
40
|
-
@upper_bound = upper_bound
|
41
|
-
@lower_bound = lower_bound
|
42
|
-
@unit = unit
|
43
|
-
end
|
44
|
-
|
45
|
-
# @return [Hash]
|
46
|
-
def to_h
|
47
|
-
{
|
48
|
-
amount: @amount,
|
49
|
-
upper_bound: @upper_bound,
|
50
|
-
lower_bound: @lower_bound,
|
51
|
-
unit: @unit
|
52
|
-
}
|
53
|
-
end
|
54
|
-
|
55
|
-
# The "type" value used by Wikibase, for use when creating/updating statements.
|
56
|
-
#
|
57
|
-
# @return [String]
|
58
|
-
def wikibase_type
|
59
|
-
'quantity'
|
60
|
-
end
|
61
|
-
|
62
|
-
# The "datatype" value used by Wikibase, usually identical to wikibase_type
|
63
|
-
# but not always.
|
64
|
-
#
|
65
|
-
# @return [String]
|
66
|
-
def wikibase_datatype
|
67
|
-
wikibase_type
|
68
|
-
end
|
69
|
-
|
70
|
-
# @!visibility private
|
71
|
-
def self.marshal_load(data_value_json)
|
72
|
-
Wikidatum::DataValueType::Base.new(
|
73
|
-
type: :quantity,
|
74
|
-
value: new(
|
75
|
-
amount: data_value_json['amount'],
|
76
|
-
upper_bound: data_value_json['upperBound'],
|
77
|
-
lower_bound: data_value_json['lowerBound'],
|
78
|
-
unit: data_value_json['unit']
|
79
|
-
)
|
80
|
-
)
|
81
|
-
end
|
82
|
-
|
83
|
-
# @!visibility private
|
84
|
-
def marshal_dump
|
85
|
-
{
|
86
|
-
amount: @amount,
|
87
|
-
upperBound: @upper_bound,
|
88
|
-
lowerBound: @lower_bound,
|
89
|
-
unit: @unit
|
90
|
-
}
|
91
|
-
end
|
92
|
-
end
|