Class: MediawikiApi::Client
- Inherits:
-
Object
- Object
- MediawikiApi::Client
- Defined in:
- lib/mediawiki_api/client.rb
Overview
high level client for MediaWiki
Constant Summary collapse
- FORMAT =
'json'
Instance Attribute Summary collapse
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#logged_in ⇒ Object
(also: #logged_in?)
Returns the value of attribute logged_in.
Instance Method Summary collapse
- #action(name, params = {}) ⇒ Object
- #create_account(username, password) ⇒ Object
- #create_account_new(username, password) ⇒ Object
- #create_account_old(username, password, token = nil) ⇒ Object
- #create_page(title, content) ⇒ Object
- #delete_page(title, reason) ⇒ Object
- #edit(params = {}) ⇒ Object
- #get_wikitext(title) ⇒ Object
-
#initialize(url, log: false) ⇒ Client
constructor
A new instance of Client.
- #list(type, params = {}) ⇒ Object
- #log_in(username, password, token = nil) ⇒ Object
- #meta(type, params = {}) ⇒ Object
-
#oauth_access_token(access_token) ⇒ Object
set the OAuth access token to be used for all subsequent actions (obtaining the token is up to you).
- #prop(type, params = {}) ⇒ Object
- #protect_page(title, reason, protections = 'edit=sysop|move=sysop') ⇒ Object
- #query(params = {}) ⇒ Object
- #unwatch_page(title) ⇒ Object
- #upload_image(filename, path, comment, ignorewarnings, text = nil) ⇒ Object
- #watch_page(title) ⇒ Object
Constructor Details
#initialize(url, log: false) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mediawiki_api/client.rb', line 20 def initialize(url, log: false) @cookies = HTTP::CookieJar.new @conn = Faraday.new(url: url) do |faraday| faraday.request :multipart faraday.request :url_encoded faraday.response :logger if log faraday.use :cookie_jar, jar: @cookies faraday.response :follow_redirects faraday.adapter Faraday.default_adapter end @logged_in = false @tokens = {} end |
Instance Attribute Details
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
15 16 17 |
# File 'lib/mediawiki_api/client.rb', line 15 def @cookies end |
#logged_in ⇒ Object Also known as: logged_in?
Returns the value of attribute logged_in.
16 17 18 |
# File 'lib/mediawiki_api/client.rb', line 16 def logged_in @logged_in end |
Instance Method Details
#action(name, params = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/mediawiki_api/client.rb', line 36 def action(name, params = {}) raw_action(name, params) rescue ApiError => e # propagate the exception raise unless e.code == 'badtoken' @tokens.clear # ensure fresh token on re-try raw_action(name, params) # no rescue this time; only re-try once. end |
#create_account(username, password) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mediawiki_api/client.rb', line 51 def create_account(username, password) params = { modules: 'createaccount', token_type: false } d = action(:paraminfo, params).data params = d['modules'] && d['modules'][0] && d['modules'][0]['parameters'] raise CreateAccountError, 'unexpected API response format' if !params || !params.map params = params.map{ |o| o['name'] } if params.include? 'requests' create_account_new(username, password) else create_account_old(username, password) end end |
#create_account_new(username, password) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mediawiki_api/client.rb', line 65 def create_account_new(username, password) # post-AuthManager data = action(:query, { meta: 'tokens', type: 'createaccount', token_type: false }).data token = data['tokens'] && data['tokens']['createaccounttoken'] raise CreateAccountError, 'failed to get createaccount API token' unless token data = action(:createaccount, { username: username, password: password, retype: password, createreturnurl: 'http://example.com', # won't be used but must be a valid URL createtoken: token, token_type: false }).data raise CreateAccountError, data['message'] if data['status'] != 'PASS' data end |
#create_account_old(username, password, token = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mediawiki_api/client.rb', line 83 def create_account_old(username, password, token = nil) # pre-AuthManager params = { name: username, password: password, token_type: false } params[:token] = token unless token.nil? data = action(:createaccount, params).data case data['result'] when 'Success' @logged_in = true @tokens.clear when 'NeedToken' data = create_account_old(username, password, data['token']) else raise CreateAccountError, data['result'] end data end |
#create_page(title, content) ⇒ Object
103 104 105 |
# File 'lib/mediawiki_api/client.rb', line 103 def create_page(title, content) edit(title: title, text: content) end |
#delete_page(title, reason) ⇒ Object
107 108 109 |
# File 'lib/mediawiki_api/client.rb', line 107 def delete_page(title, reason) action(:delete, title: title, reason: reason) end |
#edit(params = {}) ⇒ Object
111 112 113 114 115 |
# File 'lib/mediawiki_api/client.rb', line 111 def edit(params = {}) response = action(:edit, params) raise EditError, response if response.data['result'] == 'Failure' response end |
#get_wikitext(title) ⇒ Object
117 118 119 |
# File 'lib/mediawiki_api/client.rb', line 117 def get_wikitext(title) @conn.get '/w/index.php', action: 'raw', title: title end |
#list(type, params = {}) ⇒ Object
121 122 123 |
# File 'lib/mediawiki_api/client.rb', line 121 def list(type, params = {}) subquery(:list, type, params) end |
#log_in(username, password, token = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mediawiki_api/client.rb', line 125 def log_in(username, password, token = nil) params = { lgname: username, lgpassword: password, token_type: false } params[:lgtoken] = token unless token.nil? data = action(:login, params).data case data['result'] when 'Success' @logged_in = true @tokens.clear when 'NeedToken' raise LoginError, "failed to log in with the returned token '#{token}'" unless token.nil? data = log_in(username, password, data['token']) else raise LoginError, data['result'] end data end |
#meta(type, params = {}) ⇒ Object
145 146 147 |
# File 'lib/mediawiki_api/client.rb', line 145 def (type, params = {}) subquery(:meta, type, params) end |
#oauth_access_token(access_token) ⇒ Object
set the OAuth access token to be used for all subsequent actions (obtaining the token is up to you)
47 48 49 |
# File 'lib/mediawiki_api/client.rb', line 47 def oauth_access_token(access_token) @conn.headers['Authorization'] = "Bearer #{access_token}" end |
#prop(type, params = {}) ⇒ Object
149 150 151 |
# File 'lib/mediawiki_api/client.rb', line 149 def prop(type, params = {}) subquery(:prop, type, params) end |
#protect_page(title, reason, protections = 'edit=sysop|move=sysop') ⇒ Object
153 154 155 |
# File 'lib/mediawiki_api/client.rb', line 153 def protect_page(title, reason, protections = 'edit=sysop|move=sysop') action(:protect, title: title, reason: reason, protections: protections) end |
#query(params = {}) ⇒ Object
157 158 159 |
# File 'lib/mediawiki_api/client.rb', line 157 def query(params = {}) action(:query, { token_type: false, http_method: :get }.merge(params)) end |
#unwatch_page(title) ⇒ Object
161 162 163 |
# File 'lib/mediawiki_api/client.rb', line 161 def unwatch_page(title) action(:watch, token_type: 'watch', titles: title, unwatch: true) end |
#upload_image(filename, path, comment, ignorewarnings, text = nil) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/mediawiki_api/client.rb', line 165 def upload_image(filename, path, comment, ignorewarnings, text = nil) file = Faraday::UploadIO.new(path, 'image/png') action(:upload, filename: filename, file: file, comment: comment, text: text, ignorewarnings: ignorewarnings) end |
#watch_page(title) ⇒ Object
172 173 174 |
# File 'lib/mediawiki_api/client.rb', line 172 def watch_page(title) action(:watch, token_type: 'watch', titles: title) end |