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::Time', 'Wikidatum::DataType::WikibaseItem', 'Wikidatum::DataType::WikibaseString', '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.
-
#labels(id:) ⇒ Array<Wikidatum::Term>
Get labels for an item from the Wikibase API based on the item’s 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.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wikidatum/client.rb', line 82 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.
53 54 55 |
# File 'lib/wikidatum/client.rb', line 53 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.
45 46 47 |
# File 'lib/wikidatum/client.rb', line 45 def bot @bot end |
#user_agent ⇒ String (readonly)
Returns the UserAgent header to send with all requests to the Wikibase API.
49 50 51 |
# File 'lib/wikidatum/client.rb', line 49 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.
41 42 43 |
# File 'lib/wikidatum/client.rb', line 41 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.
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/wikidatum/client.rb', line 276 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?
367 368 369 |
# File 'lib/wikidatum/client.rb', line 367 def allow_ip_edits? @allow_ip_edits end |
#authenticated? ⇒ Boolean
Is the current instance of Client authenticated as a Wikibase user?
358 359 360 361 362 |
# File 'lib/wikidatum/client.rb', line 358 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?
374 375 376 |
# File 'lib/wikidatum/client.rb', line 374 def bot? @bot end |
#delete_statement(id:, tags: [], comment: nil) ⇒ Boolean
Delete a statement from an item.
345 346 347 348 349 350 351 352 353 |
# File 'lib/wikidatum/client.rb', line 345 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.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/wikidatum/client.rb', line 103 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 |
#labels(id:) ⇒ Array<Wikidatum::Term>
Get labels for an item from the Wikibase API based on the item’s QID.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/wikidatum/client.rb', line 125 def labels(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}/labels") puts JSON.pretty_generate(response) if ENV['DEBUG'] response.to_a.map { |lang, val| Wikidatum::Term.new(lang: lang, value: val) } end |
#statement(id:) ⇒ Wikidatum::Statement
Get a statement from the Wikibase API based on its ID.
144 145 146 147 148 149 150 151 152 |
# File 'lib/wikidatum/client.rb', line 144 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 |