class GimmeWikidata::WikidataAPI
Responsible for communication with the Wikidata API.
Handles the language and formatting of API calls, and the making of calls.
Not responsible for making sense of the responses; for that, see Parser
.
Currently supported API actions:
-
wbsearchentities
-> www.wikidata.org/w/api.php?action=help&modules=wbsearchentities -
wbgetentities
-> www.wikidata.org/w/api.php?action=help&modules=wbgetentities
Constants
- APIURL
The base API URL: www.wikidata.org/w/api.php?
- FORMAT
The format in which to pull data (json)
Public Class Methods
Helper function to build a commonly-used URL. Appends default format and language to the base Wikidata API URL
# File lib/gimme_wikidata/wikidata_api.rb, line 100 def self.base_url url = [APIURL] url << 'format=' << FORMAT url << '&languages=' << @@language url.join end
Build a query to get Entities (Items and Properties) from Wikidata.
The query is described here: www.wikidata.org/w/api.php?action=help&modules=wbgetentities
-
Parameters :
-
ids
-> an array of strings representing the ids of the entities on Wikidata. -
props
-> the properties to get. See theProps
class.
-
-
Returns :
-
A
wbgetentities
query to be used in a Wikidata API call
-
# File lib/gimme_wikidata/wikidata_api.rb, line 89 def self.get_entities_query(ids = ['Q1'], props: [Props::LABELS, Props::DESCRIPTIONS, Props::ALIASES]) raise ArgumentError.new("Ids must be an array of Wikidata ids; it was: #{ids}") unless GimmeWikidata::valid_ids? ids url = [base_url] url << '&action=' << Actions::GET_ENTITIES url << '&ids=' << ids.join('|') url << '&props=' << props.join('|') url.join end
Gets the language used to communicate to the Wikidata API
# File lib/gimme_wikidata/wikidata_api.rb, line 48 def self.get_language @@language end
Makes a call to the Wikidata API and formats the response into a symbolized hash
Uses HTTParty gem
-
Parameters:
-
query
-> The query for the API call
-
-
Returns :
-
A hash representation of the API’s response
-
# File lib/gimme_wikidata/wikidata_api.rb, line 116 def self.make_call(query) response = HTTParty.get(query).to_h SymbolizeHelper.symbolize_recursive(response) end
Builds a search query.
The query is described here: www.wikidata.org/w/api.php?action=help&modules=wbsearchentities
-
Parameters :
-
search
-> the search term to look for -
language
-> the language in which to search -
strict_language
-> disable language fallback or not -
type
-> either ‘item’ or ‘property’ -
limit
-> maximum number of things returned -
continue
-> offset of things
-
-
Returns :
-
A
wbsearchentities
query to be used in a Wikidata API call
-
# File lib/gimme_wikidata/wikidata_api.rb, line 66 def self.search_query (search = "wikidata", strict_language: false, type: 'item', limit: 50, continue: 0) url = [APIURL] url << "action=#{Actions::SEARCH}" url << "&format=#{FORMAT}" url << "&search=#{search}" url << "&language=#{@@language}" url << "&strictlanguage=#{strict_language}" unless strict_language == false url << "&type=#{type}" unless type == 'item' url << "&limit=#{limit}" unless limit == 50 url << "&continue=#{continue}" unless continue == 0 url.join end
Set the language to use when communicating to the the WikidataAPI
.
-
Parameters :
-
language_symbol
-> A symbol from theLanguages
Enum class
-
-
Returns :
-
The string format of the language it is set to (or, of the current language if not set successfully)
-
# File lib/gimme_wikidata/wikidata_api.rb, line 40 def self.set_language(language_symbol) new_lang = Languages.to_h[language_symbol] @@language = new_lang unless new_lang.nil? @@language end