wikidatum 0.3.2 → 0.3.3

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: bcaa080ee9ab979f63dbde3fc8bca7f819ffd47b0a7d463c5f34bd57e1f2a988
4
- data.tar.gz: c0226585d7af46f01c7127b7de8ec13cc22a593f414431acd3ec11f580425b88
3
+ metadata.gz: 524da97907bba148830e9191d3c0ba5c614ddbee6c50fd74c6e91595082e73bd
4
+ data.tar.gz: 2321efe8c69e953521dbe5f4da51725fd6183ac871e7c00a17c552c12dc15dc2
5
5
  SHA512:
6
- metadata.gz: '058da6289ed70d623511a2732b3c83c4d1b574aadc8570cbceee83c23df7adf71ef34e8413783cc1c0e9173c8ea7fcbb419d01b9fd58816278614bfd2f10e83f'
7
- data.tar.gz: 0a0220e396643884380d858b0fe4cd4ab867f5f3a7ff92ccbbc54440ac4668f490a172f7244d567b093cd753044f845ced5daae3482514fa897cb0a42234ffe6
6
+ metadata.gz: 634701e7e5204ea770eb114f62982140e3441358e71f1ca32bf38718f9e72f29fe582340546c28e0dad4e575903c6822057bb12f21572333c68cd6c28b2f1e46
7
+ data.tar.gz: 26cf62b267be9bb4a6cc752d1968740b3d153f972204f0b1d7900fa7e087cea6eef0e543972b3aafb22f98ffbea8cdc6e30f22cc30f34aa77d2f4d1605655d02
data/CHANGELOG.md CHANGED
@@ -6,6 +6,15 @@ 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
+
9
18
  ## 0.3.2 - 2022-12-29
10
19
  ### Added
11
20
 
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 Connor Shea
3
+ Copyright (c) 2022-2023 Connor Shea
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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's JSON format](https://doc.wikimedia.org/Wikibase/master/php/docs_topics_json.html) are also very useful for interacting with/contributing to this gem.
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 early development and is not ready for production usage**.
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
- # Currently only the beta site has the API available, you'll
37
- # likely want to use wikidata.org once it's stable.
38
- wikibase_url: 'https://wikidata.beta.wmflabs.org',
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
 
@@ -23,6 +23,19 @@ module Wikidatum
23
23
  'Wikidatum::DataType::WikibaseUrl'
24
24
  ].freeze
25
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'
37
+ ].freeze
38
+
26
39
  # @return [String] the root URL of the Wikibase instance we want to interact
27
40
  # with. If not provided, will default to Wikidata.
28
41
  attr_reader :wikibase_url
@@ -55,7 +68,10 @@ module Wikidatum
55
68
  # to interact with. If not provided, will default to
56
69
  # `https://www.wikidata.org`. Do not include a `/` at the end of the URL.
57
70
  # @param bot [Boolean] Whether requests sent by this client instance should
58
- # 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`.
59
75
  # @param allow_ip_edits [Boolean] whether this client should allow non-GET
60
76
  # requests if authentication hasn't been provided. Defaults to false. If
61
77
  # this is set to true, the IP address of the device from which the
@@ -96,6 +112,28 @@ module Wikidatum
96
112
  Wikidatum::Item.marshal_load(response)
97
113
  end
98
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
+
99
137
  # Get a statement from the Wikibase API based on its ID.
100
138
  #
101
139
  # @example
@@ -113,19 +151,6 @@ module Wikidatum
113
151
  Wikidatum::Statement.marshal_load(response)
114
152
  end
115
153
 
116
- # @!private
117
- CONTENT_DATA_TYPES = [
118
- 'Wikidatum::DataType::CommonsMedia',
119
- 'Wikidatum::DataType::ExternalId',
120
- 'Wikidatum::DataType::GlobeCoordinate',
121
- 'Wikidatum::DataType::MonolingualText',
122
- 'Wikidatum::DataType::Quantity',
123
- 'Wikidatum::DataType::WikibaseString',
124
- 'Wikidatum::DataType::Time',
125
- 'Wikidatum::DataType::WikibaseItem',
126
- 'Wikidatum::DataType::WikibaseUrl'
127
- ].freeze
128
-
129
154
  # Add a statement to an item.
130
155
  #
131
156
  # NOTE: Adding references/qualifiers with `add_statement` is untested and
@@ -489,10 +514,10 @@ module Wikidatum
489
514
  # error message if so.
490
515
  #
491
516
  # @return [void]
492
- # @raise [DisallowedIpEditError, DisallowedBotEditError]
517
+ # @raise [Errors::DisallowedIpEditError, Errors::DisallowedBotEditError]
493
518
  def ensure_edit_permitted!
494
- raise DisallowedIpEditError if !authenticated? && !allow_ip_edits?
495
- raise DisallowedBotEditError if !authenticated? && bot?
519
+ raise Errors::DisallowedIpEditError if !authenticated? && !allow_ip_edits?
520
+ raise Errors::DisallowedBotEditError if !authenticated? && bot?
496
521
  end
497
522
  end
498
523
  end
@@ -26,13 +26,13 @@ module Wikidatum::DataType
26
26
 
27
27
  # The value of the "content" attribute in the response.
28
28
  #
29
- # If the `type` is `novalue` or `somevalue`, this returns `nil`.
29
+ # If the `type` is `:no_value` or `:some_value`, this returns `nil`.
30
30
  #
31
- # @return [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::WikibaseString, DataType::Time, DataType::WikibaseItem, nil]
31
+ # @return [DataType::CommonsMedia, DataType::ExternalId, DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::Time, DataType::WikibaseItem, DataType::WikibaseString, DataType::WikibaseUrl, nil]
32
32
  attr_reader :content
33
33
 
34
34
  # @param type [Symbol]
35
- # @param content [DataType::GlobeCoordinate, DataType::MonolingualText, DataType::Quantity, DataType::WikibaseString, DataType::Time, DataType::WikibaseItem, nil] nil if type is no_value or some_value
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
36
36
  # @return [void]
37
37
  def initialize(type:, content:)
38
38
  @type = type
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wikidatum
4
+ class Error < StandardError; end
5
+ end
@@ -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
@@ -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/md_docs_topics_json.html#json_datavalues
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/md_docs_topics_json.html#json_datavalues
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::WikibaseString, DataType::Time, DataType::WikibaseItem, DataType::NoValue, DataType::SomeValue]
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
@@ -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
@@ -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::WikibaseString, DataType::Time, DataType::WikibaseItem, nil]
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::WikibaseString, DataType::Time, DataType::WikibaseItem, nil]
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.
@@ -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 term.
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wikidatum
4
- VERSION = '0.3.2'
4
+ VERSION = '0.3.3'
5
5
  end
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
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.2
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: 2022-12-29 00:00:00.000000000 Z
11
+ date: 2023-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -51,6 +51,8 @@ files:
51
51
  - lib/wikidatum/data_type/wikibase_item.rb
52
52
  - lib/wikidatum/data_type/wikibase_string.rb
53
53
  - lib/wikidatum/data_type/wikibase_url.rb
54
+ - lib/wikidatum/error.rb
55
+ - lib/wikidatum/errors.rb
54
56
  - lib/wikidatum/item.rb
55
57
  - lib/wikidatum/qualifier.rb
56
58
  - lib/wikidatum/reference.rb