wikidatum 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8f1ec02cd83bf3ea3e432868a8283474f810bfd55740b5759c820d880f95e31
4
- data.tar.gz: a5af62f917570a29ceceb6e420377154775d9820dec8abb02eb84e037f0bab89
3
+ metadata.gz: 50ad5328f1593f1de47d492e6c4e761d80d3e43bb967bcf9386f72053881b705
4
+ data.tar.gz: eb17d97dfdbe7c45c2b10bc7d34aeeb76136b3473c72817f7a1b053f0e2d16eb
5
5
  SHA512:
6
- metadata.gz: a6c46eacdb7d7455ee5e96d3cf7c0970bd85ffc3abd1dc09c856b0f92062beaede5d451c9587ab72afc14d3b3c3a4e800c9685b435fbb4074b7d5e0b77cda463
7
- data.tar.gz: bf4810fc7384b5b5cecf2db956758a1125a1664b62b8f7275d77347c7e0b78d64fbd76d011375914162c7a3a687bb4b679fd2ace7e8addcda2ad84775b88503f
6
+ metadata.gz: f6cb6a7968e9f8943b6b6cd06311d74139a8580dde6e925a83cdf891292d5e6028a63b027394af2b04b31cadc78af88170f3a3fd3c92c2d4aeaca72560048402
7
+ data.tar.gz: d27a8240fec4fd41caa3ec21ce7bab706fed304bd364b076c741b7577bc04ce1fdd3df5db1675a5cbc431872ad1fe69f7f7b035a696b6212a38a97afec3bc2c6
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
 
7
7
  ## Unreleased
8
8
 
9
+ ## 0.2.1 - 2022-08-13
10
+ ### Fixed
11
+ - Fix a mistake that broke loading the gem.
12
+
9
13
  ## 0.2.0 - 2022-08-13
10
14
  ### Added
11
15
 
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__)
@@ -3,368 +3,370 @@
3
3
  require 'faraday'
4
4
  require 'faraday/net_http'
5
5
 
6
- class Wikidatum::Client
7
- ITEM_REGEX = /^Q?\d+$/.freeze
8
- STATEMENT_REGEX = /^Q?\d+\$[\w-]+$/.freeze
9
-
10
- # @return [String] the root URL of the Wikibase instance we want to interact
11
- # with. If not provided, will default to Wikidata.
12
- attr_reader :wikibase_url
13
-
14
- # @return [Boolean] whether this client instance should identify itself
15
- # as a bot when making requests.
16
- attr_reader :bot
17
-
18
- # @return [String] the UserAgent header to send with all requests to the
19
- # Wikibase API.
20
- attr_reader :user_agent
21
-
22
- # Create a new Wikidatum::Client to interact with the Wikibase REST API.
23
- #
24
- # @example
25
- # wikidatum_client = Wikidatum::Client.new(
26
- # user_agent: 'REPLACE ME WITH THE NAME OF YOUR BOT!',
27
- # wikibase_url: 'https://www.wikidata.org',
28
- # bot: true
29
- # )
30
- #
31
- # @param user_agent [String] The UserAgent header to send with all requests
32
- # to the Wikibase API.
33
- # @param wikibase_url [String] The root URL of the Wikibase instance we want
34
- # to interact with. If not provided, will default to
35
- # `https://www.wikidata.org`. Do not include a `/` at the end of the URL.
36
- # @param bot [Boolean] Whether requests sent by this client instance should
37
- # be registered as bot requests. Defaults to `true`.
38
- # @return [Wikidatum::Client]
39
- def initialize(user_agent:, wikibase_url: 'https://www.wikidata.org', bot: true)
40
- raise ArgumentError, "Wikibase URL must not end with a `/`, got #{wikibase_url.inspect}." if wikibase_url.end_with?('/')
41
-
42
- # TODO: Add the Ruby gem version to the UserAgent automatically, and
43
- # restrict the ability for end-users to actually set the UserAgent?
44
- @user_agent = user_agent
45
- @wikibase_url = wikibase_url
46
- @bot = bot
47
-
48
- Faraday.default_adapter = :net_http
49
- end
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
- # Get an item from the Wikibase API based on its QID.
52
- #
53
- # @example
54
- # wikidatum_client.item(id: 'Q123')
55
- # wikidatum_client.item(id: 123)
56
- # wikidatum_client.item(id: '123')
57
- #
58
- # @param id [String, Integer] Either a string or integer representation of
59
- # the item's QID, e.g. `"Q123"`, `"123"`, or `123`.
60
- # @return [Wikidatum::Item]
61
- def item(id:)
62
- 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)
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
- id = coerce_item_id(id)
65
+ id = coerce_item_id(id)
65
66
 
66
- response = get_request("/entities/items/#{id}")
67
+ response = get_request("/entities/items/#{id}")
67
68
 
68
- puts JSON.pretty_generate(response) if ENV['DEBUG']
69
+ puts JSON.pretty_generate(response) if ENV['DEBUG']
69
70
 
70
- Wikidatum::Item.marshal_load(response)
71
- end
71
+ Wikidatum::Item.marshal_load(response)
72
+ end
72
73
 
73
- # Get a statement from the Wikibase API based on its ID.
74
- #
75
- # @example
76
- # wikidatum_client.statement(id: 'Q123$f004ec2b-4857-3b69-b370-e8124f5bd3ac')
77
- #
78
- # @param id [String] A string representation of the statement's ID.
79
- # @return [Wikidatum::Statement]
80
- def statement(id:)
81
- 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)
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
- response = get_request("/statements/#{id}")
84
+ response = get_request("/statements/#{id}")
84
85
 
85
- puts JSON.pretty_generate(response) if ENV['DEBUG']
86
+ puts JSON.pretty_generate(response) if ENV['DEBUG']
86
87
 
87
- Wikidatum::Statement.marshal_load(response)
88
- end
88
+ Wikidatum::Statement.marshal_load(response)
89
+ end
89
90
 
90
- # Add a statement to an item.
91
- #
92
- # NOTE: Adding references/qualifiers with `add_statement` is untested and
93
- # effectively unsupported for now.
94
- #
95
- # @example Add a string statement.
96
- # wikidatum_client.add_statement(
97
- # id: 'Q123',
98
- # property: 'P23',
99
- # datavalue: Wikidatum::DataValueType::WikibaseString.new(string: 'Foo'),
100
- # comment: 'Adding something or another.'
101
- # )
102
- #
103
- # @example Add a 'no value' statement.
104
- # wikidatum_client.add_statement(
105
- # id: 'Q123',
106
- # property: 'P124',
107
- # datavalue: Wikidatum::DataValueType::NoValue.new(
108
- # type: :no_value,
109
- # value: nil
110
- # )
111
- # )
112
- #
113
- # @example Add an 'unknown value' statement.
114
- # wikidatum_client.add_statement(
115
- # id: 'Q123',
116
- # property: 'P124',
117
- # datavalue: Wikidatum::DataValueType::SomeValue.new(
118
- # type: :some_value,
119
- # value: nil
120
- # )
121
- # )
122
- #
123
- # @example Add a globe coordinate statement.
124
- # wikidatum_client.add_statement(
125
- # id: 'Q123',
126
- # property: 'P124',
127
- # datavalue: Wikidatum::DataValueType::GlobeCoordinate.new(
128
- # latitude: 52.51666,
129
- # longitude: 13.3833,
130
- # precision: 0.01666,
131
- # globe: 'https://wikidata.org/entity/Q2'
132
- # )
133
- # )
134
- #
135
- # @example Add a monolingual text statement.
136
- # wikidatum_client.add_statement(
137
- # id: 'Q123',
138
- # property: 'P124',
139
- # datavalue: Wikidatum::DataValueType::MonolingualText.new(
140
- # language: 'en',
141
- # text: 'Foobar'
142
- # )
143
- # )
144
- #
145
- # @example Add a quantity statement.
146
- # wikidatum_client.add_statement(
147
- # id: 'Q123',
148
- # property: 'P124',
149
- # datavalue: Wikidatum::DataValueType::Quantity.new(
150
- # amount: '+12',
151
- # upper_bound: nil,
152
- # lower_bound: nil,
153
- # unit: 'https://wikidata.org/entity/Q1234'
154
- # )
155
- # )
156
- #
157
- # @example Add a time statement.
158
- # wikidatum_client.add_statement(
159
- # id: 'Q123',
160
- # property: 'P124',
161
- # datavalue: Wikidatum::DataValueType::Time.new(
162
- # time: '+2022-08-12T00:00:00Z',
163
- # time_zone: 0,
164
- # precision: 11,
165
- # calendar_model: 'https://wikidata.org/entity/Q1234'
166
- # )
167
- # )
168
- #
169
- # @example Add a Wikibase item statement.
170
- # wikidatum_client.add_statement(
171
- # id: 'Q123',
172
- # property: 'P124',
173
- # datavalue: Wikidatum::DataValueType::WikibaseEntityId.new(
174
- # entity_type: 'item',
175
- # numeric_id: 1234,
176
- # id: 'Q1234'
177
- # )
178
- # )
179
- #
180
- # @param id [String] the ID of the item on which the statement will be added.
181
- # @param property [String] property ID in the format 'P123'.
182
- # @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.
183
- # @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).
184
- # @param qualifiers [Hash<String, Array<Wikidatum::Snak>>]
185
- # @param references [Array<Wikidatum::Reference>]
186
- # @param rank [String]
187
- # @param tags [Array<String>]
188
- # @param comment [String, nil]
189
- # @return [Boolean] True if the request succeeded.
190
- def add_statement(id:, property:, datavalue:, datatype: nil, qualifiers: {}, references: [], rank: 'normal', tags: [], comment: nil)
191
- 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)
192
-
193
- id = coerce_item_id(id)
194
-
195
- # Unless datatype is set explicitly by the caller, just assume we can pull the
196
- # default from the datavalue class.
197
- datatype ||= datavalue.wikibase_datatype
198
-
199
- case datavalue.class.to_s
200
- when 'Wikidatum::DataValueType::NoValue'
201
- statement_hash = {
202
- mainsnak: {
203
- snaktype: 'novalue',
204
- property: property,
205
- datatype: datatype
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
- when 'Wikidatum::DataValueType::SomeValue'
209
- statement_hash = {
210
- mainsnak: {
211
- snaktype: 'somevalue',
212
- property: property,
213
- datatype: datatype
209
+ when 'Wikidatum::DataValueType::SomeValue'
210
+ statement_hash = {
211
+ mainsnak: {
212
+ snaktype: 'somevalue',
213
+ property: property,
214
+ datatype: datatype
215
+ }
214
216
  }
215
- }
216
- when 'Wikidatum::DataValueType::GlobeCoordinate', 'Wikidatum::DataValueType::MonolingualText', 'Wikidatum::DataValueType::Quantity', 'Wikidatum::DataValueType::WikibaseString', 'Wikidatum::DataValueType::Time', 'Wikidatum::DataValueType::WikibaseEntityId'
217
- statement_hash = {
218
- mainsnak: {
219
- snaktype: 'value',
220
- property: property,
221
- datatype: datatype,
222
- datavalue: {
223
- type: datavalue.wikibase_type,
224
- value: datavalue.marshal_dump
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
- else
229
- raise ArgumentError, "Expected an instance of one of Wikidatum::DataValueType's subclasses for datavalue, but got #{datavalue.inspect}."
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
- body = { statement: statement_hash.merge({ qualifiers: qualifiers, references: references, rank: rank, type: "statement" }) }
233
+ body = { statement: statement_hash.merge({ qualifiers: qualifiers, references: references, rank: rank, type: "statement" }) }
233
234
 
234
- response = post_request("/entities/items/#{id}/statements", body, tags: tags, comment: comment)
235
+ response = post_request("/entities/items/#{id}/statements", body, tags: tags, comment: comment)
235
236
 
236
- puts JSON.pretty_generate(response) if ENV['DEBUG']
237
+ puts JSON.pretty_generate(response) if ENV['DEBUG']
237
238
 
238
- response.success?
239
- end
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
- private
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
- # For now this just returns the `@wikibase_url`, but in the future the API
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
- # Default headers to be sent with every request.
275
- #
276
- # @return [Hash] A hash of some headers that should be used when sending a request.
277
- def universal_headers
278
- @universal_headers ||= {
279
- 'User-Agent' => @user_agent,
280
- 'Content-Type' => 'application/json'
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
- # Make a GET request to a given Wikibase endpoint.
285
- #
286
- # @param path [String] The relative path for the API endpoint.
287
- # @param params [Hash] Query parameters to send with the request, if any.
288
- # @return [Hash] JSON response, parsed into a hash.
289
- def get_request(path, params = nil)
290
- url = "#{api_url}#{path}"
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
- JSON.parse(response.body)
302
- end
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
- # Make a POST request to a given Wikibase endpoint.
305
- #
306
- # @param path [String] The relative path for the API endpoint.
307
- # @param body [Hash] The body to post to the endpoint.
308
- # @param tags [Array<String>] The tags to apply to the edit being made by this request, for PUT/POST/DELETE requests.
309
- # @param comment [String] The edit description, for PUT/POST/DELETE requests.
310
- # @return [Hash] JSON response, parsed into a hash.
311
- def post_request(path, body = {}, tags: nil, comment: nil)
312
- url = "#{api_url}#{path}"
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
- puts response.body.inspect if ENV['DEBUG']
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
- response
326
- end
315
+ body[:bot] = @bot
316
+ body[:tags] = tags unless tags.empty?
317
+ body[:comment] = comment unless comment.nil?
327
318
 
328
- # Make a DELETE request to a given Wikibase endpoint.
329
- #
330
- # @param path [String] The relative path for the API endpoint.
331
- # @param tags [Array<String>] The tags to apply to the edit being made by this request, for PUT/POST/DELETE requests.
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
- puts response.body.inspect if ENV['DEBUG']
324
+ puts response.body.inspect if ENV['DEBUG']
348
325
 
349
- # Error handling if it doesn't return a 200
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
- response
357
- end
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
- # Coerce an Item ID in the formats 'Q123', '123' or 123 into a consistent
360
- # 'Q123' format. We need to have the ID in the format 'Q123' for the API
361
- # request, which is why coercion is necessary.
362
- #
363
- # @param id [String, Integer]
364
- # @return [String]
365
- def coerce_item_id(id)
366
- return id if id.to_s.start_with?('Q')
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
- "Q#{id}"
369
+ "Q#{id}"
370
+ end
369
371
  end
370
372
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wikidatum
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/wikidatum.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'wikidatum/client'
4
- require_relative 'wikidatum/data_value_type'
5
- require_relative 'wikidatum/item'
6
- require_relative 'wikidatum/qualifier'
7
- require_relative 'wikidatum/reference'
8
- require_relative 'wikidatum/sitelink'
9
- require_relative 'wikidatum/snak'
10
- require_relative 'wikidatum/statement'
11
- require_relative 'wikidatum/term'
12
- require_relative 'wikidatum/version'
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.0
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]