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
-
#api ⇒ MediawikiApi::Client
An authenticated MediaWiki API client.
-
#api_url ⇒ String
URL to the API endpoint for the current wiki.
-
#ensure_account(id) ⇒ Object
Ensures the given alternative account exists by attempting to create it via the API.
-
#missing_extensions(dependencies) ⇒ Array
Queries MW via the API to check for missing extension dependencies.
-
#on_wiki(id) {|wiki_url, api_url| ... } ⇒ Object
Extends parent implementation to also override the API URL.
Instance Method Details
#api ⇒ MediawikiApi::Client
An authenticated MediaWiki API 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.log_in(user, password) end end |
#api_url ⇒ String
URL to the API endpoint for the current wiki.
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.
51 52 53 54 55 |
# File 'lib/mediawiki_selenium/helpers/api_helper.rb', line 51 def ensure_account(id) api.create_account(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.
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.(: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.
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 |