Class: Wikidatum::Client
- Inherits:
-
Object
- Object
- Wikidatum::Client
- Defined in:
- lib/wikidatum/client.rb
Constant Summary collapse
- ITEM_REGEX =
/^Q?\d+$/.freeze
- PROPERTY_REGEX =
/^P?\d+$/.freeze
- STATEMENT_REGEX =
/^Q?\d+\$[\w-]+$/.freeze
- VALID_RANKS =
['preferred', 'normal', 'deprecated'].freeze
- VALID_DATA_TYPES =
[ 'Wikidatum::DataType::CommonsMedia', 'Wikidatum::DataType::ExternalId', 'Wikidatum::DataType::GlobeCoordinate', 'Wikidatum::DataType::MonolingualText', 'Wikidatum::DataType::NoValue', 'Wikidatum::DataType::Quantity', 'Wikidatum::DataType::SomeValue', 'Wikidatum::DataType::Time', 'Wikidatum::DataType::WikibaseItem', 'Wikidatum::DataType::WikibaseString', 'Wikidatum::DataType::WikibaseUrl' ].freeze
- CONTENT_DATA_TYPES =
[ 'Wikidatum::DataType::CommonsMedia', 'Wikidatum::DataType::ExternalId', 'Wikidatum::DataType::GlobeCoordinate', 'Wikidatum::DataType::MonolingualText', 'Wikidatum::DataType::Quantity', 'Wikidatum::DataType::WikibaseString', 'Wikidatum::DataType::Time', 'Wikidatum::DataType::WikibaseItem', 'Wikidatum::DataType::WikibaseUrl' ].freeze
Instance Attribute Summary collapse
-
#allow_ip_edits ⇒ Boolean
readonly
Whether this client should allow non-GET requests if authentication hasn’t been provided.
-
#bot ⇒ Boolean
readonly
Whether this client instance should identify itself as a bot when making requests.
-
#user_agent ⇒ String
readonly
The UserAgent header to send with all requests to the Wikibase API.
-
#wikibase_url ⇒ String
readonly
The root URL of the Wikibase instance we want to interact with.
Instance Method Summary collapse
-
#add_statement(id:, property:, value:, qualifiers: [], references: [], rank: 'normal', tags: [], comment: nil) ⇒ Boolean
Add a statement to an item.
-
#allow_ip_edits? ⇒ Boolean
Does the current instance of Client allow anonymous IP-based edits?.
-
#authenticated? ⇒ Boolean
Is the current instance of Client authenticated as a Wikibase user?.
-
#bot? ⇒ Boolean
Is the current instance of Client editing as a bot?.
-
#delete_statement(id:, tags: [], comment: nil) ⇒ Boolean
Delete a statement from an item.
-
#initialize(user_agent:, wikibase_url: 'https://www.wikidata.org', bot: true, allow_ip_edits: false) ⇒ Wikidatum::Client
constructor
Create a new Wikidatum::Client to interact with the Wikibase REST API.
-
#item(id:) ⇒ Wikidatum::Item
Get an item from the Wikibase API based on its QID.
-
#statement(id:) ⇒ Wikidatum::Statement
Get a statement from the Wikibase API based on its ID.
Constructor Details
#initialize(user_agent:, wikibase_url: 'https://www.wikidata.org', bot: true, allow_ip_edits: false) ⇒ Wikidatum::Client
Create a new Wikidatum::Client to interact with the Wikibase REST API.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/wikidatum/client.rb', line 66 def initialize(user_agent:, wikibase_url: 'https://www.wikidata.org', bot: true, allow_ip_edits: false) raise ArgumentError, "Wikibase URL must not end with a `/`, got #{wikibase_url.inspect}." if wikibase_url.end_with?('/') @user_agent = "Wikidatum Ruby gem v#{Wikidatum::VERSION}: #{user_agent}" @wikibase_url = wikibase_url @bot = bot @allow_ip_edits = allow_ip_edits Faraday.default_adapter = :net_http end |
Instance Attribute Details
#allow_ip_edits ⇒ Boolean (readonly)
Returns whether this client should allow non-GET requests if authentication hasn’t been provided. Defaults to false.
40 41 42 |
# File 'lib/wikidatum/client.rb', line 40 def allow_ip_edits @allow_ip_edits end |
#bot ⇒ Boolean (readonly)
Returns whether this client instance should identify itself as a bot when making requests.
32 33 34 |
# File 'lib/wikidatum/client.rb', line 32 def bot @bot end |
#user_agent ⇒ String (readonly)
Returns the UserAgent header to send with all requests to the Wikibase API.
36 37 38 |
# File 'lib/wikidatum/client.rb', line 36 def user_agent @user_agent end |
#wikibase_url ⇒ String (readonly)
Returns the root URL of the Wikibase instance we want to interact with. If not provided, will default to Wikidata.
28 29 30 |
# File 'lib/wikidatum/client.rb', line 28 def wikibase_url @wikibase_url end |
Instance Method Details
#add_statement(id:, property:, value:, qualifiers: [], references: [], rank: 'normal', tags: [], comment: nil) ⇒ Boolean
Add a statement to an item.
NOTE: Adding references/qualifiers with ‘add_statement` is untested and effectively unsupported for now.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/wikidatum/client.rb', line 251 def add_statement(id:, property:, value:, qualifiers: [], references: [], rank: 'normal', tags: [], comment: nil) 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) raise ArgumentError, "#{property.inspect} is an invalid Wikibase PID. Must be an integer, a string representation of an integer, or in the format 'P123'." unless property.is_a?(Integer) || property.match?(PROPERTY_REGEX) raise ArgumentError, "#{rank.inspect} is an invalid rank. Must be normal, preferred, or deprecated." unless VALID_RANKS.include?(rank.to_s) raise ArgumentError, "Expected an instance of one of Wikidatum::DataType's subclasses for value, but got #{value.inspect}." unless VALID_DATA_TYPES.include?(value.class.to_s) id = coerce_item_id(id) property = coerce_property_id(property) case value.class.to_s when 'Wikidatum::DataType::NoValue' statement_hash = { property: { id: property }, value: { type: 'novalue' } } when 'Wikidatum::DataType::SomeValue' statement_hash = { property: { id: property }, value: { type: 'somevalue' } } when *CONTENT_DATA_TYPES statement_hash = { property: { id: property }, value: { type: 'value', content: value.marshal_dump } } end body = { statement: statement_hash.merge( { qualifiers: qualifiers, references: references, rank: rank.to_s } ) } response = post_request("/entities/items/#{id}/statements", body, tags: , comment: comment) puts JSON.pretty_generate(response) if ENV['DEBUG'] response.success? end |
#allow_ip_edits? ⇒ Boolean
Does the current instance of Client allow anonymous IP-based edits?
342 343 344 |
# File 'lib/wikidatum/client.rb', line 342 def allow_ip_edits? @allow_ip_edits end |
#authenticated? ⇒ Boolean
Is the current instance of Client authenticated as a Wikibase user?
333 334 335 336 337 |
# File 'lib/wikidatum/client.rb', line 333 def authenticated? # TODO: Make it possible for this to be true once authentication # is implemented. false end |
#bot? ⇒ Boolean
Is the current instance of Client editing as a bot?
349 350 351 |
# File 'lib/wikidatum/client.rb', line 349 def bot? @bot end |
#delete_statement(id:, tags: [], comment: nil) ⇒ Boolean
Delete a statement from an item.
320 321 322 323 324 325 326 327 328 |
# File 'lib/wikidatum/client.rb', line 320 def delete_statement(id:, tags: [], comment: nil) 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) response = delete_request("/statements/#{id}", tags: , comment: comment) puts JSON.pretty_generate(response) if ENV['DEBUG'] response.success? end |
#item(id:) ⇒ Wikidatum::Item
Get an item from the Wikibase API based on its QID.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/wikidatum/client.rb', line 87 def item(id:) 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) id = coerce_item_id(id) response = get_request("/entities/items/#{id}") puts JSON.pretty_generate(response) if ENV['DEBUG'] Wikidatum::Item.marshal_load(response) end |
#statement(id:) ⇒ Wikidatum::Statement
Get a statement from the Wikibase API based on its ID.
106 107 108 109 110 111 112 113 114 |
# File 'lib/wikidatum/client.rb', line 106 def statement(id:) 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) response = get_request("/statements/#{id}") puts JSON.pretty_generate(response) if ENV['DEBUG'] Wikidatum::Statement.marshal_load(response) end |