wikidatum 0.2.1 → 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 +10 -2
- data/README.md +24 -2
- data/lib/wikidatum/client.rb +120 -54
- 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 +23 -12
- metadata +15 -14
- 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
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/data_type/base'
|
4
|
+
|
5
|
+
# The Quantity type JSON looks like this:
|
6
|
+
#
|
7
|
+
# ```json
|
8
|
+
# {
|
9
|
+
# "property": {
|
10
|
+
# "id": "P937",
|
11
|
+
# "data-type": "quantity"
|
12
|
+
# },
|
13
|
+
# "value": {
|
14
|
+
# "type": "value",
|
15
|
+
# "content": {
|
16
|
+
# "amount": "+15",
|
17
|
+
# "unit": "1"
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
# }
|
21
|
+
# ```
|
22
|
+
class Wikidatum::DataType::Quantity
|
23
|
+
# @return [String] A string value like "+2", usually an integer but not always.
|
24
|
+
attr_reader :amount
|
25
|
+
|
26
|
+
# @return [String] a URL describing the unit for this quantity, e.g. "meter", "kilometer", "pound", "chapter", "section", etc.
|
27
|
+
attr_reader :unit
|
28
|
+
|
29
|
+
# @param amount [String]
|
30
|
+
# @param unit [String]
|
31
|
+
# @return [void]
|
32
|
+
def initialize(amount:, unit:)
|
33
|
+
@amount = amount
|
34
|
+
@unit = unit
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Hash]
|
38
|
+
def to_h
|
39
|
+
{
|
40
|
+
amount: @amount,
|
41
|
+
unit: @unit
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
# The "type" value used by Wikibase, for use when creating/updating statements.
|
46
|
+
#
|
47
|
+
# @return [String]
|
48
|
+
def wikibase_type
|
49
|
+
'quantity'
|
50
|
+
end
|
51
|
+
|
52
|
+
# @return [Symbol]
|
53
|
+
def self.symbolized_name
|
54
|
+
:quantity
|
55
|
+
end
|
56
|
+
|
57
|
+
# @!visibility private
|
58
|
+
def self.marshal_load(data_value_json)
|
59
|
+
Wikidatum::DataType::Base.new(
|
60
|
+
type: symbolized_name,
|
61
|
+
content: new(
|
62
|
+
amount: data_value_json['amount'],
|
63
|
+
unit: data_value_json['unit']
|
64
|
+
)
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
# @!visibility private
|
69
|
+
def marshal_dump
|
70
|
+
{
|
71
|
+
amount: @amount,
|
72
|
+
unit: @unit
|
73
|
+
}
|
74
|
+
end
|
75
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'wikidatum/
|
3
|
+
require 'wikidatum/data_type/base'
|
4
4
|
|
5
5
|
# The SomeValue type actually has no datavalue key in the blob at all. We work
|
6
6
|
# around this by just passing nil to the serializer.
|
7
7
|
|
8
8
|
# Represents a value of "unknown value".
|
9
|
-
class Wikidatum::
|
9
|
+
class Wikidatum::DataType::SomeValue < Wikidatum::DataType::Base
|
10
10
|
# The "type" value used by Wikibase, for use when creating/updating statements.
|
11
11
|
#
|
12
12
|
# @return [String]
|
@@ -14,16 +14,8 @@ class Wikidatum::DataValueType::SomeValue < Wikidatum::DataValueType::Base
|
|
14
14
|
'string'
|
15
15
|
end
|
16
16
|
|
17
|
-
# The "datatype" value used by Wikibase, usually identical to wikibase_type
|
18
|
-
# but not always.
|
19
|
-
#
|
20
|
-
# @return [String]
|
21
|
-
def wikibase_datatype
|
22
|
-
wikibase_type
|
23
|
-
end
|
24
|
-
|
25
17
|
# @!visibility private
|
26
18
|
def self.marshal_load(_data_value_json)
|
27
|
-
new(type: :some_value,
|
19
|
+
new(type: :some_value, content: nil)
|
28
20
|
end
|
29
21
|
end
|
@@ -1,33 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'wikidatum/
|
3
|
+
require 'wikidatum/data_type/base'
|
4
4
|
|
5
|
-
# The time type
|
5
|
+
# The time type JSON looks like this:
|
6
6
|
#
|
7
7
|
# ```json
|
8
8
|
# {
|
9
|
-
# "
|
10
|
-
# "
|
9
|
+
# "property": {
|
10
|
+
# "id": "P761",
|
11
|
+
# "data-type": "time"
|
12
|
+
# },
|
13
|
+
# "value": {
|
14
|
+
# "type": "value",
|
15
|
+
# "content": {
|
11
16
|
# "time": "+2019-11-14T00:00:00Z",
|
12
|
-
# "timezone": 0,
|
13
|
-
# "before": 0,
|
14
|
-
# "after": 0,
|
15
17
|
# "precision": 11,
|
16
18
|
# "calendarmodel": "http://www.wikidata.org/entity/Q1985727"
|
17
|
-
# }
|
18
|
-
# "type": "time"
|
19
|
+
# }
|
19
20
|
# }
|
20
21
|
# }
|
21
22
|
# ```
|
22
23
|
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# NOTE: For consistency with Ruby snake_case attribute names, `timezone` from
|
27
|
-
# the API is represented as `time_zone` and `calendarmodel` is
|
28
|
-
# `calendar_model`. However, we expose aliases so `timezone` and
|
24
|
+
# NOTE: For consistency with Ruby snake_case attribute names, `calendarmodel`
|
25
|
+
# in the API is `calendar_model`. However, we expose an alias so
|
29
26
|
# `calendarmodel` will still work.
|
30
|
-
class Wikidatum::
|
27
|
+
class Wikidatum::DataType::Time
|
31
28
|
# A string representing the time in a format that is very similar to ISO 8601.
|
32
29
|
#
|
33
30
|
# For example, here are what dates look like for the most common precisions:
|
@@ -48,11 +45,6 @@ class Wikidatum::DataValueType::Time
|
|
48
45
|
# @return [String] the time value, in a format like "+2022-01-01T00:00:00Z", though how this should be interpreted depends on the precision.
|
49
46
|
attr_reader :time
|
50
47
|
|
51
|
-
# @return [Integer] an integer for the offset (in minutes) from UTC. 0 means
|
52
|
-
# UTC, will currently always be 0 but the Wikibase backend may change that
|
53
|
-
# in the future.
|
54
|
-
attr_reader :time_zone
|
55
|
-
|
56
48
|
# An integer representing the precision of the date, where the integers correspond to the following:
|
57
49
|
#
|
58
50
|
# - 0: 1 Gigayear
|
@@ -91,13 +83,11 @@ class Wikidatum::DataValueType::Time
|
|
91
83
|
attr_reader :calendar_model
|
92
84
|
|
93
85
|
# @param time [String]
|
94
|
-
# @param time_zone [Integer]
|
95
86
|
# @param precision [Integer]
|
96
87
|
# @param calendar_model [String]
|
97
88
|
# @return [void]
|
98
|
-
def initialize(time:,
|
89
|
+
def initialize(time:, precision:, calendar_model:)
|
99
90
|
@time = time
|
100
|
-
@time_zone = time_zone
|
101
91
|
@precision = precision
|
102
92
|
@calendar_model = calendar_model
|
103
93
|
end
|
@@ -106,7 +96,6 @@ class Wikidatum::DataValueType::Time
|
|
106
96
|
def to_h
|
107
97
|
{
|
108
98
|
time: @time,
|
109
|
-
time_zone: @time_zone,
|
110
99
|
precision: @precision,
|
111
100
|
pretty_precision: pretty_precision,
|
112
101
|
calendar_model: @calendar_model
|
@@ -120,21 +109,17 @@ class Wikidatum::DataValueType::Time
|
|
120
109
|
'time'
|
121
110
|
end
|
122
111
|
|
123
|
-
#
|
124
|
-
|
125
|
-
|
126
|
-
# @return [String]
|
127
|
-
def wikibase_datatype
|
128
|
-
wikibase_type
|
112
|
+
# @return [Symbol]
|
113
|
+
def self.symbolized_name
|
114
|
+
:time
|
129
115
|
end
|
130
116
|
|
131
117
|
# @!visibility private
|
132
118
|
def self.marshal_load(data_value_json)
|
133
|
-
Wikidatum::
|
134
|
-
type:
|
135
|
-
|
119
|
+
Wikidatum::DataType::Base.new(
|
120
|
+
type: symbolized_name,
|
121
|
+
content: new(
|
136
122
|
time: data_value_json['time'],
|
137
|
-
time_zone: data_value_json['timezone'],
|
138
123
|
precision: data_value_json['precision'],
|
139
124
|
calendar_model: data_value_json['calendarmodel']
|
140
125
|
)
|
@@ -145,7 +130,6 @@ class Wikidatum::DataValueType::Time
|
|
145
130
|
def marshal_dump
|
146
131
|
{
|
147
132
|
time: @time,
|
148
|
-
timezone: @time_zone,
|
149
133
|
precision: @precision,
|
150
134
|
calendarmodel: @calendar_model
|
151
135
|
}
|
@@ -182,6 +166,5 @@ class Wikidatum::DataValueType::Time
|
|
182
166
|
|
183
167
|
# Aliases to match the name returned by the REST API.
|
184
168
|
|
185
|
-
alias timezone time_zone
|
186
169
|
alias calendarmodel calendar_model
|
187
170
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/data_type/base'
|
4
|
+
|
5
|
+
# The Wikibase Item type JSON looks like this:
|
6
|
+
#
|
7
|
+
# ```json
|
8
|
+
# {
|
9
|
+
# "property": {
|
10
|
+
# "id": "P963",
|
11
|
+
# "data-type": "wikibase-item"
|
12
|
+
# },
|
13
|
+
# "value": {
|
14
|
+
# "type": "value",
|
15
|
+
# "content": "Q524026"
|
16
|
+
# }
|
17
|
+
# }
|
18
|
+
# ```
|
19
|
+
class Wikidatum::DataType::WikibaseItem
|
20
|
+
# @return [String] in the format "Q123".
|
21
|
+
attr_reader :id
|
22
|
+
|
23
|
+
# @param id [String]
|
24
|
+
# @return [void]
|
25
|
+
def initialize(id:)
|
26
|
+
@id = id
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Hash]
|
30
|
+
def to_h
|
31
|
+
{
|
32
|
+
id: @id
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
# The "type" value used by Wikibase, for use when creating/updating statements.
|
37
|
+
#
|
38
|
+
# @return [String]
|
39
|
+
def wikibase_type
|
40
|
+
'wikibase-item'
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [Symbol]
|
44
|
+
def self.symbolized_name
|
45
|
+
:wikibase_item
|
46
|
+
end
|
47
|
+
|
48
|
+
# @!visibility private
|
49
|
+
def self.marshal_load(id)
|
50
|
+
Wikidatum::DataType::Base.new(
|
51
|
+
type: symbolized_name,
|
52
|
+
content: new(
|
53
|
+
id: id
|
54
|
+
)
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
# @!visibility private
|
59
|
+
def marshal_dump
|
60
|
+
{
|
61
|
+
id: @id
|
62
|
+
}
|
63
|
+
end
|
64
|
+
end
|
@@ -1,18 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'wikidatum/
|
3
|
+
require 'wikidatum/data_type/base'
|
4
4
|
|
5
|
-
# The String type
|
5
|
+
# The String type JSON looks like this:
|
6
6
|
#
|
7
7
|
# ```json
|
8
8
|
# {
|
9
|
-
# "
|
10
|
-
# "
|
11
|
-
# "type": "string"
|
9
|
+
# "property": {
|
10
|
+
# "id": "P143",
|
11
|
+
# "data-type": "string"
|
12
|
+
# },
|
13
|
+
# "value": {
|
14
|
+
# "type": "value",
|
15
|
+
# "content": "foo"
|
12
16
|
# }
|
13
17
|
# }
|
14
18
|
# ```
|
15
|
-
class Wikidatum::
|
19
|
+
class Wikidatum::DataType::WikibaseString
|
16
20
|
# @return [String] the value for the string.
|
17
21
|
attr_reader :string
|
18
22
|
|
@@ -36,19 +40,16 @@ class Wikidatum::DataValueType::WikibaseString
|
|
36
40
|
'string'
|
37
41
|
end
|
38
42
|
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
# @return [String]
|
43
|
-
def wikibase_datatype
|
44
|
-
wikibase_type
|
43
|
+
# @return [Symbol]
|
44
|
+
def self.symbolized_name
|
45
|
+
:string
|
45
46
|
end
|
46
47
|
|
47
48
|
# @!visibility private
|
48
49
|
def self.marshal_load(string)
|
49
|
-
Wikidatum::
|
50
|
-
type:
|
51
|
-
|
50
|
+
Wikidatum::DataType::Base.new(
|
51
|
+
type: symbolized_name,
|
52
|
+
content: new(
|
52
53
|
string: string
|
53
54
|
)
|
54
55
|
)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/data_type/base'
|
4
|
+
require 'wikidatum/data_type/globe_coordinate'
|
5
|
+
require 'wikidatum/data_type/monolingual_text'
|
6
|
+
require 'wikidatum/data_type/no_value'
|
7
|
+
require 'wikidatum/data_type/quantity'
|
8
|
+
require 'wikidatum/data_type/some_value'
|
9
|
+
require 'wikidatum/data_type/time'
|
10
|
+
require 'wikidatum/data_type/wikibase_item'
|
11
|
+
require 'wikidatum/data_type/wikibase_string'
|
data/lib/wikidatum/item.rb
CHANGED
@@ -186,8 +186,8 @@ class Wikidatum::Item
|
|
186
186
|
statements = item_json['statements'].to_a.flat_map do |_property_id, st_arr|
|
187
187
|
st_arr.map { |statement| Wikidatum::Statement.marshal_load(statement) }
|
188
188
|
end
|
189
|
-
sitelinks = item_json['sitelinks'].to_a.map do |
|
190
|
-
Wikidatum::Sitelink.new(site:
|
189
|
+
sitelinks = item_json['sitelinks'].to_a.map do |site, sitelink|
|
190
|
+
Wikidatum::Sitelink.new(site: site, title: sitelink['title'], badges: sitelink['badges'])
|
191
191
|
end
|
192
192
|
|
193
193
|
Wikidatum::Item.new(
|
data/lib/wikidatum/qualifier.rb
CHANGED
@@ -1,7 +1,55 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class Wikidatum::Qualifier
|
4
|
+
# @return [String] ID of the property for this Qualifier, in the format 'P123'.
|
5
|
+
attr_reader :property_id
|
4
6
|
|
5
|
-
#
|
6
|
-
|
7
|
-
|
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 for this Qualifier, 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::Qualifier 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
|
+
# qualifier the API and turns it into an actual instance of a Qualifier.
|
45
|
+
#
|
46
|
+
# @param qualifier_json [Hash]
|
47
|
+
# @return [Wikidatum::Qualifier]
|
48
|
+
def self.marshal_load(qualifier_json)
|
49
|
+
Wikidatum::Qualifier.new(
|
50
|
+
property_id: qualifier_json.dig('property', 'id'),
|
51
|
+
data_type: qualifier_json.dig('property', 'data-type'),
|
52
|
+
value: Wikidatum::Utils.ingest_snak(qualifier_json)
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
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