Module: MediawikiSelenium::ApiHelper

Defined in:
lib/mediawiki_selenium/helpers/api_helper.rb

Overview

Provides more direct access to the API client from hooks and step definitions.

Instance Method Summary collapse

Instance Method Details

#apiMediawikiApi::Client

An authenticated MediaWiki API client.

Returns:

  • (MediawikiApi::Client)


12
13
14
15
16
17
18
19
20
# File 'lib/mediawiki_selenium/helpers/api_helper.rb', line 12

def api
  @_api_cache ||= {}

  url = api_url

  @_api_cache[[url, user]] ||= MediawikiApi::Client.new(url).tap do |client|
    client.(user, password)
  end
end

#api_urlString

URL to the API endpoint for the current wiki.

Returns:

  • (String)


26
27
28
# File 'lib/mediawiki_selenium/helpers/api_helper.rb', line 26

def api_url
  lookup(:mediawiki_api_url, default: -> { api_url_from(lookup(:mediawiki_url)) })
end

#ensure_account(id) ⇒ Object

Ensures the given alternative account exists by attempting to create it via the API. Any errors related to the account already existing are swallowed.

Parameters:

  • id (Symbol)

    ID of alternative user.



51
52
53
54
55
# File 'lib/mediawiki_selenium/helpers/api_helper.rb', line 51

def (id)
  api.(user(id), password(id))
rescue MediawikiApi::ApiError => e
  raise e unless e.code == 'userexists'
end

#missing_extensions(dependencies) ⇒ Array

Queries MW via the API to check for missing extension dependencies.

Parameters:

  • dependencies (Array)

    Extension dependencies.

Returns:

  • (Array)

    Missing extensions.



36
37
38
39
40
41
42
43
# File 'lib/mediawiki_selenium/helpers/api_helper.rb', line 36

def missing_extensions(dependencies)
  return [] if dependencies.empty?

  extensions = api.meta(:siteinfo, siprop: 'extensions').data['extensions']
  extensions = extensions.map { |ext| ext['name'] }.compact.map(&:downcase)

  dependencies - extensions
end

#on_wiki(id) {|wiki_url, api_url| ... } ⇒ Object

Extends parent implementation to also override the API URL. If no API URL is explicitly defined for the given alternative, one is constructed relative to the wiki URL.

Yields:

Yield Parameters:

  • wiki_url (String)

    Alternative wiki URL.

  • api_url (String)

    Alternative API URL.

See Also:



67
68
69
70
71
72
# File 'lib/mediawiki_selenium/helpers/api_helper.rb', line 67

def on_wiki(id, &blk)
  super(id) do |wiki_url|
    api_url = lookup(:mediawiki_api_url, id: id, default: -> { api_url_from(wiki_url) })
    return with(mediawiki_url: wiki_url, mediawiki_api_url: api_url, &blk)
  end
end