wikidatum 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wikidatum.rb CHANGED
@@ -1,19 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'wikidatum/client'
4
- require 'wikidatum/data_value_type'
4
+ require 'wikidatum/data_type'
5
5
  require 'wikidatum/item'
6
6
  require 'wikidatum/qualifier'
7
7
  require 'wikidatum/reference'
8
+ require 'wikidatum/reference_part'
8
9
  require 'wikidatum/sitelink'
9
- require 'wikidatum/snak'
10
10
  require 'wikidatum/statement'
11
11
  require 'wikidatum/term'
12
+ require 'wikidatum/utils'
12
13
  require 'wikidatum/version'
13
14
 
14
15
  module Wikidatum
15
16
  class Error < StandardError; end
16
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
+
17
36
  # rubocop:disable Naming/ConstantName
18
37
 
19
38
  # These language codes are not enforced, you can pass whatever language code
@@ -45,17 +64,18 @@ module Wikidatum
45
64
  # rubocop:enable Naming/ConstantName
46
65
  end
47
66
 
48
- module DataValueType
67
+ module DataType
68
+ # TODO: Add commonsMedia and url to the possible types.
49
69
  # rubocop:disable Lint/SymbolConversion
50
- DATA_VALUE_TYPES = {
51
- 'globecoordinate': 'Wikidatum::DataValueType::GlobeCoordinate',
52
- 'monolingualtext': 'Wikidatum::DataValueType::MonolingualText',
53
- 'novalue': 'Wikidatum::DataValueType::NoValue',
54
- 'quantity': 'Wikidatum::DataValueType::Quantity',
55
- 'somevalue': 'Wikidatum::DataValueType::SomeValue',
56
- 'string': 'Wikidatum::DataValueType::WikibaseString',
57
- 'time': 'Wikidatum::DataValueType::Time',
58
- 'wikibase-entityid': 'Wikidatum::DataValueType::WikibaseEntityId'
70
+ DATA_TYPES = {
71
+ 'globe-coordinate': 'Wikidatum::DataType::GlobeCoordinate',
72
+ 'monolingualtext': 'Wikidatum::DataType::MonolingualText',
73
+ 'novalue': 'Wikidatum::DataType::NoValue',
74
+ 'quantity': 'Wikidatum::DataType::Quantity',
75
+ 'somevalue': 'Wikidatum::DataType::SomeValue',
76
+ 'string': 'Wikidatum::DataType::WikibaseString',
77
+ 'time': 'Wikidatum::DataType::Time',
78
+ 'wikibase-item': 'Wikidatum::DataType::WikibaseItem'
59
79
  }.freeze
60
80
  # rubocop:enable Lint/SymbolConversion
61
81
  end
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.2.1
4
+ version: 0.3.1
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-08-13 00:00:00.000000000 Z
11
+ date: 2022-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,23 +38,24 @@ files:
38
38
  - bin/setup
39
39
  - lib/wikidatum.rb
40
40
  - lib/wikidatum/client.rb
41
- - lib/wikidatum/data_value_type.rb
42
- - lib/wikidatum/data_value_type/base.rb
43
- - lib/wikidatum/data_value_type/globe_coordinate.rb
44
- - lib/wikidatum/data_value_type/monolingual_text.rb
45
- - lib/wikidatum/data_value_type/no_value.rb
46
- - lib/wikidatum/data_value_type/quantity.rb
47
- - lib/wikidatum/data_value_type/some_value.rb
48
- - lib/wikidatum/data_value_type/time.rb
49
- - lib/wikidatum/data_value_type/wikibase_entity_id.rb
50
- - lib/wikidatum/data_value_type/wikibase_string.rb
41
+ - lib/wikidatum/data_type.rb
42
+ - lib/wikidatum/data_type/base.rb
43
+ - lib/wikidatum/data_type/globe_coordinate.rb
44
+ - lib/wikidatum/data_type/monolingual_text.rb
45
+ - lib/wikidatum/data_type/no_value.rb
46
+ - lib/wikidatum/data_type/quantity.rb
47
+ - lib/wikidatum/data_type/some_value.rb
48
+ - lib/wikidatum/data_type/time.rb
49
+ - lib/wikidatum/data_type/wikibase_item.rb
50
+ - lib/wikidatum/data_type/wikibase_string.rb
51
51
  - lib/wikidatum/item.rb
52
52
  - lib/wikidatum/qualifier.rb
53
53
  - lib/wikidatum/reference.rb
54
+ - lib/wikidatum/reference_part.rb
54
55
  - lib/wikidatum/sitelink.rb
55
- - lib/wikidatum/snak.rb
56
56
  - lib/wikidatum/statement.rb
57
57
  - lib/wikidatum/term.rb
58
+ - lib/wikidatum/utils.rb
58
59
  - lib/wikidatum/version.rb
59
60
  - wikidatum.gemspec
60
61
  homepage:
@@ -81,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubygems_version: 3.2.33
85
+ rubygems_version: 3.4.1
85
86
  signing_key:
86
87
  specification_version: 4
87
88
  summary: Ruby gem for the new Wikidata REST API.
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # For more information on the possible types that can be returned by
4
- # datavalues, see the official documentation:
5
- # https://doc.wikimedia.org/Wikibase/master/php/md_docs_topics_json.html#json_datavalues
6
- module Wikidatum::DataValueType
7
- class Base
8
- # Represents the type for this datavalue instance.
9
- #
10
- # Possible values for the `type` attribute are:
11
- #
12
- # - `:no_value`: No value
13
- # - `:some_value`: Unknown value
14
- # - `:globe_coordinate`: {DataValueType::GlobeCoordinate}
15
- # - `:monolingual_text`: {DataValueType::MonolingualText}
16
- # - `:quantity`: {DataValueType::Quantity}
17
- # - `:string`: {DataValueType::WikibaseString}
18
- # - `:time`: {DataValueType::Time}
19
- # - `:wikibase_entity_id`: {DataValueType::WikibaseEntityId}
20
- #
21
- # @return [Symbol]
22
- attr_reader :type
23
-
24
- # The value of the datavalue object in the response.
25
- #
26
- # If the `type` is `novalue` or `somevalue`, this returns `nil`.
27
- #
28
- # @return [DataValueType::GlobeCoordinate, DataValueType::MonolingualText, DataValueType::Quantity, DataValueType::WikibaseString, DataValueType::Time, DataValueType::WikibaseEntityId, nil]
29
- attr_reader :value
30
-
31
- # @param type [Symbol]
32
- # @param value [DataValueType::GlobeCoordinate, DataValueType::MonolingualText, DataValueType::Quantity, DataValueType::WikibaseString, DataValueType::Time, DataValueType::WikibaseEntityId, nil] nil if type is no_value or some_value
33
- # @return [void]
34
- def initialize(type:, value:)
35
- @type = type
36
- @value = value
37
- end
38
-
39
- # @return [Hash]
40
- def to_h
41
- {
42
- type: @type,
43
- value: @value&.to_h
44
- }
45
- end
46
-
47
- # @!visibility private
48
- #
49
- # @param data_value_type [String] The value of `type` for the given Snak's datavalue.
50
- # @param data_value_json [Hash] The `value` part of datavalue.
51
- # @return [Wikidatum::DataValueType::Base] An instance of Base.
52
- def self.marshal_load(data_value_type, data_value_json)
53
- unless Wikidatum::DataValueType::DATA_VALUE_TYPES.keys.include?(data_value_type.to_sym)
54
- puts 'WARNING: Unsupported datavalue type.'
55
- return nil
56
- end
57
-
58
- Object.const_get(Wikidatum::DataValueType::DATA_VALUE_TYPES[data_value_type.to_sym]).marshal_load(data_value_json)
59
- end
60
- end
61
- end
@@ -1,92 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wikidatum/data_value_type/base'
4
-
5
- # The Quantity type datavalue JSON looks like this:
6
- #
7
- # ```json
8
- # {
9
- # "datavalue": {
10
- # "value": {
11
- # "amount": "+10.38",
12
- # "upperBound": "+10.375",
13
- # "lowerBound": "+10.385",
14
- # "unit": "http://www.wikidata.org/entity/Q712226"
15
- # },
16
- # "type": "quantity"
17
- # }
18
- # }
19
- # ```
20
- class Wikidatum::DataValueType::Quantity
21
- # @return [String] A string value like "+2", usually an integer but not always.
22
- attr_reader :amount
23
-
24
- # @return [String, nil] upper bound, if one is defined.
25
- attr_reader :upper_bound
26
-
27
- # @return [String, nil] lower bound, if one is defined.
28
- attr_reader :lower_bound
29
-
30
- # @return [String] a URL describing the unit for this quantity, e.g. "meter", "kilometer", "pound", "chapter", "section", etc.
31
- attr_reader :unit
32
-
33
- # @param amount [String]
34
- # @param upper_bound [String]
35
- # @param lower_bound [String]
36
- # @param unit [String]
37
- # @return [void]
38
- def initialize(amount:, upper_bound:, lower_bound:, unit:)
39
- @amount = amount
40
- @upper_bound = upper_bound
41
- @lower_bound = lower_bound
42
- @unit = unit
43
- end
44
-
45
- # @return [Hash]
46
- def to_h
47
- {
48
- amount: @amount,
49
- upper_bound: @upper_bound,
50
- lower_bound: @lower_bound,
51
- unit: @unit
52
- }
53
- end
54
-
55
- # The "type" value used by Wikibase, for use when creating/updating statements.
56
- #
57
- # @return [String]
58
- def wikibase_type
59
- 'quantity'
60
- end
61
-
62
- # The "datatype" value used by Wikibase, usually identical to wikibase_type
63
- # but not always.
64
- #
65
- # @return [String]
66
- def wikibase_datatype
67
- wikibase_type
68
- end
69
-
70
- # @!visibility private
71
- def self.marshal_load(data_value_json)
72
- Wikidatum::DataValueType::Base.new(
73
- type: :quantity,
74
- value: new(
75
- amount: data_value_json['amount'],
76
- upper_bound: data_value_json['upperBound'],
77
- lower_bound: data_value_json['lowerBound'],
78
- unit: data_value_json['unit']
79
- )
80
- )
81
- end
82
-
83
- # @!visibility private
84
- def marshal_dump
85
- {
86
- amount: @amount,
87
- upperBound: @upper_bound,
88
- lowerBound: @lower_bound,
89
- unit: @unit
90
- }
91
- end
92
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wikidatum/data_value_type/base'
4
-
5
- # The Wikibase Entity ID type datavalue JSON looks like this:
6
- #
7
- # ```json
8
- # {
9
- # "datavalue": {
10
- # "value": {
11
- # "entity-type": "item",
12
- # "numeric-id": 552863,
13
- # "id": "Q552863"
14
- # },
15
- # "type": "wikibase-entityid"
16
- # }
17
- # }
18
- # ```
19
- class Wikidatum::DataValueType::WikibaseEntityId
20
- # @return [String] usually "item"
21
- attr_reader :entity_type
22
-
23
- # @return [Integer] the integer representation of the Wikibase ID.
24
- attr_reader :numeric_id
25
-
26
- # @return [String] in the format "Q123".
27
- attr_reader :id
28
-
29
- # @param entity_type [String]
30
- # @param numeric_id [Integer]
31
- # @param id [String]
32
- # @return [void]
33
- def initialize(entity_type:, numeric_id:, id:)
34
- @entity_type = entity_type
35
- @numeric_id = numeric_id
36
- @id = id
37
- end
38
-
39
- # @return [Hash]
40
- def to_h
41
- {
42
- entity_type: @entity_type,
43
- numeric_id: @numeric_id,
44
- id: @id
45
- }
46
- end
47
-
48
- # The "type" value used by Wikibase, for use when creating/updating statements.
49
- #
50
- # @return [String]
51
- def wikibase_type
52
- 'wikibase-entityid'
53
- end
54
-
55
- # The "datatype" value used by Wikibase, usually identical to wikibase_type
56
- # but not always.
57
- #
58
- # @return [String]
59
- def wikibase_datatype
60
- 'wikibase-item' # yes, really
61
- end
62
-
63
- # @!visibility private
64
- def self.marshal_load(data_value_json)
65
- Wikidatum::DataValueType::Base.new(
66
- type: :wikibase_entity_id,
67
- value: new(
68
- entity_type: data_value_json['entity-type'],
69
- numeric_id: data_value_json['numeric-id'],
70
- id: data_value_json['id']
71
- )
72
- )
73
- end
74
-
75
- # @!visibility private
76
- def marshal_dump
77
- {
78
- 'entity-type': @entity_type,
79
- 'numeric-id': @numeric_id,
80
- id: @id
81
- }
82
- end
83
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wikidatum/data_value_type/base'
4
- require 'wikidatum/data_value_type/globe_coordinate'
5
- require 'wikidatum/data_value_type/monolingual_text'
6
- require 'wikidatum/data_value_type/no_value'
7
- require 'wikidatum/data_value_type/quantity'
8
- require 'wikidatum/data_value_type/some_value'
9
- require 'wikidatum/data_value_type/time'
10
- require 'wikidatum/data_value_type/wikibase_entity_id'
11
- require 'wikidatum/data_value_type/wikibase_string'
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Wikidatum::Snak
4
- # @return [String] Hash of a snak (a cryptographic hash, not a Ruby hash).
5
- attr_reader :hash
6
-
7
- # @return [String]
8
- attr_reader :snaktype
9
-
10
- # @return [String] ID of the property for this Snak, in the format 'P123'.
11
- attr_reader :property
12
-
13
- # @return [String]
14
- attr_reader :datatype
15
-
16
- # For more information on the possible types that can be returned by
17
- # datavalues, see the official documentation:
18
- # https://doc.wikimedia.org/Wikibase/master/php/md_docs_topics_json.html#json_datavalues
19
- #
20
- # @return [Wikidatum::DataValueType::Base] the value of the statement, can take various forms
21
- attr_reader :datavalue
22
-
23
- # @!visibility private
24
- # @param hash [String] Hash of a snak (a cryptographic hash, not a Ruby hash).
25
- # @param snaktype [String]
26
- # @param property [String] ID of the property for this Snak, in the format 'P123'.
27
- # @param datatype [String]
28
- # @param datavalue [DataValueType::GlobeCoordinate, DataValueType::MonolingualText, DataValueType::Quantity, DataValueType::WikibaseString, DataValueType::Time, DataValueType::WikibaseEntityId]
29
- def initialize(hash:, snaktype:, property:, datatype:, datavalue:)
30
- @hash = hash
31
- @snaktype = snaktype
32
- @property = property
33
- @datatype = datatype
34
- @datavalue = datavalue
35
- end
36
-
37
- # @return [Hash]
38
- def to_h
39
- {
40
- hash: @hash,
41
- snaktype: @snaktype,
42
- property: @property,
43
- datatype: @datatype,
44
- datavalue: @datavalue.to_h
45
- }
46
- end
47
-
48
- # @return [String]
49
- def inspect
50
- "<Wikidatum::Snak hash=#{@hash.inspect} snaktype=#{@snaktype.inspect} property=#{@property.inspect} datatype=#{@datatype.inspect} datavalue=#{@datavalue.inspect}>"
51
- end
52
-
53
- # @!visibility private
54
- #
55
- # This takes in the JSON blob (as a hash) that is output for a given
56
- # snak in the API and turns it into an actual instance of a Snak.
57
- #
58
- # @param snak_json [Hash]
59
- # @return [Wikidatum::Snak]
60
- def self.marshal_load(snak_json)
61
- # snaktype can be 'novalue' (no value) or 'somevalue' (unknown), so we handle those as somewhat special cases
62
- case snak_json['snaktype']
63
- when 'novalue'
64
- datavalue = Wikidatum::DataValueType::Base.marshal_load('novalue', nil)
65
- when 'somevalue'
66
- datavalue = Wikidatum::DataValueType::Base.marshal_load('somevalue', nil)
67
- when 'value'
68
- datavalue = Wikidatum::DataValueType::Base.marshal_load(snak_json['datavalue']['type'], snak_json['datavalue']['value'])
69
- end
70
-
71
- Wikidatum::Snak.new(
72
- hash: snak_json['hash'],
73
- snaktype: snak_json['snaktype'],
74
- property: snak_json['property'],
75
- datatype: snak_json['datatype'],
76
- datavalue: datavalue
77
- )
78
- end
79
- end