Class: MediawikiApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mediawiki_api/client.rb

Overview

high level client for MediaWiki

Constant Summary collapse

FORMAT =
'json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, log = false) ⇒ Client

Returns a new instance of Client.


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mediawiki_api/client.rb', line 17

def initialize(url, log = false)
  @conn = Faraday.new(url: url) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.response :logger if log
    faraday.use :cookie_jar
    faraday.adapter Faraday.default_adapter
  end
  @logged_in = false
  @tokens = {}
end

Instance Attribute Details

#logged_inObject Also known as: logged_in?

Returns the value of attribute logged_in


13
14
15
# File 'lib/mediawiki_api/client.rb', line 13

def logged_in
  @logged_in
end

Instance Method Details

#action(name, params = {}) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mediawiki_api/client.rb', line 29

def action(name, params = {})
  name = name.to_s

  method = params.delete(:http_method) || :post
  token_type = params.delete(:token_type)
  envelope = (params.delete(:envelope) || [name]).map(&:to_s)

  params[:token] = get_token(token_type || name) unless token_type == false
  params = compile_parameters(params)

  send_request(method, params.merge(action: name, format: FORMAT), envelope)
end

#create_account(username, password, token = nil) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mediawiki_api/client.rb', line 42

def (username, password, token = nil)
  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 = (username, password, data['token'])
  else
    raise CreateAccountError, data['result']
  end

  data
end

#create_page(title, content) ⇒ Object


61
62
63
# File 'lib/mediawiki_api/client.rb', line 61

def create_page(title, content)
  edit(title: title, text: content)
end

#delete_page(title, reason) ⇒ Object


65
66
67
# File 'lib/mediawiki_api/client.rb', line 65

def delete_page(title, reason)
  action(:delete, title: title, reason: reason)
end

#edit(params = {}) ⇒ Object

Raises:


69
70
71
72
73
# File 'lib/mediawiki_api/client.rb', line 69

def edit(params = {})
  response = action(:edit, params)
  raise EditError, response if response.data['result'] == 'Failure'
  response
end

#get_wikitext(title) ⇒ Object


75
76
77
# File 'lib/mediawiki_api/client.rb', line 75

def get_wikitext(title)
  @conn.get '/w/index.php', action: 'raw', title: title
end

#list(type, params = {}) ⇒ Object


79
80
81
# File 'lib/mediawiki_api/client.rb', line 79

def list(type, params = {})
  subquery(:list, type, params)
end

#log_in(username, password, token = nil) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mediawiki_api/client.rb', line 83

def (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'
    data = (username, password, data['token'])
  else
    raise LoginError, data['result']
  end

  data
end

#meta(type, params = {}) ⇒ Object


102
103
104
# File 'lib/mediawiki_api/client.rb', line 102

def meta(type, params = {})
  subquery(:meta, type, params)
end

#prop(type, params = {}) ⇒ Object


106
107
108
# File 'lib/mediawiki_api/client.rb', line 106

def prop(type, params = {})
  subquery(:prop, type, params)
end

#protect_page(title, reason, protections = 'edit=sysop|move=sysop') ⇒ Object


110
111
112
# File 'lib/mediawiki_api/client.rb', line 110

def protect_page(title, reason, protections = 'edit=sysop|move=sysop')
  action(:protect, title: title, reason: reason, protections: protections)
end

#query(params = {}) ⇒ Object


114
115
116
# File 'lib/mediawiki_api/client.rb', line 114

def query(params = {})
  action(:query, { token_type: false, http_method: :get }.merge(params))
end

#unwatch_page(title) ⇒ Object


118
119
120
# File 'lib/mediawiki_api/client.rb', line 118

def unwatch_page(title)
  action(:watch, titles: title, unwatch: true)
end

#upload_image(filename, path, comment, ignorewarnings) ⇒ Object


122
123
124
125
126
127
# File 'lib/mediawiki_api/client.rb', line 122

def upload_image(filename, path, comment, ignorewarnings)
  file = Faraday::UploadIO.new(path, 'image/png')
  action(:upload,
         token_type: 'edit', filename: filename, file: file, comment: comment,
         ignorewarnings: ignorewarnings)
end

#watch_page(title) ⇒ Object


129
130
131
# File 'lib/mediawiki_api/client.rb', line 129

def watch_page(title)
  action(:watch, titles: title)
end