wikidatum 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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