wikidata_adaptor 1.0.0

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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.env.test +7 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +45 -0
  5. data/.yardopts +10 -0
  6. data/CHANGELOG.md +106 -0
  7. data/CLAUDE.md +200 -0
  8. data/CODE_OF_CONDUCT.md +84 -0
  9. data/COVERAGE.md +77 -0
  10. data/Gemfile +20 -0
  11. data/Gemfile.lock +124 -0
  12. data/LICENSE.txt +21 -0
  13. data/README.md +269 -0
  14. data/Rakefile +18 -0
  15. data/integration/.env.example +20 -0
  16. data/integration/README.md +58 -0
  17. data/integration/config/99_IntegrationTesting.php +3 -0
  18. data/integration/config/wikibase-php.ini +15 -0
  19. data/integration/docker-compose.yml +98 -0
  20. data/lib/wikidata_adaptor/rest_api/aliases.rb +88 -0
  21. data/lib/wikidata_adaptor/rest_api/descriptions.rb +138 -0
  22. data/lib/wikidata_adaptor/rest_api/items.rb +36 -0
  23. data/lib/wikidata_adaptor/rest_api/labels.rb +138 -0
  24. data/lib/wikidata_adaptor/rest_api/open_api_document.rb +15 -0
  25. data/lib/wikidata_adaptor/rest_api/properties.rb +36 -0
  26. data/lib/wikidata_adaptor/rest_api/property_data_types.rb +15 -0
  27. data/lib/wikidata_adaptor/rest_api/search_item.rb +44 -0
  28. data/lib/wikidata_adaptor/rest_api/search_property.rb +44 -0
  29. data/lib/wikidata_adaptor/rest_api/sitelinks.rb +59 -0
  30. data/lib/wikidata_adaptor/rest_api/statements.rb +153 -0
  31. data/lib/wikidata_adaptor/rest_api.rb +32 -0
  32. data/lib/wikidata_adaptor/test_helpers/rest_api/aliases.rb +229 -0
  33. data/lib/wikidata_adaptor/test_helpers/rest_api/descriptions.rb +308 -0
  34. data/lib/wikidata_adaptor/test_helpers/rest_api/items.rb +213 -0
  35. data/lib/wikidata_adaptor/test_helpers/rest_api/labels.rb +302 -0
  36. data/lib/wikidata_adaptor/test_helpers/rest_api/open_api_document.rb +29 -0
  37. data/lib/wikidata_adaptor/test_helpers/rest_api/properties.rb +233 -0
  38. data/lib/wikidata_adaptor/test_helpers/rest_api/property_data_types.rb +23 -0
  39. data/lib/wikidata_adaptor/test_helpers/rest_api/search_item.rb +118 -0
  40. data/lib/wikidata_adaptor/test_helpers/rest_api/search_property.rb +118 -0
  41. data/lib/wikidata_adaptor/test_helpers/rest_api/sitelinks.rb +143 -0
  42. data/lib/wikidata_adaptor/test_helpers/rest_api/statements.rb +475 -0
  43. data/lib/wikidata_adaptor/test_helpers/rest_api/support/support.rb +310 -0
  44. data/lib/wikidata_adaptor/test_helpers/rest_api.rb +89 -0
  45. data/lib/wikidata_adaptor/version.rb +6 -0
  46. data/lib/wikidata_adaptor.rb +28 -0
  47. data/sig/wikidata_adaptor.rbs +4 -0
  48. metadata +106 -0
@@ -0,0 +1,310 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WikidataAdaptor
4
+ module TestHelpers
5
+ module RestApi
6
+ # WebMock stubs for Wikibase REST API support utilities
7
+ module Support
8
+ # Load the OpenAPI specification from file
9
+ #
10
+ # @return [Hash] The parsed OpenAPI specification
11
+ def load_openapi_spec
12
+ JSON.parse(
13
+ File.read(File.join("spec", "openapi", "openapi.json"))
14
+ )
15
+ end
16
+
17
+ # Load example response from OpenAPI specification for a given path and method
18
+ #
19
+ # @param path [String] The API path
20
+ # @param method [String] The HTTP method
21
+ # @param response_code [String] The HTTP response code
22
+ #
23
+ # @return [Hash] The example response
24
+ def load_path_example(path, method, response_code = "200")
25
+ load_openapi_spec.dig(
26
+ "paths",
27
+ path,
28
+ method,
29
+ "responses",
30
+ response_code,
31
+ "content",
32
+ "application/json",
33
+ "example"
34
+ )
35
+ end
36
+
37
+ # Fixture for a posted item response
38
+ #
39
+ # @return [Hash] The posted item response fixture
40
+ def posted_item_response_fixture
41
+ {
42
+ "id" => "Q24",
43
+ "type" => "item",
44
+ "labels" => {
45
+ "en" => "Jane Doe",
46
+ "ru" => "Джейн Доу"
47
+ },
48
+ "descriptions" => {
49
+ "en" => "famous person",
50
+ "ru" => "известная личность"
51
+ },
52
+ "aliases" => {
53
+ "en" => [
54
+ "Jane M. Doe",
55
+ "JD"
56
+ ],
57
+ "ru" => [
58
+ "Джейн М. Доу"
59
+ ]
60
+ },
61
+ "statements" => {
62
+ "P694" => [
63
+ {
64
+ "id" => "Q24$BB728546-A400-4116-A772-16D54B62AC2B",
65
+ "rank" => "normal",
66
+ "property" => {
67
+ "id" => "P694",
68
+ "data_type" => "wikibase-item"
69
+ },
70
+ "value" => {
71
+ "type" => "value",
72
+ "content" => "Q626683"
73
+ },
74
+ "qualifiers" => [],
75
+ "references" => []
76
+ }
77
+ ],
78
+ "P476" => [
79
+ {
80
+ "id" => "Q24$F3B2F956-B6AB-4984-8D89-BEE0FFFA3385",
81
+ "rank" => "normal",
82
+ "property" => {
83
+ "id" => "P476",
84
+ "data_type" => "time"
85
+ },
86
+ "value" => {
87
+ "type" => "value",
88
+ "content" => {
89
+ "time" => "+1986-01-27T00:00:00Z",
90
+ "precision" => 11,
91
+ "calendarmodel" => "http://www.wikidata.org/entity/Q1985727"
92
+ }
93
+ },
94
+ "qualifiers" => [],
95
+ "references" => []
96
+ }
97
+ ],
98
+ "P17" => [
99
+ {
100
+ "id" => "Q24$9966A1CA-F3F5-4B1D-A534-7CD5953169DA",
101
+ "rank" => "normal",
102
+ "property" => {
103
+ "id" => "P17",
104
+ "data_type" => "string"
105
+ },
106
+ "value" => {
107
+ "type" => "value",
108
+ "content" => "Senior Team Supervisor"
109
+ },
110
+ "qualifiers" => [
111
+ {
112
+ "property" => {
113
+ "id" => "P706",
114
+ "data_type" => "time"
115
+ },
116
+ "value" => {
117
+ "type" => "value",
118
+ "content" => {
119
+ "time" => "+2023-06-13T00:00:00Z",
120
+ "precision" => 11,
121
+ "calendarmodel" => "http://www.wikidata.org/entity/Q1985727"
122
+ }
123
+ }
124
+ }
125
+ ],
126
+ "references" => [
127
+ {
128
+ "hash" => "7ccd777f870b71a4c5056c7fd2a83a22cc39be6d",
129
+ "parts" => [
130
+ {
131
+ "property" => {
132
+ "id" => "P709",
133
+ "data_type" => "url"
134
+ },
135
+ "value" => {
136
+ "type" => "value",
137
+ "content" => "https://news.example.org"
138
+ }
139
+ }
140
+ ]
141
+ }
142
+ ]
143
+ }
144
+ ]
145
+ },
146
+ "sitelinks" => {
147
+ "enwiki" => {
148
+ "title" => "Jane Doe",
149
+ "badges" => [],
150
+ "url" => "https://enwiki.example.org/wiki/Jane_Doe"
151
+ },
152
+ "ruwiki" => {
153
+ "title" => "Джейн Доу",
154
+ "badges" => [],
155
+ "url" => "https://ruwiki.example.org/wiki/Джейн_Доу"
156
+ }
157
+ }
158
+ }
159
+ end
160
+
161
+ # Fixture for a posted property response
162
+ #
163
+ # @return [Hash] The posted property response fixture
164
+ def posted_property_response_fixture
165
+ {
166
+ "id" => "P1",
167
+ "type" => "property",
168
+ "data_type" => "string",
169
+ "labels" => {
170
+ "en" => "instance of"
171
+ },
172
+ "descriptions" => {
173
+ "en" => "that class of which this subject is a particular example and member"
174
+ },
175
+ "aliases" => {
176
+ "en" => ["is a"]
177
+ },
178
+ "statements" => {}
179
+ }
180
+ end
181
+
182
+ # Fixture for a property creation payload
183
+ #
184
+ # @return [Hash] The posted property payload fixture
185
+ def posted_property_payload_fixture
186
+ {
187
+ "property" => {
188
+ "data_type" => "string",
189
+ "labels" => {
190
+ "en" => "instance of"
191
+ },
192
+ "descriptions" => {
193
+ "en" => "that class of which this subject is a particular example and member"
194
+ },
195
+ "aliases" => {
196
+ "en" => ["is a"]
197
+ }
198
+ },
199
+ "comment" => "Create a Property"
200
+ }
201
+ end
202
+
203
+ # Fixture for an item creation payload
204
+ #
205
+ # @return [Hash] The posted item payload fixture
206
+ def posted_item_payload_fixture
207
+ {
208
+ "item" => {
209
+ "labels" => {
210
+ "en" => "Jane Doe",
211
+ "ru" => "Джейн Доу"
212
+ },
213
+ "descriptions" => {
214
+ "en" => "famous person",
215
+ "ru" => "известная личность"
216
+ },
217
+ "aliases" => {
218
+ "en" => [
219
+ "Jane M. Doe",
220
+ "JD"
221
+ ],
222
+ "ru" => [
223
+ "Джейн М. Доу"
224
+ ]
225
+ },
226
+ "statements" => {
227
+ "P694" => [
228
+ {
229
+ "property" => {
230
+ "id" => "P694"
231
+ },
232
+ "value" => {
233
+ "type" => "value",
234
+ "content" => "Q626683"
235
+ }
236
+ }
237
+ ],
238
+ "P476" => [
239
+ {
240
+ "property" => {
241
+ "id" => "P476"
242
+ },
243
+ "value" => {
244
+ "type" => "value",
245
+ "content" => {
246
+ "time" => "+1986-01-27T00:00:00Z",
247
+ "precision" => 11,
248
+ "calendarmodel" => "http://www.wikidata.org/entity/Q1985727"
249
+ }
250
+ }
251
+ }
252
+ ],
253
+ "P17" => [
254
+ {
255
+ "property" => {
256
+ "id" => "P17"
257
+ },
258
+ "value" => {
259
+ "type" => "value",
260
+ "content" => "Senior Team Supervisor"
261
+ },
262
+ "qualifiers" => [
263
+ {
264
+ "property" => {
265
+ "id" => "P706"
266
+ },
267
+ "value" => {
268
+ "type" => "value",
269
+ "content" => {
270
+ "time" => "+2023-06-13T00:00:00Z",
271
+ "precision" => 11,
272
+ "calendarmodel" => "http://www.wikidata.org/entity/Q1985727"
273
+ }
274
+ }
275
+ }
276
+ ],
277
+ "references" => [
278
+ {
279
+ "parts" => [
280
+ {
281
+ "property" => {
282
+ "id" => "P709"
283
+ },
284
+ "value" => {
285
+ "type" => "value",
286
+ "content" => "https://news.example.org"
287
+ }
288
+ }
289
+ ]
290
+ }
291
+ ]
292
+ }
293
+ ]
294
+ },
295
+ "sitelinks" => {
296
+ "enwiki" => {
297
+ "title" => "Jane Doe"
298
+ },
299
+ "ruwiki" => {
300
+ "title" => "Джейн Доу"
301
+ }
302
+ }
303
+ },
304
+ "comment" => "Create an Item for Jane Doe"
305
+ }
306
+ end
307
+ end
308
+ end
309
+ end
310
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "webmock"
5
+
6
+ module WikidataAdaptor
7
+ # Test helpers for stubbing WikidataAdaptor API calls in tests
8
+ module TestHelpers
9
+ # WebMock stubs for the Wikibase REST API
10
+ #
11
+ # Include this module in your RSpec tests to access stub methods
12
+ # for all Wikibase REST API endpoints.
13
+ #
14
+ # @example
15
+ # RSpec.describe MyClass do
16
+ # include WikidataAdaptor::TestHelpers::RestApi
17
+ #
18
+ # it "fetches an item" do
19
+ # stub_get_item("Q42")
20
+ # # ...
21
+ # end
22
+ # end
23
+ module RestApi
24
+ require_relative "rest_api/open_api_document"
25
+ require_relative "rest_api/aliases"
26
+ require_relative "rest_api/items"
27
+ require_relative "rest_api/sitelinks"
28
+ require_relative "rest_api/properties"
29
+ require_relative "rest_api/labels"
30
+ require_relative "rest_api/descriptions"
31
+ require_relative "rest_api/statements"
32
+ require_relative "rest_api/property_data_types"
33
+ require_relative "rest_api/search_item"
34
+ require_relative "rest_api/search_property"
35
+ include WikidataAdaptor::TestHelpers::RestApi::OpenApiDocument
36
+ include WikidataAdaptor::TestHelpers::RestApi::Aliases
37
+ include WikidataAdaptor::TestHelpers::RestApi::Items
38
+ include WikidataAdaptor::TestHelpers::RestApi::Sitelinks
39
+ include WikidataAdaptor::TestHelpers::RestApi::Properties
40
+ include WikidataAdaptor::TestHelpers::RestApi::Labels
41
+ include WikidataAdaptor::TestHelpers::RestApi::Descriptions
42
+ include WikidataAdaptor::TestHelpers::RestApi::Statements
43
+ include WikidataAdaptor::TestHelpers::RestApi::PropertyDataTypes
44
+ include WikidataAdaptor::TestHelpers::RestApi::SearchItem
45
+ include WikidataAdaptor::TestHelpers::RestApi::SearchProperty
46
+
47
+ # Default test endpoint for stubbed requests
48
+ WIKIBASE_REST_ENDPOINT = ENV["WIKIBASE_REST_ENDPOINT"] || "https://test.test/w/rest.php/wikibase"
49
+
50
+ # Stub a Wikibase REST API request with WebMock
51
+ #
52
+ # This is the base method used by all specific stub_* helpers.
53
+ # Use the specific stub methods instead of calling this directly.
54
+ #
55
+ # @param [Symbol] method HTTP method (:get, :post, :put, :patch, :delete)
56
+ # @param [String] path API path (e.g., "/v1/entities/items/Q42")
57
+ # @param [Hash] with Optional request matching criteria
58
+ # @param [Integer] response_status HTTP status code to return
59
+ # @param [Hash] response_headers HTTP headers to return
60
+ # @param [Hash, Array, String] response_body Response body
61
+ # @param [String, nil] session Optional session token
62
+ #
63
+ # @return [WebMock::RequestStub] The configured stub
64
+ def stub_rest_api_request(method, path, with: {}, response_status: 200, response_headers: {}, response_body: {}, session: nil)
65
+ with.merge!(headers: { WikidataAdaptor::RestApi::AUTH_HEADER_NAME => session }) if session
66
+ session = nil if response_status >= 400
67
+ to_return = { status: response_status, headers: response_headers, body: prepare_response(response_body, session) }
68
+ if with.empty?
69
+ stub_request(method, "#{WIKIBASE_REST_ENDPOINT}#{path}").to_return(**to_return)
70
+ else
71
+ stub_request(method, "#{WIKIBASE_REST_ENDPOINT}#{path}").with(**with).to_return(**to_return)
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def prepare_response(response_body, session)
78
+ case response_body
79
+ when Hash
80
+ response_body.merge(session: session).compact.to_json
81
+ when Array
82
+ response_body.to_json
83
+ else # String or other
84
+ response_body
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WikidataAdaptor
4
+ # Gem version
5
+ VERSION = "1.0.0"
6
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "api_adaptor"
4
+
5
+ require_relative "wikidata_adaptor/version"
6
+ require_relative "wikidata_adaptor/rest_api"
7
+
8
+ module WikidataAdaptor
9
+ # Base error class for WikidataAdaptor exceptions
10
+ class Error < StandardError; end
11
+
12
+ # Get the Wikibase REST API endpoint URL
13
+ #
14
+ # Reads from the WIKIBASE_REST_ENDPOINT environment variable,
15
+ # defaulting to the production Wikidata endpoint.
16
+ #
17
+ # @return [String] The base URL for the Wikibase REST API
18
+ def self.rest_endpoint
19
+ ENV["WIKIBASE_REST_ENDPOINT"] || "https://www.wikidata.org/w/rest.php/wikibase"
20
+ end
21
+
22
+ # Creates a WikidataAdaptor::RestApi adapter
23
+ #
24
+ # @return [WikidataAdaptor::RestApi]
25
+ def self.rest_api
26
+ WikidataAdaptor::RestApi.new(rest_endpoint)
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module WikidataAdaptor
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wikidata_adaptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Huw Diprose
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: api_adaptor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 1.0.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.0
26
+ description: Use the Wikibase Action and REST API to interact with wikidata
27
+ email:
28
+ - mail@huwdiprose.co.uk
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".env.test"
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - ".yardopts"
37
+ - CHANGELOG.md
38
+ - CLAUDE.md
39
+ - CODE_OF_CONDUCT.md
40
+ - COVERAGE.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - integration/.env.example
47
+ - integration/README.md
48
+ - integration/config/99_IntegrationTesting.php
49
+ - integration/config/wikibase-php.ini
50
+ - integration/docker-compose.yml
51
+ - lib/wikidata_adaptor.rb
52
+ - lib/wikidata_adaptor/rest_api.rb
53
+ - lib/wikidata_adaptor/rest_api/aliases.rb
54
+ - lib/wikidata_adaptor/rest_api/descriptions.rb
55
+ - lib/wikidata_adaptor/rest_api/items.rb
56
+ - lib/wikidata_adaptor/rest_api/labels.rb
57
+ - lib/wikidata_adaptor/rest_api/open_api_document.rb
58
+ - lib/wikidata_adaptor/rest_api/properties.rb
59
+ - lib/wikidata_adaptor/rest_api/property_data_types.rb
60
+ - lib/wikidata_adaptor/rest_api/search_item.rb
61
+ - lib/wikidata_adaptor/rest_api/search_property.rb
62
+ - lib/wikidata_adaptor/rest_api/sitelinks.rb
63
+ - lib/wikidata_adaptor/rest_api/statements.rb
64
+ - lib/wikidata_adaptor/test_helpers/rest_api.rb
65
+ - lib/wikidata_adaptor/test_helpers/rest_api/aliases.rb
66
+ - lib/wikidata_adaptor/test_helpers/rest_api/descriptions.rb
67
+ - lib/wikidata_adaptor/test_helpers/rest_api/items.rb
68
+ - lib/wikidata_adaptor/test_helpers/rest_api/labels.rb
69
+ - lib/wikidata_adaptor/test_helpers/rest_api/open_api_document.rb
70
+ - lib/wikidata_adaptor/test_helpers/rest_api/properties.rb
71
+ - lib/wikidata_adaptor/test_helpers/rest_api/property_data_types.rb
72
+ - lib/wikidata_adaptor/test_helpers/rest_api/search_item.rb
73
+ - lib/wikidata_adaptor/test_helpers/rest_api/search_property.rb
74
+ - lib/wikidata_adaptor/test_helpers/rest_api/sitelinks.rb
75
+ - lib/wikidata_adaptor/test_helpers/rest_api/statements.rb
76
+ - lib/wikidata_adaptor/test_helpers/rest_api/support/support.rb
77
+ - lib/wikidata_adaptor/version.rb
78
+ - sig/wikidata_adaptor.rbs
79
+ homepage: https://github.com/huwd/wikidata_adaptor
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://github.com/huwd/wikidata_adaptor
84
+ source_code_uri: https://github.com/huwd/wikidata_adaptor
85
+ changelog_uri: https://github.com/huwd/wikidata_adaptor/blob/main/CHANGELOG.md
86
+ documentation_uri: https://huwd.github.io/wikidata_adaptor/
87
+ bug_tracker_uri: https://github.com/huwd/wikidata_adaptor/issues
88
+ rubygems_mfa_required: 'true'
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.6
104
+ specification_version: 4
105
+ summary: An API wrapper to interact with Wikidata and Wikibase APIs
106
+ test_files: []