gimme_wikidata 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ module GimmeWikidata
2
+ require 'colorize'
3
+
4
+ class Entity
5
+ ##
6
+ # Prints a pretty version of the Entity to console
7
+ def print(colour = :blue)
8
+ heading = "#{label} (#{id})"
9
+ puts '=' * heading.length
10
+ puts "#{label} (#{id})".bold.colorize(background: colour)
11
+ puts '=' * heading.length
12
+ puts "Description\t".bold + @description
13
+ puts "Aliases\t\t".bold + @aliases.join(', ')
14
+ puts "Claims:".underline
15
+ claims.each {|sc| sc.print }
16
+ return nil
17
+ end
18
+ end
19
+
20
+ class Claim
21
+ ##
22
+ # Prints a pretty version of the Claim to the console
23
+ def print(colour = :blue)
24
+ simple = simplify
25
+ puts "#{simple[:property]}: ".bold.colorize(color: colour) + "#{simple[:value].to_s}"
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,18 @@
1
+ require 'gimme_wikidata/entity'
2
+
3
+ module GimmeWikidata
4
+
5
+ ##
6
+ # Models an Property on Wikidata, which is a kind of Entity
7
+ #
8
+ # Please see: https://www.wikidata.org/wiki/Wikidata:Glossary#Entities.2C_items.2C_properties_and_queries for more details
9
+ class Property < Entity
10
+
11
+ def initialize(id, label = nil, description = nil, aliases = nil, claims = [])
12
+ throw ArgumentError.new "Invalid Wikidata Property id: #{id}" unless GimmeWikidata.valid_id?(id, [:property])
13
+ super(id, label, description, aliases, claims)
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,55 @@
1
+ module GimmeWikidata
2
+
3
+ ##
4
+ # TODO: DOCUMENT THIS CLASS
5
+ class Search
6
+
7
+ attr_reader :search_term, :success, :error
8
+ attr_accessor :results
9
+
10
+ def initialize(success, error, search_term, results = [])
11
+ @search_term = search_term
12
+ @success = (success == 1)
13
+ @results = results
14
+ @error = error
15
+ end
16
+
17
+ def was_successful?
18
+ @success
19
+ end
20
+
21
+ def empty?
22
+ results.empty?
23
+ end
24
+
25
+ def top_result
26
+ @results.first
27
+ end
28
+
29
+ end
30
+
31
+ ##
32
+ # TODO: DOCUMENT THIS CLASS
33
+ class SearchResult
34
+
35
+ attr_accessor :id, :type, :label, :description
36
+
37
+ def initialize(id, label, description)
38
+ @id = id
39
+ @label = label
40
+ @description = description
41
+
42
+ case @id[0]
43
+ when 'Q'
44
+ @type = Item
45
+ when 'P'
46
+ @type = Property
47
+ else
48
+ @type = :unknown_type
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+
55
+ end
@@ -0,0 +1,3 @@
1
+ module GimmeWikidata
2
+ VERSION = "0.4.2"
3
+ end
@@ -0,0 +1,123 @@
1
+ require 'httparty'
2
+ require 'gimme_wikidata/entity'
3
+ require 'gimme_wikidata/enums'
4
+
5
+ module GimmeWikidata
6
+
7
+ ##
8
+ # Responsible for communication with the Wikidata API.
9
+ #
10
+ # Handles the language and formatting of API calls, and the making of calls.
11
+ #
12
+ # Not responsible for making sense of the responses; for that, see Parser.
13
+ #
14
+ # Currently supported API actions:
15
+ # - +wbsearchentities+ -> https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities
16
+ # - +wbgetentities+ -> https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities
17
+ class WikidataAPI
18
+
19
+ include SymbolizeHelper
20
+
21
+ ##
22
+ # The base API URL: https://www.wikidata.org/w/api.php?
23
+ APIURL = 'https://www.wikidata.org/w/api.php?'
24
+
25
+ ##
26
+ # The format in which to pull data (json)
27
+ FORMAT = 'json'
28
+
29
+ ##
30
+ # The current language to use in all communication with the Wikidata API.
31
+ @@language = Languages::ENGLISH
32
+
33
+ ##
34
+ # Set the language to use when communicating to the the WikidataAPI.
35
+ #
36
+ # * *Parameters* :
37
+ # - +language_symbol+ -> A symbol from the Languages Enum class
38
+ # * *Returns* :
39
+ # - The string format of the language it is set to (or, of the current language if *not* set successfully)
40
+ def self.set_language(language_symbol)
41
+ new_lang = Languages.to_h[language_symbol]
42
+ @@language = new_lang unless new_lang.nil?
43
+ @@language
44
+ end
45
+
46
+ ##
47
+ # Gets the language used to communicate to the Wikidata API
48
+ def self.get_language
49
+ @@language
50
+ end
51
+
52
+ ##
53
+ # Builds a search query.
54
+ #
55
+ # The query is described here: https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities
56
+ #
57
+ # * *Parameters* :
58
+ # - +search+ -> the search term to look for
59
+ # - +language+ -> the language in which to search
60
+ # - +strict_language+ -> disable language fallback or not
61
+ # - +type+ -> either 'item' or 'property'
62
+ # - +limit+ -> maximum number of things returned
63
+ # - +continue+ -> offset of things
64
+ # * *Returns* :
65
+ # - A +wbsearchentities+ query to be used in a Wikidata API call
66
+ def self.search_query (search = "wikidata", strict_language: false, type: 'item', limit: 50, continue: 0)
67
+ url = [APIURL]
68
+ url << "action=#{Actions::SEARCH}"
69
+ url << "&format=#{FORMAT}"
70
+ url << "&search=#{search}"
71
+ url << "&language=#{@@language}"
72
+ url << "&strictlanguage=#{strict_language}" unless strict_language == false
73
+ url << "&type=#{type}" unless type == 'item'
74
+ url << "&limit=#{limit}" unless limit == 50
75
+ url << "&continue=#{continue}" unless continue == 0
76
+ url.join
77
+ end
78
+
79
+ ##
80
+ # Build a query to get Entities (Items and Properties) from Wikidata.
81
+ #
82
+ # The query is described here: https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities
83
+ #
84
+ # * *Parameters* :
85
+ # - +ids+ -> an array of strings representing the ids of the entities on Wikidata.
86
+ # - +props+ -> the properties to get. See the Props class.
87
+ # * *Returns* :
88
+ # - A +wbgetentities+ query to be used in a Wikidata API call
89
+ def self.get_entities_query(ids = ['Q1'], props: [Props::LABELS, Props::DESCRIPTIONS, Props::ALIASES])
90
+ raise ArgumentError.new("Ids must be an array of Wikidata ids; it was: #{ids}") unless GimmeWikidata::valid_ids? ids
91
+ url = [base_url]
92
+ url << '&action=' << Actions::GET_ENTITIES
93
+ url << '&ids=' << ids.join('|')
94
+ url << '&props=' << props.join('|')
95
+ url.join
96
+ end
97
+
98
+ ##
99
+ # Helper function to build a commonly-used URL. Appends default format and language to the base Wikidata API URL
100
+ def self.base_url
101
+ url = [APIURL]
102
+ url << 'format=' << FORMAT
103
+ url << '&languages=' << @@language
104
+ url.join
105
+ end
106
+
107
+ ##
108
+ # Makes a call to the Wikidata API and formats the response into a symbolized hash
109
+ #
110
+ # Uses HTTParty gem
111
+ #
112
+ # * *Parameters*:
113
+ # - +query+ -> The query for the API call
114
+ # * *Returns* :
115
+ # - A hash representation of the API's response
116
+ def self.make_call(query)
117
+ response = HTTParty.get(query).to_h
118
+ SymbolizeHelper.symbolize_recursive(response)
119
+ end
120
+
121
+ end
122
+
123
+ end
metadata ADDED
@@ -0,0 +1,384 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gimme_wikidata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.2
5
+ platform: ruby
6
+ authors:
7
+ - Bradley Marques
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 11.1.2
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: 11.1.2
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 11.1.2
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 11.1.2
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.11.2
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.11.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 0.11.2
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.11.2
67
+ - !ruby/object:Gem::Dependency
68
+ name: minitest-display
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 0.3.1
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 0.3.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 0.3.1
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: 0.3.1
87
+ - !ruby/object:Gem::Dependency
88
+ name: minitest-reporters
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: 1.1.9
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.1.9
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.1.9
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 1.1.9
107
+ - !ruby/object:Gem::Dependency
108
+ name: m
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 1.5.0
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 1.5.0
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 1.5.0
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: 1.5.0
127
+ - !ruby/object:Gem::Dependency
128
+ name: pry
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: 0.10.3
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 0.10.3
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: 0.10.3
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 0.10.3
147
+ - !ruby/object:Gem::Dependency
148
+ name: rdoc
149
+ requirement: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: 4.2.2
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: 4.2.2
157
+ type: :development
158
+ prerelease: false
159
+ version_requirements: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - "~>"
162
+ - !ruby/object:Gem::Version
163
+ version: 4.2.2
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: 4.2.2
167
+ - !ruby/object:Gem::Dependency
168
+ name: mocha
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 1.1.0
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: 1.1.0
177
+ type: :development
178
+ prerelease: false
179
+ version_requirements: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - "~>"
182
+ - !ruby/object:Gem::Version
183
+ version: 1.1.0
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: 1.1.0
187
+ - !ruby/object:Gem::Dependency
188
+ name: dotenv
189
+ requirement: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - "~>"
192
+ - !ruby/object:Gem::Version
193
+ version: 2.0.2
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: 2.0.2
197
+ type: :development
198
+ prerelease: false
199
+ version_requirements: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - "~>"
202
+ - !ruby/object:Gem::Version
203
+ version: 2.0.2
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: 2.0.2
207
+ - !ruby/object:Gem::Dependency
208
+ name: codeclimate-test-reporter
209
+ requirement: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - "~>"
212
+ - !ruby/object:Gem::Version
213
+ version: 0.5.2
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: 0.5.2
217
+ type: :development
218
+ prerelease: false
219
+ version_requirements: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - "~>"
222
+ - !ruby/object:Gem::Version
223
+ version: 0.5.2
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ version: 0.5.2
227
+ - !ruby/object:Gem::Dependency
228
+ name: httparty
229
+ requirement: !ruby/object:Gem::Requirement
230
+ requirements:
231
+ - - "~>"
232
+ - !ruby/object:Gem::Version
233
+ version: 0.13.7
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: 0.13.7
237
+ type: :runtime
238
+ prerelease: false
239
+ version_requirements: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - "~>"
242
+ - !ruby/object:Gem::Version
243
+ version: 0.13.7
244
+ - - ">="
245
+ - !ruby/object:Gem::Version
246
+ version: 0.13.7
247
+ - !ruby/object:Gem::Dependency
248
+ name: ruby-enum
249
+ requirement: !ruby/object:Gem::Requirement
250
+ requirements:
251
+ - - "~>"
252
+ - !ruby/object:Gem::Version
253
+ version: 0.5.0
254
+ - - ">="
255
+ - !ruby/object:Gem::Version
256
+ version: 0.5.0
257
+ type: :runtime
258
+ prerelease: false
259
+ version_requirements: !ruby/object:Gem::Requirement
260
+ requirements:
261
+ - - "~>"
262
+ - !ruby/object:Gem::Version
263
+ version: 0.5.0
264
+ - - ">="
265
+ - !ruby/object:Gem::Version
266
+ version: 0.5.0
267
+ - !ruby/object:Gem::Dependency
268
+ name: colorize
269
+ requirement: !ruby/object:Gem::Requirement
270
+ requirements:
271
+ - - "~>"
272
+ - !ruby/object:Gem::Version
273
+ version: 0.7.7
274
+ - - ">="
275
+ - !ruby/object:Gem::Version
276
+ version: 0.7.7
277
+ type: :runtime
278
+ prerelease: false
279
+ version_requirements: !ruby/object:Gem::Requirement
280
+ requirements:
281
+ - - "~>"
282
+ - !ruby/object:Gem::Version
283
+ version: 0.7.7
284
+ - - ">="
285
+ - !ruby/object:Gem::Version
286
+ version: 0.7.7
287
+ - !ruby/object:Gem::Dependency
288
+ name: json
289
+ requirement: !ruby/object:Gem::Requirement
290
+ requirements:
291
+ - - "~>"
292
+ - !ruby/object:Gem::Version
293
+ version: 1.8.3
294
+ - - ">="
295
+ - !ruby/object:Gem::Version
296
+ version: 1.8.3
297
+ type: :runtime
298
+ prerelease: false
299
+ version_requirements: !ruby/object:Gem::Requirement
300
+ requirements:
301
+ - - "~>"
302
+ - !ruby/object:Gem::Version
303
+ version: 1.8.3
304
+ - - ">="
305
+ - !ruby/object:Gem::Version
306
+ version: 1.8.3
307
+ - !ruby/object:Gem::Dependency
308
+ name: carbon_date
309
+ requirement: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - "~>"
312
+ - !ruby/object:Gem::Version
313
+ version: 0.1.2
314
+ - - ">="
315
+ - !ruby/object:Gem::Version
316
+ version: 0.1.2
317
+ type: :runtime
318
+ prerelease: false
319
+ version_requirements: !ruby/object:Gem::Requirement
320
+ requirements:
321
+ - - "~>"
322
+ - !ruby/object:Gem::Version
323
+ version: 0.1.2
324
+ - - ">="
325
+ - !ruby/object:Gem::Version
326
+ version: 0.1.2
327
+ description: GimmeWikidata is a Ruby gem that provides an interface to search, pull
328
+ (and, in future, publish) data from Wikidata. It provides a number of classes that
329
+ encapsulate the data model of Wikidata.
330
+ email:
331
+ - bradmarxmoosepi@gmail.com
332
+ executables: []
333
+ extensions: []
334
+ extra_rdoc_files: []
335
+ files:
336
+ - ".gitignore"
337
+ - ".travis.yml"
338
+ - Gemfile
339
+ - LICENSE.txt
340
+ - README.md
341
+ - Rakefile
342
+ - bin/console
343
+ - bin/setup
344
+ - gimme_wikidata.gemspec
345
+ - img/gimme_wikidata_logo.png
346
+ - lib/gimme_wikidata.rb
347
+ - lib/gimme_wikidata/claim.rb
348
+ - lib/gimme_wikidata/entity.rb
349
+ - lib/gimme_wikidata/entity_result.rb
350
+ - lib/gimme_wikidata/enums.rb
351
+ - lib/gimme_wikidata/extensions.rb
352
+ - lib/gimme_wikidata/item.rb
353
+ - lib/gimme_wikidata/parser.rb
354
+ - lib/gimme_wikidata/printer.rb
355
+ - lib/gimme_wikidata/property.rb
356
+ - lib/gimme_wikidata/search.rb
357
+ - lib/gimme_wikidata/version.rb
358
+ - lib/gimme_wikidata/wikidata_api.rb
359
+ homepage: https://github.com/bradleymarques/gimme_wikidata
360
+ licenses:
361
+ - MIT
362
+ metadata:
363
+ allowed_push_host: https://rubygems.org
364
+ post_install_message:
365
+ rdoc_options: []
366
+ require_paths:
367
+ - lib
368
+ required_ruby_version: !ruby/object:Gem::Requirement
369
+ requirements:
370
+ - - ">="
371
+ - !ruby/object:Gem::Version
372
+ version: '0'
373
+ required_rubygems_version: !ruby/object:Gem::Requirement
374
+ requirements:
375
+ - - ">="
376
+ - !ruby/object:Gem::Version
377
+ version: '0'
378
+ requirements: []
379
+ rubyforge_project:
380
+ rubygems_version: 2.4.5
381
+ signing_key:
382
+ specification_version: 4
383
+ summary: Search and get data from the Wikidata open knowledge base.
384
+ test_files: []