wikidatum 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/bin/console +0 -4
- data/lib/wikidatum/client.rb +330 -328
- data/lib/wikidatum/version.rb +1 -1
- data/lib/wikidatum.rb +10 -10
- data/wikidatum.gemspec +1 -1
- metadata +1 -4
- data/Gemfile +0 -13
- data/Gemfile.lock +0 -73
- data/Rakefile +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50ad5328f1593f1de47d492e6c4e761d80d3e43bb967bcf9386f72053881b705
|
4
|
+
data.tar.gz: eb17d97dfdbe7c45c2b10bc7d34aeeb76136b3473c72817f7a1b053f0e2d16eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6cb6a7968e9f8943b6b6cd06311d74139a8580dde6e925a83cdf891292d5e6028a63b027394af2b04b31cadc78af88170f3a3fd3c92c2d4aeaca72560048402
|
7
|
+
data.tar.gz: d27a8240fec4fd41caa3ec21ce7bab706fed304bd364b076c741b7577bc04ce1fdd3df5db1675a5cbc431872ad1fe69f7f7b035a696b6212a38a97afec3bc2c6
|
data/CHANGELOG.md
CHANGED
data/bin/console
CHANGED
@@ -7,9 +7,5 @@ require "wikidatum"
|
|
7
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
8
8
|
# with your gem easier. You can also use a different console, if you like.
|
9
9
|
|
10
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
-
# require "pry"
|
12
|
-
# Pry.start
|
13
|
-
|
14
10
|
require "irb"
|
15
11
|
IRB.start(__FILE__)
|
data/lib/wikidatum/client.rb
CHANGED
@@ -3,368 +3,370 @@
|
|
3
3
|
require 'faraday'
|
4
4
|
require 'faraday/net_http'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
6
|
+
module Wikidatum
|
7
|
+
class Client
|
8
|
+
ITEM_REGEX = /^Q?\d+$/.freeze
|
9
|
+
STATEMENT_REGEX = /^Q?\d+\$[\w-]+$/.freeze
|
10
|
+
|
11
|
+
# @return [String] the root URL of the Wikibase instance we want to interact
|
12
|
+
# with. If not provided, will default to Wikidata.
|
13
|
+
attr_reader :wikibase_url
|
14
|
+
|
15
|
+
# @return [Boolean] whether this client instance should identify itself
|
16
|
+
# as a bot when making requests.
|
17
|
+
attr_reader :bot
|
18
|
+
|
19
|
+
# @return [String] the UserAgent header to send with all requests to the
|
20
|
+
# Wikibase API.
|
21
|
+
attr_reader :user_agent
|
22
|
+
|
23
|
+
# Create a new Wikidatum::Client to interact with the Wikibase REST API.
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# wikidatum_client = Wikidatum::Client.new(
|
27
|
+
# user_agent: 'REPLACE ME WITH THE NAME OF YOUR BOT!',
|
28
|
+
# wikibase_url: 'https://www.wikidata.org',
|
29
|
+
# bot: true
|
30
|
+
# )
|
31
|
+
#
|
32
|
+
# @param user_agent [String] The UserAgent header to send with all requests
|
33
|
+
# to the Wikibase API.
|
34
|
+
# @param wikibase_url [String] The root URL of the Wikibase instance we want
|
35
|
+
# to interact with. If not provided, will default to
|
36
|
+
# `https://www.wikidata.org`. Do not include a `/` at the end of the URL.
|
37
|
+
# @param bot [Boolean] Whether requests sent by this client instance should
|
38
|
+
# be registered as bot requests. Defaults to `true`.
|
39
|
+
# @return [Wikidatum::Client]
|
40
|
+
def initialize(user_agent:, wikibase_url: 'https://www.wikidata.org', bot: true)
|
41
|
+
raise ArgumentError, "Wikibase URL must not end with a `/`, got #{wikibase_url.inspect}." if wikibase_url.end_with?('/')
|
42
|
+
|
43
|
+
# TODO: Add the Ruby gem version to the UserAgent automatically, and
|
44
|
+
# restrict the ability for end-users to actually set the UserAgent?
|
45
|
+
@user_agent = user_agent
|
46
|
+
@wikibase_url = wikibase_url
|
47
|
+
@bot = bot
|
48
|
+
|
49
|
+
Faraday.default_adapter = :net_http
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
52
|
+
# Get an item from the Wikibase API based on its QID.
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# wikidatum_client.item(id: 'Q123')
|
56
|
+
# wikidatum_client.item(id: 123)
|
57
|
+
# wikidatum_client.item(id: '123')
|
58
|
+
#
|
59
|
+
# @param id [String, Integer] Either a string or integer representation of
|
60
|
+
# the item's QID, e.g. `"Q123"`, `"123"`, or `123`.
|
61
|
+
# @return [Wikidatum::Item]
|
62
|
+
def item(id:)
|
63
|
+
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)
|
63
64
|
|
64
|
-
|
65
|
+
id = coerce_item_id(id)
|
65
66
|
|
66
|
-
|
67
|
+
response = get_request("/entities/items/#{id}")
|
67
68
|
|
68
|
-
|
69
|
+
puts JSON.pretty_generate(response) if ENV['DEBUG']
|
69
70
|
|
70
|
-
|
71
|
-
|
71
|
+
Wikidatum::Item.marshal_load(response)
|
72
|
+
end
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
74
|
+
# Get a statement from the Wikibase API based on its ID.
|
75
|
+
#
|
76
|
+
# @example
|
77
|
+
# wikidatum_client.statement(id: 'Q123$f004ec2b-4857-3b69-b370-e8124f5bd3ac')
|
78
|
+
#
|
79
|
+
# @param id [String] A string representation of the statement's ID.
|
80
|
+
# @return [Wikidatum::Statement]
|
81
|
+
def statement(id:)
|
82
|
+
raise ArgumentError, "#{id.inspect} is an invalid Wikibase Statement ID. Must be a string in the format 'Q123$f004ec2b-4857-3b69-b370-e8124f5bd3ac'." unless id.match?(STATEMENT_REGEX)
|
82
83
|
|
83
|
-
|
84
|
+
response = get_request("/statements/#{id}")
|
84
85
|
|
85
|
-
|
86
|
+
puts JSON.pretty_generate(response) if ENV['DEBUG']
|
86
87
|
|
87
|
-
|
88
|
-
|
88
|
+
Wikidatum::Statement.marshal_load(response)
|
89
|
+
end
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
91
|
+
# Add a statement to an item.
|
92
|
+
#
|
93
|
+
# NOTE: Adding references/qualifiers with `add_statement` is untested and
|
94
|
+
# effectively unsupported for now.
|
95
|
+
#
|
96
|
+
# @example Add a string statement.
|
97
|
+
# wikidatum_client.add_statement(
|
98
|
+
# id: 'Q123',
|
99
|
+
# property: 'P23',
|
100
|
+
# datavalue: Wikidatum::DataValueType::WikibaseString.new(string: 'Foo'),
|
101
|
+
# comment: 'Adding something or another.'
|
102
|
+
# )
|
103
|
+
#
|
104
|
+
# @example Add a 'no value' statement.
|
105
|
+
# wikidatum_client.add_statement(
|
106
|
+
# id: 'Q123',
|
107
|
+
# property: 'P124',
|
108
|
+
# datavalue: Wikidatum::DataValueType::NoValue.new(
|
109
|
+
# type: :no_value,
|
110
|
+
# value: nil
|
111
|
+
# )
|
112
|
+
# )
|
113
|
+
#
|
114
|
+
# @example Add an 'unknown value' statement.
|
115
|
+
# wikidatum_client.add_statement(
|
116
|
+
# id: 'Q123',
|
117
|
+
# property: 'P124',
|
118
|
+
# datavalue: Wikidatum::DataValueType::SomeValue.new(
|
119
|
+
# type: :some_value,
|
120
|
+
# value: nil
|
121
|
+
# )
|
122
|
+
# )
|
123
|
+
#
|
124
|
+
# @example Add a globe coordinate statement.
|
125
|
+
# wikidatum_client.add_statement(
|
126
|
+
# id: 'Q123',
|
127
|
+
# property: 'P124',
|
128
|
+
# datavalue: Wikidatum::DataValueType::GlobeCoordinate.new(
|
129
|
+
# latitude: 52.51666,
|
130
|
+
# longitude: 13.3833,
|
131
|
+
# precision: 0.01666,
|
132
|
+
# globe: 'https://wikidata.org/entity/Q2'
|
133
|
+
# )
|
134
|
+
# )
|
135
|
+
#
|
136
|
+
# @example Add a monolingual text statement.
|
137
|
+
# wikidatum_client.add_statement(
|
138
|
+
# id: 'Q123',
|
139
|
+
# property: 'P124',
|
140
|
+
# datavalue: Wikidatum::DataValueType::MonolingualText.new(
|
141
|
+
# language: 'en',
|
142
|
+
# text: 'Foobar'
|
143
|
+
# )
|
144
|
+
# )
|
145
|
+
#
|
146
|
+
# @example Add a quantity statement.
|
147
|
+
# wikidatum_client.add_statement(
|
148
|
+
# id: 'Q123',
|
149
|
+
# property: 'P124',
|
150
|
+
# datavalue: Wikidatum::DataValueType::Quantity.new(
|
151
|
+
# amount: '+12',
|
152
|
+
# upper_bound: nil,
|
153
|
+
# lower_bound: nil,
|
154
|
+
# unit: 'https://wikidata.org/entity/Q1234'
|
155
|
+
# )
|
156
|
+
# )
|
157
|
+
#
|
158
|
+
# @example Add a time statement.
|
159
|
+
# wikidatum_client.add_statement(
|
160
|
+
# id: 'Q123',
|
161
|
+
# property: 'P124',
|
162
|
+
# datavalue: Wikidatum::DataValueType::Time.new(
|
163
|
+
# time: '+2022-08-12T00:00:00Z',
|
164
|
+
# time_zone: 0,
|
165
|
+
# precision: 11,
|
166
|
+
# calendar_model: 'https://wikidata.org/entity/Q1234'
|
167
|
+
# )
|
168
|
+
# )
|
169
|
+
#
|
170
|
+
# @example Add a Wikibase item statement.
|
171
|
+
# wikidatum_client.add_statement(
|
172
|
+
# id: 'Q123',
|
173
|
+
# property: 'P124',
|
174
|
+
# datavalue: Wikidatum::DataValueType::WikibaseEntityId.new(
|
175
|
+
# entity_type: 'item',
|
176
|
+
# numeric_id: 1234,
|
177
|
+
# id: 'Q1234'
|
178
|
+
# )
|
179
|
+
# )
|
180
|
+
#
|
181
|
+
# @param id [String] the ID of the item on which the statement will be added.
|
182
|
+
# @param property [String] property ID in the format 'P123'.
|
183
|
+
# @param datavalue [Wikidatum::DataValueType::GlobeCoordinate, Wikidatum::DataValueType::MonolingualText, Wikidatum::DataValueType::Quantity, Wikidatum::DataValueType::WikibaseString, Wikidatum::DataValueType::Time, Wikidatum::DataValueType::WikibaseEntityId, Wikidatum::DataValueType::NoValue, Wikidatum::DataValueType::SomeValue] the datavalue of the statement being created.
|
184
|
+
# @param datatype [String, nil] if nil, it'll determine the type based on what was passed for the statement argument. This may differ from the type of the Statement's datavalue (for example with the 'url' type).
|
185
|
+
# @param qualifiers [Hash<String, Array<Wikidatum::Snak>>]
|
186
|
+
# @param references [Array<Wikidatum::Reference>]
|
187
|
+
# @param rank [String]
|
188
|
+
# @param tags [Array<String>]
|
189
|
+
# @param comment [String, nil]
|
190
|
+
# @return [Boolean] True if the request succeeded.
|
191
|
+
def add_statement(id:, property:, datavalue:, datatype: nil, qualifiers: {}, references: [], rank: 'normal', tags: [], comment: nil)
|
192
|
+
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)
|
193
|
+
|
194
|
+
id = coerce_item_id(id)
|
195
|
+
|
196
|
+
# Unless datatype is set explicitly by the caller, just assume we can pull the
|
197
|
+
# default from the datavalue class.
|
198
|
+
datatype ||= datavalue.wikibase_datatype
|
199
|
+
|
200
|
+
case datavalue.class.to_s
|
201
|
+
when 'Wikidatum::DataValueType::NoValue'
|
202
|
+
statement_hash = {
|
203
|
+
mainsnak: {
|
204
|
+
snaktype: 'novalue',
|
205
|
+
property: property,
|
206
|
+
datatype: datatype
|
207
|
+
}
|
206
208
|
}
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
209
|
+
when 'Wikidatum::DataValueType::SomeValue'
|
210
|
+
statement_hash = {
|
211
|
+
mainsnak: {
|
212
|
+
snaktype: 'somevalue',
|
213
|
+
property: property,
|
214
|
+
datatype: datatype
|
215
|
+
}
|
214
216
|
}
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
217
|
+
when 'Wikidatum::DataValueType::GlobeCoordinate', 'Wikidatum::DataValueType::MonolingualText', 'Wikidatum::DataValueType::Quantity', 'Wikidatum::DataValueType::WikibaseString', 'Wikidatum::DataValueType::Time', 'Wikidatum::DataValueType::WikibaseEntityId'
|
218
|
+
statement_hash = {
|
219
|
+
mainsnak: {
|
220
|
+
snaktype: 'value',
|
221
|
+
property: property,
|
222
|
+
datatype: datatype,
|
223
|
+
datavalue: {
|
224
|
+
type: datavalue.wikibase_type,
|
225
|
+
value: datavalue.marshal_dump
|
226
|
+
}
|
225
227
|
}
|
226
228
|
}
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
end
|
229
|
+
else
|
230
|
+
raise ArgumentError, "Expected an instance of one of Wikidatum::DataValueType's subclasses for datavalue, but got #{datavalue.inspect}."
|
231
|
+
end
|
231
232
|
|
232
|
-
|
233
|
+
body = { statement: statement_hash.merge({ qualifiers: qualifiers, references: references, rank: rank, type: "statement" }) }
|
233
234
|
|
234
|
-
|
235
|
+
response = post_request("/entities/items/#{id}/statements", body, tags: tags, comment: comment)
|
235
236
|
|
236
|
-
|
237
|
+
puts JSON.pretty_generate(response) if ENV['DEBUG']
|
237
238
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
# Delete a statement from an item.
|
242
|
-
#
|
243
|
-
# @example
|
244
|
-
# wikidatum_client.delete_statement(
|
245
|
-
# id: 'Q123$4543523c-1d1d-1111-1e1e-11b11111b1f1',
|
246
|
-
# comment: "Deleting this statement because it's bad."
|
247
|
-
# )
|
248
|
-
#
|
249
|
-
# @param id [String] the ID of the statemnt being deleted.
|
250
|
-
# @param tags [Array<String>]
|
251
|
-
# @param comment [String, nil]
|
252
|
-
# @return [Boolean] True if the request succeeded.
|
253
|
-
def delete_statement(id:, tags: [], comment: nil)
|
254
|
-
raise ArgumentError, "#{id.inspect} is an invalid Wikibase Statement ID. Must be a string in the format 'Q123$f004ec2b-4857-3b69-b370-e8124f5bd3ac'." unless id.match?(STATEMENT_REGEX)
|
255
|
-
|
256
|
-
response = delete_request("/statements/#{id}", tags: tags, comment: comment)
|
257
|
-
|
258
|
-
puts JSON.pretty_generate(response) if ENV['DEBUG']
|
259
|
-
|
260
|
-
response.success?
|
261
|
-
end
|
239
|
+
response.success?
|
240
|
+
end
|
262
241
|
|
263
|
-
|
242
|
+
# Delete a statement from an item.
|
243
|
+
#
|
244
|
+
# @example
|
245
|
+
# wikidatum_client.delete_statement(
|
246
|
+
# id: 'Q123$4543523c-1d1d-1111-1e1e-11b11111b1f1',
|
247
|
+
# comment: "Deleting this statement because it's bad."
|
248
|
+
# )
|
249
|
+
#
|
250
|
+
# @param id [String] the ID of the statemnt being deleted.
|
251
|
+
# @param tags [Array<String>]
|
252
|
+
# @param comment [String, nil]
|
253
|
+
# @return [Boolean] True if the request succeeded.
|
254
|
+
def delete_statement(id:, tags: [], comment: nil)
|
255
|
+
raise ArgumentError, "#{id.inspect} is an invalid Wikibase Statement ID. Must be a string in the format 'Q123$f004ec2b-4857-3b69-b370-e8124f5bd3ac'." unless id.match?(STATEMENT_REGEX)
|
256
|
+
|
257
|
+
response = delete_request("/statements/#{id}", tags: tags, comment: comment)
|
258
|
+
|
259
|
+
puts JSON.pretty_generate(response) if ENV['DEBUG']
|
260
|
+
|
261
|
+
response.success?
|
262
|
+
end
|
264
263
|
|
265
|
-
|
266
|
-
# routes will presumably be nested further, so this is just future-proofing
|
267
|
-
# to allow that to be easily changed later.
|
268
|
-
#
|
269
|
-
# @return [String] URL for the Wikibase API endpoint.
|
270
|
-
def api_url
|
271
|
-
@api_url ||= "#{@wikibase_url}/w/rest.php/wikibase/v0"
|
272
|
-
end
|
264
|
+
private
|
273
265
|
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
@
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
end
|
266
|
+
# For now this just returns the `@wikibase_url`, but in the future the API
|
267
|
+
# routes will presumably be nested further, so this is just future-proofing
|
268
|
+
# to allow that to be easily changed later.
|
269
|
+
#
|
270
|
+
# @return [String] URL for the Wikibase API endpoint.
|
271
|
+
def api_url
|
272
|
+
@api_url ||= "#{@wikibase_url}/w/rest.php/wikibase/v0"
|
273
|
+
end
|
283
274
|
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
response = Faraday.get(url, params, universal_headers)
|
293
|
-
|
294
|
-
# Error handling if it doesn't return a 200
|
295
|
-
unless response.success?
|
296
|
-
puts 'Something went wrong with this request!'
|
297
|
-
puts "Status Code: #{response.status}"
|
298
|
-
puts response.body.inspect
|
275
|
+
# Default headers to be sent with every request.
|
276
|
+
#
|
277
|
+
# @return [Hash] A hash of some headers that should be used when sending a request.
|
278
|
+
def universal_headers
|
279
|
+
@universal_headers ||= {
|
280
|
+
'User-Agent' => @user_agent,
|
281
|
+
'Content-Type' => 'application/json'
|
282
|
+
}
|
299
283
|
end
|
300
284
|
|
301
|
-
|
302
|
-
|
285
|
+
# Make a GET request to a given Wikibase endpoint.
|
286
|
+
#
|
287
|
+
# @param path [String] The relative path for the API endpoint.
|
288
|
+
# @param params [Hash] Query parameters to send with the request, if any.
|
289
|
+
# @return [Hash] JSON response, parsed into a hash.
|
290
|
+
def get_request(path, params = nil)
|
291
|
+
url = "#{api_url}#{path}"
|
303
292
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
body[:bot] = @bot
|
315
|
-
body[:tags] = tags unless tags.empty?
|
316
|
-
body[:comment] = comment unless comment.nil?
|
317
|
-
|
318
|
-
response = Faraday.post(url) do |req|
|
319
|
-
req.body = JSON.generate(body)
|
320
|
-
req.headers = universal_headers
|
293
|
+
response = Faraday.get(url, params, universal_headers)
|
294
|
+
|
295
|
+
# Error handling if it doesn't return a 200
|
296
|
+
unless response.success?
|
297
|
+
puts 'Something went wrong with this request!'
|
298
|
+
puts "Status Code: #{response.status}"
|
299
|
+
puts response.body.inspect
|
300
|
+
end
|
301
|
+
|
302
|
+
JSON.parse(response.body)
|
321
303
|
end
|
322
304
|
|
323
|
-
|
305
|
+
# Make a POST request to a given Wikibase endpoint.
|
306
|
+
#
|
307
|
+
# @param path [String] The relative path for the API endpoint.
|
308
|
+
# @param body [Hash] The body to post to the endpoint.
|
309
|
+
# @param tags [Array<String>] The tags to apply to the edit being made by this request, for PUT/POST/DELETE requests.
|
310
|
+
# @param comment [String] The edit description, for PUT/POST/DELETE requests.
|
311
|
+
# @return [Hash] JSON response, parsed into a hash.
|
312
|
+
def post_request(path, body = {}, tags: nil, comment: nil)
|
313
|
+
url = "#{api_url}#{path}"
|
324
314
|
|
325
|
-
|
326
|
-
|
315
|
+
body[:bot] = @bot
|
316
|
+
body[:tags] = tags unless tags.empty?
|
317
|
+
body[:comment] = comment unless comment.nil?
|
327
318
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
# @param comment [String] The edit description, for PUT/POST/DELETE requests.
|
333
|
-
# @return [Hash] JSON response, parsed into a hash.
|
334
|
-
def delete_request(path, tags: [], comment: nil)
|
335
|
-
url = "#{api_url}#{path}"
|
336
|
-
|
337
|
-
body = {}
|
338
|
-
body[:bot] = @bot
|
339
|
-
body[:tags] = tags unless tags.empty?
|
340
|
-
body[:comment] = comment unless comment.nil?
|
341
|
-
|
342
|
-
response = Faraday.delete(url) do |req|
|
343
|
-
req.body = JSON.generate(body)
|
344
|
-
req.headers = universal_headers
|
345
|
-
end
|
319
|
+
response = Faraday.post(url) do |req|
|
320
|
+
req.body = JSON.generate(body)
|
321
|
+
req.headers = universal_headers
|
322
|
+
end
|
346
323
|
|
347
|
-
|
324
|
+
puts response.body.inspect if ENV['DEBUG']
|
348
325
|
|
349
|
-
|
350
|
-
unless response.success?
|
351
|
-
puts 'Something went wrong with this request!'
|
352
|
-
puts "Status Code: #{response.status}"
|
353
|
-
puts response.body.inspect
|
326
|
+
response
|
354
327
|
end
|
355
328
|
|
356
|
-
|
357
|
-
|
329
|
+
# Make a DELETE request to a given Wikibase endpoint.
|
330
|
+
#
|
331
|
+
# @param path [String] The relative path for the API endpoint.
|
332
|
+
# @param tags [Array<String>] The tags to apply to the edit being made by this request, for PUT/POST/DELETE requests.
|
333
|
+
# @param comment [String] The edit description, for PUT/POST/DELETE requests.
|
334
|
+
# @return [Hash] JSON response, parsed into a hash.
|
335
|
+
def delete_request(path, tags: [], comment: nil)
|
336
|
+
url = "#{api_url}#{path}"
|
337
|
+
|
338
|
+
body = {}
|
339
|
+
body[:bot] = @bot
|
340
|
+
body[:tags] = tags unless tags.empty?
|
341
|
+
body[:comment] = comment unless comment.nil?
|
342
|
+
|
343
|
+
response = Faraday.delete(url) do |req|
|
344
|
+
req.body = JSON.generate(body)
|
345
|
+
req.headers = universal_headers
|
346
|
+
end
|
347
|
+
|
348
|
+
puts response.body.inspect if ENV['DEBUG']
|
349
|
+
|
350
|
+
# Error handling if it doesn't return a 200
|
351
|
+
unless response.success?
|
352
|
+
puts 'Something went wrong with this request!'
|
353
|
+
puts "Status Code: #{response.status}"
|
354
|
+
puts response.body.inspect
|
355
|
+
end
|
356
|
+
|
357
|
+
response
|
358
|
+
end
|
358
359
|
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
360
|
+
# Coerce an Item ID in the formats 'Q123', '123' or 123 into a consistent
|
361
|
+
# 'Q123' format. We need to have the ID in the format 'Q123' for the API
|
362
|
+
# request, which is why coercion is necessary.
|
363
|
+
#
|
364
|
+
# @param id [String, Integer]
|
365
|
+
# @return [String]
|
366
|
+
def coerce_item_id(id)
|
367
|
+
return id if id.to_s.start_with?('Q')
|
367
368
|
|
368
|
-
|
369
|
+
"Q#{id}"
|
370
|
+
end
|
369
371
|
end
|
370
372
|
end
|
data/lib/wikidatum/version.rb
CHANGED
data/lib/wikidatum.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
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_value_type'
|
5
|
+
require 'wikidatum/item'
|
6
|
+
require 'wikidatum/qualifier'
|
7
|
+
require 'wikidatum/reference'
|
8
|
+
require 'wikidatum/sitelink'
|
9
|
+
require 'wikidatum/snak'
|
10
|
+
require 'wikidatum/statement'
|
11
|
+
require 'wikidatum/term'
|
12
|
+
require 'wikidatum/version'
|
13
13
|
|
14
14
|
module Wikidatum
|
15
15
|
class Error < StandardError; 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,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wikidatum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Connor Shea
|
@@ -32,11 +32,8 @@ 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
|
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]
|