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:

Constants

APIURL

The base API URL: www.wikidata.org/w/api.php?

FORMAT

The format in which to pull data (json)

Public Class Methods

base_url() click to toggle source

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
get_entities_query(ids = ['Q1'], props: [Props::LABELS, Props::DESCRIPTIONS, Props::ALIASES]) click to toggle source

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 the Props 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
get_language() click to toggle source

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
make_call(query) click to toggle source

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
search_query(search = "wikidata", strict_language: false, type: 'item', limit: 50, continue: 0) click to toggle source

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_language(language_symbol) click to toggle source

Set the language to use when communicating to the the WikidataAPI.

  • Parameters :

    • language_symbol -> A symbol from the Languages 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