wikidatum 0.3.1 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/LICENSE +1 -1
- data/README.md +13 -6
- data/lib/wikidatum/client.rb +75 -7
- data/lib/wikidatum/data_type/base.rb +15 -4
- data/lib/wikidatum/data_type/commons_media.rb +20 -0
- data/lib/wikidatum/data_type/external_id.rb +20 -0
- data/lib/wikidatum/data_type/globe_coordinate.rb +11 -1
- data/lib/wikidatum/data_type/monolingual_text.rb +10 -0
- data/lib/wikidatum/data_type/no_value.rb +10 -0
- data/lib/wikidatum/data_type/quantity.rb +10 -0
- data/lib/wikidatum/data_type/some_value.rb +10 -0
- data/lib/wikidatum/data_type/time.rb +10 -0
- data/lib/wikidatum/data_type/wikibase_item.rb +8 -0
- data/lib/wikidatum/data_type/wikibase_string.rb +8 -0
- data/lib/wikidatum/data_type/wikibase_url.rb +20 -0
- data/lib/wikidatum/data_type.rb +3 -0
- data/lib/wikidatum/error.rb +5 -0
- data/lib/wikidatum/errors.rb +23 -0
- data/lib/wikidatum/qualifier.rb +1 -1
- data/lib/wikidatum/reference_part.rb +2 -2
- data/lib/wikidatum/sitelink.rb +6 -5
- data/lib/wikidatum/statement.rb +2 -2
- data/lib/wikidatum/term.rb +3 -3
- data/lib/wikidatum/version.rb +1 -1
- data/lib/wikidatum.rb +5 -21
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 524da97907bba148830e9191d3c0ba5c614ddbee6c50fd74c6e91595082e73bd
|
4
|
+
data.tar.gz: 2321efe8c69e953521dbe5f4da51725fd6183ac871e7c00a17c552c12dc15dc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 634701e7e5204ea770eb114f62982140e3441358e71f1ca32bf38718f9e72f29fe582340546c28e0dad4e575903c6822057bb12f21572333c68cd6c28b2f1e46
|
7
|
+
data.tar.gz: 26cf62b267be9bb4a6cc752d1968740b3d153f972204f0b1d7900fa7e087cea6eef0e543972b3aafb22f98ffbea8cdc6e30f22cc30f34aa77d2f4d1605655d02
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
|
7
7
|
## Unreleased
|
8
8
|
|
9
|
+
## 0.3.3 - 2023-01-24
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Add a `labels` method on Wikidatum::Client for getting item label data from the new `items/{id}/labels` endpoint. (This isn't supported in the initial release of the REST API on Wikidata, so it will not work yet)
|
13
|
+
|
14
|
+
### Changed
|
15
|
+
|
16
|
+
- Move error classes into their own `Wikidatum::Errors` namespace.
|
17
|
+
|
18
|
+
## 0.3.2 - 2022-12-29
|
19
|
+
### Added
|
20
|
+
|
21
|
+
- Add support for the `commonsMedia`, `url`, and `external-id` data types.
|
22
|
+
- Add `humanized_content` method to `Wikidatum::DataType::Base` class, for easier access to a Struct, string, or nil which represents the contents of the given data type.
|
23
|
+
|
9
24
|
## 0.3.1 - 2022-12-28
|
10
25
|
### Added
|
11
26
|
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
|
3
3
|
This gem supports making requests to the [new Wikidata/Wikibase REST API](https://doc.wikimedia.org/Wikibase/master/js/rest-api/).
|
4
4
|
|
5
|
-
The [Wikimedia Docs on Wikibase
|
5
|
+
The [Wikimedia Docs on the Wikibase REST API data format differences](https://doc.wikimedia.org/Wikibase/master/php/rest_data_format_differences.html) are also very useful for interacting with/contributing to this gem.
|
6
6
|
|
7
|
-
**The gem is currently in
|
7
|
+
**The gem is currently in development. It's ready to be used, but you should be careful when making edits with it to ensure it's working correctly.** It's missing some key features, namely authentication support, but the core of the library works.
|
8
|
+
|
9
|
+
I reserve the right to make breaking changes while the library is still in the 0.x release series, although I'll avoid them unless I believe them to be significantly better for the library's usability/maintainability (or are necessary due to breaking changes in the REST API itself). If they do happen, I'll make them clear in the Changelog.
|
8
10
|
|
9
11
|
## Installation
|
10
12
|
|
@@ -24,7 +26,7 @@ Or install it yourself as:
|
|
24
26
|
|
25
27
|
## Usage
|
26
28
|
|
27
|
-
You can view the YARD docs on GitHub Pages [here](https://connorshea.github.io/wikidatum/index.html).
|
29
|
+
You can view the YARD docs on GitHub Pages [here](https://connorshea.github.io/wikidatum/index.html). Generally, you'll want to look at the docs for {Wikidatum::Client} to get started.
|
28
30
|
|
29
31
|
Currently, the gem is able to hit a few of the basic endpoints, and currently has no way to provide authentication. The additional features will be added later.
|
30
32
|
|
@@ -33,9 +35,10 @@ require 'wikidatum'
|
|
33
35
|
|
34
36
|
wikidatum_client = Wikidatum::Client.new(
|
35
37
|
user_agent: 'REPLACE ME WITH THE NAME OF YOUR BOT!',
|
36
|
-
|
37
|
-
#
|
38
|
-
|
38
|
+
wikibase_url: 'https://wikidata.org',
|
39
|
+
# NOTE: To edit as a bot, you need to authenticate as a user with the Bot
|
40
|
+
# flag. If you don't have that flag on your Wikibase User, you'll get a
|
41
|
+
# 403 error.
|
39
42
|
bot: true
|
40
43
|
)
|
41
44
|
|
@@ -60,6 +63,10 @@ item.label(lang: :en).value #=> "Earth"
|
|
60
63
|
# Get the values for all English aliases on this item.
|
61
64
|
item.aliases(langs: [:en]).map(&:value) #=> ["Planet Earth", "Pale Blue Dot"]
|
62
65
|
|
66
|
+
# Get all labels for a given item.
|
67
|
+
wikidatum_client.labels(id: 'Q2') #=> Array<Wikidatum::Term>
|
68
|
+
|
69
|
+
# Get a specific statement from its ID.
|
63
70
|
statement_id = 'Q123$4543523c-1d1d-1111-1e1e-11b11111b1f1'
|
64
71
|
statement = wikidatum_client.statement(id: statement_id) #=> Wikidatum::Statement
|
65
72
|
|
data/lib/wikidatum/client.rb
CHANGED
@@ -10,6 +10,8 @@ module Wikidatum
|
|
10
10
|
STATEMENT_REGEX = /^Q?\d+\$[\w-]+$/.freeze
|
11
11
|
VALID_RANKS = ['preferred', 'normal', 'deprecated'].freeze
|
12
12
|
VALID_DATA_TYPES = [
|
13
|
+
'Wikidatum::DataType::CommonsMedia',
|
14
|
+
'Wikidatum::DataType::ExternalId',
|
13
15
|
'Wikidatum::DataType::GlobeCoordinate',
|
14
16
|
'Wikidatum::DataType::MonolingualText',
|
15
17
|
'Wikidatum::DataType::NoValue',
|
@@ -17,7 +19,21 @@ module Wikidatum
|
|
17
19
|
'Wikidatum::DataType::SomeValue',
|
18
20
|
'Wikidatum::DataType::Time',
|
19
21
|
'Wikidatum::DataType::WikibaseItem',
|
20
|
-
'Wikidatum::DataType::WikibaseString'
|
22
|
+
'Wikidatum::DataType::WikibaseString',
|
23
|
+
'Wikidatum::DataType::WikibaseUrl'
|
24
|
+
].freeze
|
25
|
+
|
26
|
+
# @!private
|
27
|
+
CONTENT_DATA_TYPES = [
|
28
|
+
'Wikidatum::DataType::CommonsMedia',
|
29
|
+
'Wikidatum::DataType::ExternalId',
|
30
|
+
'Wikidatum::DataType::GlobeCoordinate',
|
31
|
+
'Wikidatum::DataType::MonolingualText',
|
32
|
+
'Wikidatum::DataType::Quantity',
|
33
|
+
'Wikidatum::DataType::Time',
|
34
|
+
'Wikidatum::DataType::WikibaseItem',
|
35
|
+
'Wikidatum::DataType::WikibaseString',
|
36
|
+
'Wikidatum::DataType::WikibaseUrl'
|
21
37
|
].freeze
|
22
38
|
|
23
39
|
# @return [String] the root URL of the Wikibase instance we want to interact
|
@@ -52,7 +68,10 @@ module Wikidatum
|
|
52
68
|
# to interact with. If not provided, will default to
|
53
69
|
# `https://www.wikidata.org`. Do not include a `/` at the end of the URL.
|
54
70
|
# @param bot [Boolean] Whether requests sent by this client instance should
|
55
|
-
# be registered as bot requests. Defaults to `true`.
|
71
|
+
# be registered as bot requests. Defaults to `true`. If the user is not
|
72
|
+
# authenticated, or if the user is not marked with the Bot flag in the
|
73
|
+
# Wikibase instance, you'll be unable to make edits with this set to
|
74
|
+
# `true`.
|
56
75
|
# @param allow_ip_edits [Boolean] whether this client should allow non-GET
|
57
76
|
# requests if authentication hasn't been provided. Defaults to false. If
|
58
77
|
# this is set to true, the IP address of the device from which the
|
@@ -93,6 +112,28 @@ module Wikidatum
|
|
93
112
|
Wikidatum::Item.marshal_load(response)
|
94
113
|
end
|
95
114
|
|
115
|
+
# Get labels for an item from the Wikibase API based on the item's QID.
|
116
|
+
#
|
117
|
+
# @example
|
118
|
+
# wikidatum_client.labels(id: 'Q123') #=> [<Wikidatum::Term lang="en" value="Foo">, <Wikidatum::Term lang="es" value="Bar">]
|
119
|
+
# wikidatum_client.labels(id: 123)
|
120
|
+
# wikidatum_client.labels(id: '123')
|
121
|
+
#
|
122
|
+
# @param id [String, Integer] Either a string or integer representation of
|
123
|
+
# the relevant item's QID, e.g. `"Q123"`, `"123"`, or `123`.
|
124
|
+
# @return [Array<Wikidatum::Term>] This can, theoretically, be empty if the item has no labels.
|
125
|
+
def labels(id:)
|
126
|
+
raise ArgumentError, "#{id.inspect} is an invalid Wikibase QID. Must be an integer, a string representation of an integer, or in the format 'Q123'." unless id.is_a?(Integer) || id.match?(ITEM_REGEX)
|
127
|
+
|
128
|
+
id = coerce_item_id(id)
|
129
|
+
|
130
|
+
response = get_request("/entities/items/#{id}/labels")
|
131
|
+
|
132
|
+
puts JSON.pretty_generate(response) if ENV['DEBUG']
|
133
|
+
|
134
|
+
response.to_a.map { |lang, val| Wikidatum::Term.new(lang: lang, value: val) }
|
135
|
+
end
|
136
|
+
|
96
137
|
# Get a statement from the Wikibase API based on its ID.
|
97
138
|
#
|
98
139
|
# @example
|
@@ -195,9 +236,36 @@ module Wikidatum
|
|
195
236
|
# )
|
196
237
|
# )
|
197
238
|
#
|
239
|
+
# @example Add a URL statement.
|
240
|
+
# wikidatum_client.add_statement(
|
241
|
+
# id: 'Q123',
|
242
|
+
# property: 'P124',
|
243
|
+
# value: Wikidatum::DataType::WikibaseUrl.new(
|
244
|
+
# string: 'https://example.com'
|
245
|
+
# )
|
246
|
+
# )
|
247
|
+
#
|
248
|
+
# @example Add an External ID statement.
|
249
|
+
# wikidatum_client.add_statement(
|
250
|
+
# id: 'Q123',
|
251
|
+
# property: 'P124',
|
252
|
+
# value: Wikidatum::DataType::ExternalId.new(
|
253
|
+
# string: '123'
|
254
|
+
# )
|
255
|
+
# )
|
256
|
+
#
|
257
|
+
# @example Add a statement for an image, video, or audio file from Wikimedia Commons.
|
258
|
+
# wikidatum_client.add_statement(
|
259
|
+
# id: 'Q123',
|
260
|
+
# property: 'P124',
|
261
|
+
# value: Wikidatum::DataType::CommonsMedia.new(
|
262
|
+
# string: 'FooBar.jpg'
|
263
|
+
# )
|
264
|
+
# )
|
265
|
+
#
|
198
266
|
# @param id [String, Integer] the ID of the item on which the statement will be added.
|
199
267
|
# @param property [String, Integer] property ID in the format 'P123', or an integer.
|
200
|
-
# @param value [Wikidatum::DataType::GlobeCoordinate, Wikidatum::DataType::MonolingualText, Wikidatum::DataType::Quantity, Wikidatum::DataType::WikibaseString, Wikidatum::DataType::Time, Wikidatum::DataType::WikibaseItem, Wikidatum::DataType::NoValue, Wikidatum::DataType::SomeValue] the value of the statement being created.
|
268
|
+
# @param value [Wikidatum::DataType::CommonsMedia, Wikidatum::DataType::ExternalId, Wikidatum::DataType::GlobeCoordinate, Wikidatum::DataType::MonolingualText, Wikidatum::DataType::Quantity, Wikidatum::DataType::WikibaseString, Wikidatum::DataType::Time, Wikidatum::DataType::WikibaseItem, Wikidatum::DataType::WikibaseUrl, Wikidatum::DataType::NoValue, Wikidatum::DataType::SomeValue] the value of the statement being created.
|
201
269
|
# @param qualifiers [Array<Wikidatum::Qualifier>]
|
202
270
|
# @param references [Array<Wikidatum::Reference>]
|
203
271
|
# @param rank [String, Symbol] Valid ranks are 'preferred', 'normal', or
|
@@ -233,7 +301,7 @@ module Wikidatum
|
|
233
301
|
type: 'somevalue'
|
234
302
|
}
|
235
303
|
}
|
236
|
-
when
|
304
|
+
when *CONTENT_DATA_TYPES
|
237
305
|
statement_hash = {
|
238
306
|
property: {
|
239
307
|
id: property
|
@@ -446,10 +514,10 @@ module Wikidatum
|
|
446
514
|
# error message if so.
|
447
515
|
#
|
448
516
|
# @return [void]
|
449
|
-
# @raise [DisallowedIpEditError, DisallowedBotEditError]
|
517
|
+
# @raise [Errors::DisallowedIpEditError, Errors::DisallowedBotEditError]
|
450
518
|
def ensure_edit_permitted!
|
451
|
-
raise DisallowedIpEditError if !authenticated? && !allow_ip_edits?
|
452
|
-
raise DisallowedBotEditError if !authenticated? && bot?
|
519
|
+
raise Errors::DisallowedIpEditError if !authenticated? && !allow_ip_edits?
|
520
|
+
raise Errors::DisallowedBotEditError if !authenticated? && bot?
|
453
521
|
end
|
454
522
|
end
|
455
523
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# For more information on the possible types that can be returned by
|
4
4
|
# datavalues, see the official documentation:
|
5
|
-
# https://doc.wikimedia.org/Wikibase/master/php/
|
5
|
+
# https://doc.wikimedia.org/Wikibase/master/php/docs_topics_json.html#json_datavalues
|
6
6
|
module Wikidatum::DataType
|
7
7
|
class Base
|
8
8
|
# Represents the type for this instance.
|
@@ -11,11 +11,14 @@ module Wikidatum::DataType
|
|
11
11
|
#
|
12
12
|
# - `:no_value`: No value
|
13
13
|
# - `:some_value`: Unknown value
|
14
|
+
# - `:commons_media`: {DataType::CommonsMedia}
|
15
|
+
# - `:external_id`: {DataType::ExternalId}
|
14
16
|
# - `:globe_coordinate`: {DataType::GlobeCoordinate}
|
15
17
|
# - `:monolingual_text`: {DataType::MonolingualText}
|
16
18
|
# - `:quantity`: {DataType::Quantity}
|
17
19
|
# - `:string`: {DataType::WikibaseString}
|
18
20
|
# - `:time`: {DataType::Time}
|
21
|
+
# - `:url`: {DataType::WikibaseUrl}
|
19
22
|
# - `:wikibase_item`: {DataType::WikibaseItem}
|
20
23
|
#
|
21
24
|
# @return [Symbol]
|
@@ -23,13 +26,13 @@ module Wikidatum::DataType
|
|
23
26
|
|
24
27
|
# The value of the "content" attribute in the response.
|
25
28
|
#
|
26
|
-
# If the `type` is `
|
29
|
+
# If the `type` is `:no_value` or `:some_value`, this returns `nil`.
|
27
30
|
#
|
28
|
-
# @return [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::
|
31
|
+
# @return [DataType::CommonsMedia, DataType::ExternalId, DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::Time, DataType::WikibaseItem, DataType::WikibaseString, DataType::WikibaseUrl, nil]
|
29
32
|
attr_reader :content
|
30
33
|
|
31
34
|
# @param type [Symbol]
|
32
|
-
# @param content [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::
|
35
|
+
# @param content [DataType::CommonsMedia, DataType::ExternalId, DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::Time, DataType::WikibaseItem, DataType::WikibaseString, DataType::WikibaseUrl, nil] nil if type is no_value or some_value
|
33
36
|
# @return [void]
|
34
37
|
def initialize(type:, content:)
|
35
38
|
@type = type
|
@@ -44,6 +47,14 @@ module Wikidatum::DataType
|
|
44
47
|
}
|
45
48
|
end
|
46
49
|
|
50
|
+
# Get the humanized version of the content. Depending on the given data
|
51
|
+
# type, it will return values in different formats.
|
52
|
+
#
|
53
|
+
# @return [String, DataType::GlobeCoordinate::HumanizedStruct, DataType::MonolingualText::HumanizedStruct, DataType::Quantity::HumanizedStruct, DataType::Time::HumanizedStruct, nil]
|
54
|
+
def humanized_content
|
55
|
+
@content.humanized
|
56
|
+
end
|
57
|
+
|
47
58
|
# @!visibility private
|
48
59
|
#
|
49
60
|
# @param data_type [String] The value of `data-type` for the given Statement or Qualifier's property.
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/data_type/base'
|
4
|
+
require 'wikidatum/data_type/wikibase_string'
|
5
|
+
|
6
|
+
# Identical to the WikibaseString type, but we'll change some metadata to make
|
7
|
+
# it clear that they're technically distinct types.
|
8
|
+
class Wikidatum::DataType::CommonsMedia < Wikidatum::DataType::WikibaseString
|
9
|
+
# The "type" value used by Wikibase, for use when creating/updating statements.
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
def wikibase_type
|
13
|
+
'commonsMedia'
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Symbol]
|
17
|
+
def self.symbolized_name
|
18
|
+
:commons_media
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/data_type/base'
|
4
|
+
require 'wikidatum/data_type/wikibase_string'
|
5
|
+
|
6
|
+
# Identical to the WikibaseString type, but we'll change some metadata to make
|
7
|
+
# it clear that they're technically distinct types.
|
8
|
+
class Wikidatum::DataType::ExternalId < Wikidatum::DataType::WikibaseString
|
9
|
+
# The "type" value used by Wikibase, for use when creating/updating statements.
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
def wikibase_type
|
13
|
+
'external-id'
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Symbol]
|
17
|
+
def self.symbolized_name
|
18
|
+
:external_id
|
19
|
+
end
|
20
|
+
end
|
@@ -63,9 +63,19 @@ class Wikidatum::DataType::GlobeCoordinate
|
|
63
63
|
'globe-coordinate'
|
64
64
|
end
|
65
65
|
|
66
|
+
HumanizedStruct = Struct.new(:latitude, :longitude, :precision, :globe, keyword_init: true)
|
67
|
+
|
68
|
+
# The content of the data value object. Use this to get a more sensible
|
69
|
+
# representation of the statement's contents.
|
70
|
+
#
|
71
|
+
# @return [HumanizedStruct<latitude, longitude, precision, globe>]
|
72
|
+
def humanized
|
73
|
+
HumanizedStruct.new(to_h)
|
74
|
+
end
|
75
|
+
|
66
76
|
# @return [Symbol]
|
67
77
|
def self.symbolized_name
|
68
|
-
:
|
78
|
+
:globe_coordinate
|
69
79
|
end
|
70
80
|
|
71
81
|
# @!visibility private
|
@@ -49,6 +49,16 @@ class Wikidatum::DataType::MonolingualText
|
|
49
49
|
'monolingualtext'
|
50
50
|
end
|
51
51
|
|
52
|
+
HumanizedStruct = Struct.new(:language, :text, keyword_init: true)
|
53
|
+
|
54
|
+
# The content of the data value object. Use this to get a more sensible
|
55
|
+
# representation of the statement's contents.
|
56
|
+
#
|
57
|
+
# @return [HumanizedStruct<language, text>]
|
58
|
+
def humanized
|
59
|
+
HumanizedStruct.new(to_h)
|
60
|
+
end
|
61
|
+
|
52
62
|
# @return [Symbol]
|
53
63
|
def self.symbolized_name
|
54
64
|
:monolingual_text
|
@@ -9,11 +9,21 @@ require 'wikidatum/data_type/base'
|
|
9
9
|
class Wikidatum::DataType::NoValue < Wikidatum::DataType::Base
|
10
10
|
# The "type" value used by Wikibase, for use when creating/updating statements.
|
11
11
|
#
|
12
|
+
# In this case, the type is a lie.
|
13
|
+
#
|
12
14
|
# @return [String]
|
13
15
|
def wikibase_type
|
14
16
|
'string'
|
15
17
|
end
|
16
18
|
|
19
|
+
# The content of the data value object. Use this to get a more sensible
|
20
|
+
# representation of the statement's contents.
|
21
|
+
#
|
22
|
+
# @return [nil]
|
23
|
+
def humanized
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
17
27
|
# @!visibility private
|
18
28
|
def self.marshal_load(_data_value_json)
|
19
29
|
new(type: :no_value, content: nil)
|
@@ -49,6 +49,16 @@ class Wikidatum::DataType::Quantity
|
|
49
49
|
'quantity'
|
50
50
|
end
|
51
51
|
|
52
|
+
HumanizedStruct = Struct.new(:amount, :unit, keyword_init: true)
|
53
|
+
|
54
|
+
# The content of the data value object. Use this to get a more sensible
|
55
|
+
# representation of the statement's contents.
|
56
|
+
#
|
57
|
+
# @return [HumanizedStruct<amount, unit>]
|
58
|
+
def humanized
|
59
|
+
HumanizedStruct.new(to_h)
|
60
|
+
end
|
61
|
+
|
52
62
|
# @return [Symbol]
|
53
63
|
def self.symbolized_name
|
54
64
|
:quantity
|
@@ -9,11 +9,21 @@ require 'wikidatum/data_type/base'
|
|
9
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
|
+
# In this case, the type is a lie.
|
13
|
+
#
|
12
14
|
# @return [String]
|
13
15
|
def wikibase_type
|
14
16
|
'string'
|
15
17
|
end
|
16
18
|
|
19
|
+
# The content of the data value object. Use this to get a more sensible
|
20
|
+
# representation of the statement's contents.
|
21
|
+
#
|
22
|
+
# @return [nil]
|
23
|
+
def humanized
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
17
27
|
# @!visibility private
|
18
28
|
def self.marshal_load(_data_value_json)
|
19
29
|
new(type: :some_value, content: nil)
|
@@ -109,6 +109,16 @@ class Wikidatum::DataType::Time
|
|
109
109
|
'time'
|
110
110
|
end
|
111
111
|
|
112
|
+
HumanizedStruct = Struct.new(:time, :precision, :calendar_model, keyword_init: true)
|
113
|
+
|
114
|
+
# The content of the data value object. Use this to get a more sensible
|
115
|
+
# representation of the statement's contents.
|
116
|
+
#
|
117
|
+
# @return [HumanizedStruct<time, precision, pretty_precision, calendar_model>]
|
118
|
+
def humanized
|
119
|
+
HumanizedStruct.new(to_h)
|
120
|
+
end
|
121
|
+
|
112
122
|
# @return [Symbol]
|
113
123
|
def self.symbolized_name
|
114
124
|
:time
|
@@ -40,6 +40,14 @@ class Wikidatum::DataType::WikibaseItem
|
|
40
40
|
'wikibase-item'
|
41
41
|
end
|
42
42
|
|
43
|
+
# The content of the data value object. Use this to get a more sensible
|
44
|
+
# representation of the statement's contents.
|
45
|
+
#
|
46
|
+
# @return [String]
|
47
|
+
def humanized
|
48
|
+
@id
|
49
|
+
end
|
50
|
+
|
43
51
|
# @return [Symbol]
|
44
52
|
def self.symbolized_name
|
45
53
|
:wikibase_item
|
@@ -40,6 +40,14 @@ class Wikidatum::DataType::WikibaseString
|
|
40
40
|
'string'
|
41
41
|
end
|
42
42
|
|
43
|
+
# The content of the data value object. Use this to get a more sensible
|
44
|
+
# representation of the statement's contents.
|
45
|
+
#
|
46
|
+
# @return [String]
|
47
|
+
def humanized
|
48
|
+
@string
|
49
|
+
end
|
50
|
+
|
43
51
|
# @return [Symbol]
|
44
52
|
def self.symbolized_name
|
45
53
|
:string
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/data_type/base'
|
4
|
+
require 'wikidatum/data_type/wikibase_string'
|
5
|
+
|
6
|
+
# Identical to the WikibaseString type, but we'll change some metadata to make
|
7
|
+
# it clear that they're technically distinct types.
|
8
|
+
class Wikidatum::DataType::WikibaseUrl < Wikidatum::DataType::WikibaseString
|
9
|
+
# The "type" value used by Wikibase, for use when creating/updating statements.
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
def wikibase_type
|
13
|
+
'url'
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Symbol]
|
17
|
+
def self.symbolized_name
|
18
|
+
:url
|
19
|
+
end
|
20
|
+
end
|
data/lib/wikidatum/data_type.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'wikidatum/data_type/base'
|
4
|
+
require 'wikidatum/data_type/commons_media'
|
5
|
+
require 'wikidatum/data_type/external_id'
|
4
6
|
require 'wikidatum/data_type/globe_coordinate'
|
5
7
|
require 'wikidatum/data_type/monolingual_text'
|
6
8
|
require 'wikidatum/data_type/no_value'
|
@@ -9,3 +11,4 @@ require 'wikidatum/data_type/some_value'
|
|
9
11
|
require 'wikidatum/data_type/time'
|
10
12
|
require 'wikidatum/data_type/wikibase_item'
|
11
13
|
require 'wikidatum/data_type/wikibase_string'
|
14
|
+
require 'wikidatum/data_type/wikibase_url'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'wikidatum/error'
|
4
|
+
|
5
|
+
module Wikidatum::Errors
|
6
|
+
# If the Wikidatum::Client is set to disallow IP Edits (the default) and no
|
7
|
+
# authentication has been provided, this error will be raised when performing
|
8
|
+
# any non-GET requests.
|
9
|
+
class DisallowedIpEditError < Wikidatum::Error
|
10
|
+
def message
|
11
|
+
'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`.'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# If the Wikidatum::Client is set to mark their edits as belonging to a bot,
|
16
|
+
# they must be authenticated as a bot user. We will disallow these edits
|
17
|
+
# as long as they're not authenticated as a specific user.
|
18
|
+
class DisallowedBotEditError < Wikidatum::Error
|
19
|
+
def message
|
20
|
+
'No authentication provided, but attempted to edit as a bot. You cannot make edits as a bot unless you have authenticated as a user with the Bot flag.'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/wikidatum/qualifier.rb
CHANGED
@@ -9,7 +9,7 @@ class Wikidatum::Qualifier
|
|
9
9
|
|
10
10
|
# For more information on the possible types that can be returned by
|
11
11
|
# datavalues, see the official documentation:
|
12
|
-
# https://doc.wikimedia.org/Wikibase/master/php/
|
12
|
+
# https://doc.wikimedia.org/Wikibase/master/php/docs_topics_json.html#json_datavalues
|
13
13
|
#
|
14
14
|
# @return [Wikidatum::DataType::Base] the value of the statement, can take various forms
|
15
15
|
attr_reader :value
|
@@ -9,7 +9,7 @@ class Wikidatum::ReferencePart
|
|
9
9
|
|
10
10
|
# For more information on the possible types that can be returned by
|
11
11
|
# datavalues, see the official documentation:
|
12
|
-
# https://doc.wikimedia.org/Wikibase/master/php/
|
12
|
+
# https://doc.wikimedia.org/Wikibase/master/php/docs_topics_json.html#json_datavalues
|
13
13
|
#
|
14
14
|
# @return [Wikidatum::DataType::Base] the value of the statement, can take various forms
|
15
15
|
attr_reader :value
|
@@ -17,7 +17,7 @@ class Wikidatum::ReferencePart
|
|
17
17
|
# @!visibility private
|
18
18
|
# @param property_id [String] ID of the property used, in the format "P123".
|
19
19
|
# @param data_type [String]
|
20
|
-
# @param value [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::
|
20
|
+
# @param value [DataType::CommonsMedia, DataType::ExternalId, DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::Time, DataType::WikibaseItem, DataType::WikibaseString, DataType::WikibaseUrl, DataType::NoValue, DataType::SomeValue]
|
21
21
|
def initialize(property_id:, data_type:, value:)
|
22
22
|
@property_id = property_id
|
23
23
|
@data_type = data_type
|
data/lib/wikidatum/sitelink.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Wikidatum::Sitelinks represent associated sitelinks on a Wikidata item
|
3
|
+
# Wikidatum::Sitelinks represent associated sitelinks on a Wikidata item, for
|
4
|
+
# example the associated English Wikipedia article.
|
4
5
|
class Wikidatum::Sitelink
|
5
|
-
# @return [String]
|
6
|
+
# @return [String] The shortcode for the given site (e.g. 'enwiki', 'commons', etc.).
|
6
7
|
attr_reader :site
|
7
8
|
|
8
|
-
# @return [String]
|
9
|
+
# @return [String] The title of the page in the associated Wikimedia site.
|
9
10
|
attr_reader :title
|
10
11
|
|
11
|
-
# @return [Array<String>]
|
12
|
+
# @return [Array<String>] An array of badges, given as item IDs (e.g. `['Q123', 'Q124']`).
|
12
13
|
attr_reader :badges
|
13
14
|
|
14
15
|
# @!visibility private
|
15
16
|
#
|
16
17
|
# @param site [String, Symbol] The shortcode for the given site (e.g. 'enwiki', 'commons', etc.), can be either a string or a symbol.
|
17
18
|
# @param title [String] The title of the page in the associated Wikimedia site.
|
18
|
-
# @param badges [Array<String>] An array of badges, given as item IDs (e.g. `['Q123', 'Q124']`) optional.
|
19
|
+
# @param badges [Array<String>] An array of badges, given as item IDs (e.g. `['Q123', 'Q124']`). This parameter is optional.
|
19
20
|
# @return [Wikidatum::Sitelink]
|
20
21
|
def initialize(site:, title:, badges: [])
|
21
22
|
@site = site.to_s
|
data/lib/wikidatum/statement.rb
CHANGED
@@ -10,7 +10,7 @@ class Wikidatum::Statement
|
|
10
10
|
# @return [String]
|
11
11
|
attr_accessor :data_type
|
12
12
|
|
13
|
-
# @return [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::
|
13
|
+
# @return [DataType::CommonsMedia, DataType::ExternalId, DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::Time, DataType::WikibaseItem, DataType::WikibaseString, DataType::WikibaseUrl, nil]
|
14
14
|
attr_accessor :data_value
|
15
15
|
|
16
16
|
# @return [Array<Wikidatum::Qualifier>]
|
@@ -26,7 +26,7 @@ class Wikidatum::Statement
|
|
26
26
|
# @param id [String]
|
27
27
|
# @param property_id [String] The 'P123' ID of the property that this statement represents.
|
28
28
|
# @param data_type [String]
|
29
|
-
# @param data_value [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::
|
29
|
+
# @param data_value [DataType::CommonsMedia, DataType::ExternalId, DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::Time, DataType::WikibaseItem, DataType::WikibaseString, DataType::WikibaseUrl, nil]
|
30
30
|
# @param qualifiers [Array<Wikidatum::Qualifier>]
|
31
31
|
# @param references [Array<Wikidatum::Reference>]
|
32
32
|
# @param rank [String] The rank of the given statement.
|
data/lib/wikidatum/term.rb
CHANGED
@@ -3,16 +3,16 @@
|
|
3
3
|
# Wikidatum::Term represents "Terms", which are pairs of language codes and
|
4
4
|
# values. They're used for things like item labels, descriptions, and aliases.
|
5
5
|
class Wikidatum::Term
|
6
|
-
# @return [String]
|
6
|
+
# @return [String] A language code ('en', 'zh-mo', etc).
|
7
7
|
attr_reader :lang
|
8
8
|
|
9
|
-
# @return [String]
|
9
|
+
# @return [String] The value of the Term.
|
10
10
|
attr_reader :value
|
11
11
|
|
12
12
|
# @!visibility private
|
13
13
|
#
|
14
14
|
# @param lang [String, Symbol] A language code ('en', 'zh-mo', etc), can be a symbol or a string.
|
15
|
-
# @param value [String] The value of the
|
15
|
+
# @param value [String] The value of the Term.
|
16
16
|
# @return [Wikidatum::Term]
|
17
17
|
def initialize(lang:, value:)
|
18
18
|
@lang = lang.to_s
|
data/lib/wikidatum/version.rb
CHANGED
data/lib/wikidatum.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'wikidatum/client'
|
4
4
|
require 'wikidatum/data_type'
|
5
|
+
require 'wikidatum/error'
|
6
|
+
require 'wikidatum/errors'
|
5
7
|
require 'wikidatum/item'
|
6
8
|
require 'wikidatum/qualifier'
|
7
9
|
require 'wikidatum/reference'
|
@@ -13,26 +15,6 @@ require 'wikidatum/utils'
|
|
13
15
|
require 'wikidatum/version'
|
14
16
|
|
15
17
|
module Wikidatum
|
16
|
-
class Error < StandardError; end
|
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
|
-
|
27
|
-
# If the Wikidatum::Client is set to mark their edits as belonging to a bot,
|
28
|
-
# they must be authenticated as a bot user. We will disallow these edits
|
29
|
-
# as long as they're not authenticated as a specific user.
|
30
|
-
class DisallowedBotEditError < Error
|
31
|
-
def message
|
32
|
-
'No authentication provided, but attempted to edit as a bot. You cannot make edits as a bot unless you have authenticated as a user with the Bot flag.'
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
18
|
# rubocop:disable Naming/ConstantName
|
37
19
|
|
38
20
|
# These language codes are not enforced, you can pass whatever language code
|
@@ -65,9 +47,10 @@ module Wikidatum
|
|
65
47
|
end
|
66
48
|
|
67
49
|
module DataType
|
68
|
-
# TODO: Add commonsMedia and url to the possible types.
|
69
50
|
# rubocop:disable Lint/SymbolConversion
|
70
51
|
DATA_TYPES = {
|
52
|
+
'commonsMedia': 'Wikidatum::DataType::CommonsMedia',
|
53
|
+
'external-id': 'Wikidatum::DataType::ExternalId',
|
71
54
|
'globe-coordinate': 'Wikidatum::DataType::GlobeCoordinate',
|
72
55
|
'monolingualtext': 'Wikidatum::DataType::MonolingualText',
|
73
56
|
'novalue': 'Wikidatum::DataType::NoValue',
|
@@ -75,6 +58,7 @@ module Wikidatum
|
|
75
58
|
'somevalue': 'Wikidatum::DataType::SomeValue',
|
76
59
|
'string': 'Wikidatum::DataType::WikibaseString',
|
77
60
|
'time': 'Wikidatum::DataType::Time',
|
61
|
+
'url': 'Wikidatum::DataType::WikibaseUrl',
|
78
62
|
'wikibase-item': 'Wikidatum::DataType::WikibaseItem'
|
79
63
|
}.freeze
|
80
64
|
# rubocop:enable Lint/SymbolConversion
|
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.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Connor Shea
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -40,6 +40,8 @@ files:
|
|
40
40
|
- lib/wikidatum/client.rb
|
41
41
|
- lib/wikidatum/data_type.rb
|
42
42
|
- lib/wikidatum/data_type/base.rb
|
43
|
+
- lib/wikidatum/data_type/commons_media.rb
|
44
|
+
- lib/wikidatum/data_type/external_id.rb
|
43
45
|
- lib/wikidatum/data_type/globe_coordinate.rb
|
44
46
|
- lib/wikidatum/data_type/monolingual_text.rb
|
45
47
|
- lib/wikidatum/data_type/no_value.rb
|
@@ -48,6 +50,9 @@ files:
|
|
48
50
|
- lib/wikidatum/data_type/time.rb
|
49
51
|
- lib/wikidatum/data_type/wikibase_item.rb
|
50
52
|
- lib/wikidatum/data_type/wikibase_string.rb
|
53
|
+
- lib/wikidatum/data_type/wikibase_url.rb
|
54
|
+
- lib/wikidatum/error.rb
|
55
|
+
- lib/wikidatum/errors.rb
|
51
56
|
- lib/wikidatum/item.rb
|
52
57
|
- lib/wikidatum/qualifier.rb
|
53
58
|
- lib/wikidatum/reference.rb
|