mediawiki_api 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitreview +1 -1
- data/.rspec +1 -0
- data/CREDITS +1 -0
- data/Gemfile +6 -1
- data/Gemfile.lock +50 -0
- data/README.md +11 -25
- data/lib/mediawiki_api.rb +1 -125
- data/lib/mediawiki_api/client.rb +84 -0
- data/lib/mediawiki_api/version.rb +1 -1
- data/mediawiki_api.gemspec +13 -6
- data/spec/client_spec.rb +154 -0
- data/spec/spec_helper.rb +13 -0
- metadata +84 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee68987be1e85db0875d5297685725206b42b5ef
|
4
|
+
data.tar.gz: 8f8aef9f9d0df4ab0fe3496e14ec4f0922239032
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f6ae5b44f596d9704c1021c45bc774cb0d60b22c53c0ebce317d06230418630597656eb81655c29e33272fd0c1a06fe08bd775e1fe6f8662d423a2bc325ab77
|
7
|
+
data.tar.gz: d3ed97d5956c63342b260285c2f3f6ff30c436f72c5980e6ffe9988b2cba02bb66e2ce3c21d26a058f7c019023307fb4539ba0f0126db43df8782f04f23f6614
|
data/.gitreview
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CREDITS
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mediawiki_api (0.1.0)
|
5
|
+
faraday (~> 0.9.0)
|
6
|
+
faraday-cookie_jar (~> 0.0.6)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.3.5)
|
12
|
+
crack (0.4.2)
|
13
|
+
safe_yaml (~> 1.0.0)
|
14
|
+
diff-lcs (1.2.5)
|
15
|
+
domain_name (0.5.15)
|
16
|
+
unf (>= 0.0.5, < 1.0.0)
|
17
|
+
faraday (0.9.0)
|
18
|
+
multipart-post (>= 1.2, < 3)
|
19
|
+
faraday-cookie_jar (0.0.6)
|
20
|
+
faraday (>= 0.7.4)
|
21
|
+
http-cookie (~> 1.0.0)
|
22
|
+
http-cookie (1.0.2)
|
23
|
+
domain_name (~> 0.5)
|
24
|
+
multipart-post (2.0.0)
|
25
|
+
rake (10.1.1)
|
26
|
+
rspec (2.14.1)
|
27
|
+
rspec-core (~> 2.14.0)
|
28
|
+
rspec-expectations (~> 2.14.0)
|
29
|
+
rspec-mocks (~> 2.14.0)
|
30
|
+
rspec-core (2.14.7)
|
31
|
+
rspec-expectations (2.14.5)
|
32
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
33
|
+
rspec-mocks (2.14.5)
|
34
|
+
safe_yaml (1.0.1)
|
35
|
+
unf (0.1.3)
|
36
|
+
unf_ext
|
37
|
+
unf_ext (0.0.6)
|
38
|
+
webmock (1.17.2)
|
39
|
+
addressable (>= 2.2.7)
|
40
|
+
crack (>= 0.3.2)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (~> 1.3)
|
47
|
+
mediawiki_api!
|
48
|
+
rake
|
49
|
+
rspec (~> 2.14.1)
|
50
|
+
webmock (~> 1.17.2)
|
data/README.md
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
# MediawikiApi
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
An easy way to work with MediaWiki API from Ruby. Uses REST Client Ruby gem to communicate with MediaWiki API.
|
3
|
+
A library for interacting with MediaWiki API from Ruby. Uses adapter-agnostic
|
4
|
+
Faraday gem to talk to the API.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
9
8
|
Add this line to your application's Gemfile:
|
10
9
|
|
11
|
-
gem
|
10
|
+
gem "mediawiki_api"
|
12
11
|
|
13
12
|
And then execute:
|
14
13
|
|
@@ -20,32 +19,19 @@ Or install it yourself as:
|
|
20
19
|
|
21
20
|
## Usage
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
$ export API_URL=http://en.wikipedia.beta.wmflabs.org/w/api.php
|
26
|
-
|
27
|
-
$ irb
|
28
|
-
|
29
|
-
> require "mediawiki_api"
|
30
|
-
=> true
|
31
|
-
|
32
|
-
> include MediawikiApi
|
33
|
-
=> Object
|
34
|
-
|
35
|
-
> create_article "username", "password", "title", "content"
|
36
|
-
=> nil
|
37
|
-
|
38
|
-
> delete_article "username", "password", "title"
|
39
|
-
=> nil
|
22
|
+
```ruby
|
23
|
+
require "mediawiki_api"
|
40
24
|
|
41
|
-
|
42
|
-
|
43
|
-
|
25
|
+
client = MediawikiApi::Client.new("http://127.0.0.1/w/api.php")
|
26
|
+
client.log_in("username", "pass")
|
27
|
+
client.create_page("Test api", "lalala '''test'''")
|
28
|
+
client.delete_page("Test api", "reason for deleting")
|
29
|
+
```
|
44
30
|
|
45
31
|
## Contributing
|
46
32
|
|
47
33
|
1. Fork it ( http://github.com/<my-github-username>/mediawiki_api/fork )
|
48
34
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
-
3. Commit your changes (`git commit -am
|
35
|
+
3. Commit your changes (`git commit -am "Add some feature"`)
|
50
36
|
4. Push to the branch (`git push origin my-new-feature`)
|
51
37
|
5. Create new Pull Request
|
data/lib/mediawiki_api.rb
CHANGED
@@ -1,125 +1 @@
|
|
1
|
-
require "mediawiki_api/
|
2
|
-
require "rest-client"
|
3
|
-
|
4
|
-
module MediawikiApi
|
5
|
-
def create_article username, password, title, content
|
6
|
-
# First request is solely to obtain a valid login token in the API response.
|
7
|
-
login_token_response = RestClient.post ENV["API_URL"], {:action => "login", :lgname => username, :lgpassword => password, :format => "json", :lgtoken => ""}
|
8
|
-
|
9
|
-
login_token_data = JSON.parse(login_token_response.body)
|
10
|
-
login_token = login_token_data["login"]["token"]
|
11
|
-
cookie = login_token_response.cookies
|
12
|
-
|
13
|
-
# Second request repeats the first request with the addition of the token (to complete the login).
|
14
|
-
complete_login_response = RestClient.post ENV["API_URL"], {:action => "login", :lgname => username, :lgpassword => password, :format => "json", :lgtoken => login_token}, {:cookies => cookie}
|
15
|
-
|
16
|
-
complete_login_data = JSON.parse(complete_login_response.body)
|
17
|
-
complete_login_status = complete_login_data["login"]["result"]
|
18
|
-
complete_login_cookie = complete_login_response.cookies
|
19
|
-
# Merge the two cookie hashes together into one big super-cookie
|
20
|
-
cookie = complete_login_cookie.merge(cookie)
|
21
|
-
|
22
|
-
if (complete_login_status != "Success")
|
23
|
-
$stderr.puts "There was a problem - login was NOT successful."
|
24
|
-
end
|
25
|
-
|
26
|
-
# First request is solely to obtain a valid edit token in the API response.
|
27
|
-
edit_token_response = RestClient.post ENV["API_URL"], {:action => "tokens", :type => "edit", :format => "json"}, {:cookies => cookie}
|
28
|
-
|
29
|
-
edit_token_data = JSON.parse(edit_token_response.body)
|
30
|
-
edit_token = edit_token_data["tokens"]["edittoken"]
|
31
|
-
|
32
|
-
# Second request repeats the first request with the addition of the token (to complete the article creation).
|
33
|
-
complete_edit_response = RestClient.post ENV["API_URL"], {:action => "edit", :title => title, :text => (content + "[[Category:Browsertest article]]"), :summary => "Article created via an API 'edit' call.", :format => "json", :token => edit_token}, {:cookies => cookie}
|
34
|
-
|
35
|
-
complete_edit_data = JSON.parse(complete_edit_response.body)
|
36
|
-
complete_edit_status = complete_edit_data["edit"]["result"]
|
37
|
-
|
38
|
-
if (complete_edit_status != "Success")
|
39
|
-
$stderr.puts "There was a problem - new article creation was NOT successful."
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def delete_article username, password, title
|
44
|
-
unless (ENV["API_URL"])
|
45
|
-
puts "API_URL is not defined - make sure to export a value for that variable before running this test."
|
46
|
-
end
|
47
|
-
|
48
|
-
# First request is solely to obtain a valid login token in the API response.
|
49
|
-
login_token_response = RestClient.post ENV["API_URL"], {
|
50
|
-
"format" => "json",
|
51
|
-
"action" => "login",
|
52
|
-
"lgname" => username,
|
53
|
-
"lgpassword" => password,
|
54
|
-
}
|
55
|
-
|
56
|
-
login_token_data = JSON.parse(login_token_response.body)
|
57
|
-
login_token = login_token_data["login"]["token"]
|
58
|
-
cookie = login_token_response.cookies
|
59
|
-
|
60
|
-
# Second request repeats the first request with the addition of the token (to complete the login).
|
61
|
-
complete_login_response = RestClient.post ENV["API_URL"], {
|
62
|
-
"format" => "json",
|
63
|
-
"action" => "login",
|
64
|
-
"lgname" => username,
|
65
|
-
"lgpassword" => password,
|
66
|
-
"lgtoken" => login_token},
|
67
|
-
{:cookies => cookie}
|
68
|
-
|
69
|
-
complete_login_data = JSON.parse(complete_login_response.body)
|
70
|
-
complete_login_status = complete_login_data["login"]["result"]
|
71
|
-
|
72
|
-
if (complete_login_status != "Success")
|
73
|
-
puts "There was a problem - login was NOT successful."
|
74
|
-
end
|
75
|
-
|
76
|
-
# First request is solely to obtain a valid delete token in the API response.
|
77
|
-
delete_token_response = RestClient.post ENV["API_URL"], {
|
78
|
-
"format" => "json",
|
79
|
-
"action" => "tokens",
|
80
|
-
"type" => "delete",
|
81
|
-
},
|
82
|
-
{:cookies => cookie}
|
83
|
-
|
84
|
-
delete_token_data = JSON.parse(delete_token_response.body)
|
85
|
-
delete_token = delete_token_data["tokens"]["deletetoken"]
|
86
|
-
|
87
|
-
# Second request repeats the first request
|
88
|
-
# with the addition of the token (to complete the article creation).
|
89
|
-
complete_delete_response = RestClient.post ENV["API_URL"], {
|
90
|
-
"format" => "json",
|
91
|
-
"action" => "delete",
|
92
|
-
"title" => title,
|
93
|
-
"token" => delete_token,
|
94
|
-
"reason" => "Deleted by browser tests",
|
95
|
-
},
|
96
|
-
{:cookies => cookie}
|
97
|
-
|
98
|
-
complete_delete_data = JSON.parse(complete_delete_response.body)
|
99
|
-
|
100
|
-
p complete_delete_data if complete_delete_data["error"]
|
101
|
-
end
|
102
|
-
|
103
|
-
def create_user login, password
|
104
|
-
# First request is solely to obtain a valid token in the API response.
|
105
|
-
createaccount_token_response = RestClient.post ENV["API_URL"], {:action => "createaccount", :name => login, :password =>
|
106
|
-
password, :format => "json", :token => ""}
|
107
|
-
|
108
|
-
# Session cookie needs to be maintained for both API requests.
|
109
|
-
createaccount_token_data = JSON.parse(createaccount_token_response.body)
|
110
|
-
cookie = createaccount_token_response.cookies
|
111
|
-
|
112
|
-
createaccount_token = createaccount_token_data["createaccount"]["token"]
|
113
|
-
puts createaccount_token
|
114
|
-
|
115
|
-
# Second request repeats the first request with the addition of the token.
|
116
|
-
complete_createaccount_response = RestClient.post ENV["API_URL"], {:action => "createaccount", :name => login, :password => password, :format => "json", :token => createaccount_token}, {:cookies => cookie}
|
117
|
-
|
118
|
-
complete_createaccount_data = JSON.parse(complete_createaccount_response.body)
|
119
|
-
complete_createaccount_status = complete_createaccount_data["createaccount"]["result"]
|
120
|
-
|
121
|
-
if (complete_createaccount_status != "success")
|
122
|
-
$stderr.puts "There was a problem - new user account creation was NOT successful."
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
1
|
+
require "mediawiki_api/client"
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "faraday-cookie_jar"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module MediawikiApi
|
6
|
+
class LoginError < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
class CreateAccountError < StandardError
|
10
|
+
end
|
11
|
+
|
12
|
+
class TokenError < StandardError
|
13
|
+
end
|
14
|
+
|
15
|
+
class Client
|
16
|
+
attr_accessor :logged_in
|
17
|
+
|
18
|
+
def initialize(url)
|
19
|
+
@conn = Faraday.new(url: url) do |faraday|
|
20
|
+
faraday.request :url_encoded
|
21
|
+
faraday.response :logger
|
22
|
+
faraday.use :cookie_jar
|
23
|
+
faraday.adapter Faraday.default_adapter
|
24
|
+
end
|
25
|
+
@logged_in = false
|
26
|
+
end
|
27
|
+
|
28
|
+
def log_in(username, password, token = nil)
|
29
|
+
params = { action: "login", lgname: username, lgpassword: password, format: "json" }
|
30
|
+
params[:lgtoken] = token unless token.nil?
|
31
|
+
resp = @conn.post "", params
|
32
|
+
|
33
|
+
data = JSON.parse(resp.body)["login"]
|
34
|
+
|
35
|
+
case data["result"]
|
36
|
+
when "Success"
|
37
|
+
@logged_in = true
|
38
|
+
when "NeedToken"
|
39
|
+
log_in username, password, data["token"]
|
40
|
+
else
|
41
|
+
raise LoginError, data["result"]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_account(username, password, token = nil)
|
46
|
+
params = { action: "createaccount", name: username, password: password, format: "json" }
|
47
|
+
params[:token] = token unless token.nil?
|
48
|
+
resp = @conn.post "", params
|
49
|
+
|
50
|
+
data = JSON.parse(resp.body)["createaccount"]
|
51
|
+
|
52
|
+
case data["result"]
|
53
|
+
when "Success"
|
54
|
+
@logged_in = true
|
55
|
+
when "NeedToken"
|
56
|
+
create_account username, password, data["token"]
|
57
|
+
else
|
58
|
+
raise CreateAccountError, data["result"]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_page(title, content)
|
63
|
+
token = get_token "edit"
|
64
|
+
resp = @conn.post "", { action: "edit", title: title, text: content, token: token, format: "json" }
|
65
|
+
end
|
66
|
+
|
67
|
+
def delete_page(title, reason)
|
68
|
+
token = get_token "delete"
|
69
|
+
resp = @conn.post "", { action: "delete", title: title, reason: reason, token: token, format: "json" }
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def get_token(type)
|
75
|
+
resp = @conn.get "", { action: "tokens", type: type, format: "json" }
|
76
|
+
token_data = JSON.parse(resp.body)
|
77
|
+
if token_data.has_key?("warnings")
|
78
|
+
raise TokenError, token_data["warnings"]
|
79
|
+
else
|
80
|
+
token_data["tokens"][type + "token"]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/mediawiki_api.gemspec
CHANGED
@@ -6,15 +6,22 @@ require "mediawiki_api/version"
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "mediawiki_api"
|
8
8
|
spec.version = MediawikiApi::VERSION
|
9
|
-
spec.authors = ["Amir Aharoni", "Chris McMahon", "Jeff Hall", "Zeljko Filipin"]
|
10
|
-
spec.email = ["amir.aharoni@mail.huji.ac.il", "cmcmahon@wikimedia.org", "jhall@wikimedia.org", "zeljko.filipin@gmail.com"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{Uses
|
9
|
+
spec.authors = ["Amir Aharoni", "Chris McMahon", "Jeff Hall", "Juliusz Gonera", "Zeljko Filipin"]
|
10
|
+
spec.email = ["amir.aharoni@mail.huji.ac.il", "cmcmahon@wikimedia.org", "jhall@wikimedia.org", "jgonera@wikimedia.org", "zeljko.filipin@gmail.com"]
|
11
|
+
spec.summary = %q{A library for interacting with MediaWiki API from Ruby.}
|
12
|
+
spec.description = %q{Uses adapter-agnostic Faraday gem to talk to the API.}
|
13
13
|
spec.homepage = "https://github.com/zeljkofilipin/mediawiki_api"
|
14
14
|
spec.license = "GPL-2"
|
15
15
|
|
16
|
-
spec.files = `git ls-files
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
18
|
spec.require_paths = ["lib"]
|
18
19
|
|
19
|
-
spec.
|
20
|
+
spec.add_dependency "faraday", "~> 0.9.0"
|
21
|
+
spec.add_dependency "faraday-cookie_jar", "~> 0.0.6"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
26
|
+
spec.add_development_dependency "webmock", "~> 1.17.2"
|
20
27
|
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe MediawikiApi::Client do
|
4
|
+
subject { MediawikiApi::Client.new(api_url) }
|
5
|
+
body_base = { cookieprefix: "prefix", sessionid: "123" }
|
6
|
+
|
7
|
+
describe "#log_in" do
|
8
|
+
|
9
|
+
it "logs in when API returns Success" do
|
10
|
+
stub_request(:post, api_url).
|
11
|
+
with(body: { format: "json", action: "login", lgname: "Test", lgpassword: "qwe123" }).
|
12
|
+
to_return(body: { login: body_base.merge({ result: "Success" }) }.to_json )
|
13
|
+
|
14
|
+
subject.log_in "Test", "qwe123"
|
15
|
+
subject.logged_in.should be true
|
16
|
+
end
|
17
|
+
|
18
|
+
context "when API returns NeedToken" do
|
19
|
+
before do
|
20
|
+
headers = { "Set-Cookie" => "prefixSession=789; path=/; domain=localhost; HttpOnly" }
|
21
|
+
|
22
|
+
stub_request(:post, api_url).
|
23
|
+
with(body: { format: "json", action: "login", lgname: "Test", lgpassword: "qwe123" }).
|
24
|
+
to_return(
|
25
|
+
body: { login: body_base.merge({ result: "NeedToken", token: "456" }) }.to_json,
|
26
|
+
headers: { "Set-Cookie" => "prefixSession=789; path=/; domain=localhost; HttpOnly" }
|
27
|
+
)
|
28
|
+
|
29
|
+
@success_req = stub_request(:post, api_url).
|
30
|
+
with(body: { format: "json", action: "login", lgname: "Test", lgpassword: "qwe123", lgtoken: "456" }).
|
31
|
+
with(headers: { "Cookie" => "prefixSession=789" }).
|
32
|
+
to_return(body: { login: body_base.merge({ result: "Success" }) }.to_json )
|
33
|
+
end
|
34
|
+
|
35
|
+
it "logs in" do
|
36
|
+
subject.log_in "Test", "qwe123"
|
37
|
+
subject.logged_in.should be true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "sends second request with token and cookies" do
|
41
|
+
subject.log_in "Test", "qwe123"
|
42
|
+
@success_req.should have_been_requested
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when API returns neither Success nor NeedToken" do
|
47
|
+
before do
|
48
|
+
stub_request(:post, api_url).
|
49
|
+
with(body: { format: "json", action: "login", lgname: "Test", lgpassword: "qwe123" }).
|
50
|
+
to_return(body: { login: body_base.merge({ result: "EmptyPass" }) }.to_json )
|
51
|
+
end
|
52
|
+
|
53
|
+
it "does not log in" do
|
54
|
+
expect { subject.log_in "Test", "qwe123" }.to raise_error
|
55
|
+
subject.logged_in.should be false
|
56
|
+
end
|
57
|
+
|
58
|
+
it "raises error with proper message" do
|
59
|
+
expect { subject.log_in "Test", "qwe123" }.to raise_error MediawikiApi::LoginError, "EmptyPass"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#create_page" do
|
65
|
+
before do
|
66
|
+
stub_request(:get, api_url).
|
67
|
+
with(query: { format: "json", action: "tokens", type: "edit" }).
|
68
|
+
to_return(body: { tokens: { edittoken: "t123" } }.to_json )
|
69
|
+
@edit_req = stub_request(:post, api_url).
|
70
|
+
with(body: { format: "json", action: "edit", title: "Test", text: "test123", token: "t123" })
|
71
|
+
end
|
72
|
+
|
73
|
+
it "creates a page using an edit token" do
|
74
|
+
subject.create_page("Test", "test123")
|
75
|
+
@edit_req.should have_been_requested
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when API returns Success" do
|
79
|
+
before do
|
80
|
+
@edit_req.to_return(body: { result: "Success" }.to_json )
|
81
|
+
end
|
82
|
+
|
83
|
+
it "returns a MediawikiApi::Page"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#delete_page" do
|
88
|
+
before do
|
89
|
+
stub_request(:get, api_url).
|
90
|
+
with(query: { format: "json", action: "tokens", type: "delete" }).
|
91
|
+
to_return(body: { tokens: { deletetoken: "t123" } }.to_json )
|
92
|
+
@delete_req = stub_request(:post, api_url).
|
93
|
+
with(body: { format: "json", action: "delete", title: "Test", reason: "deleting", token: "t123" })
|
94
|
+
end
|
95
|
+
|
96
|
+
it "deletes a page using a delete token" do
|
97
|
+
subject.delete_page("Test", "deleting")
|
98
|
+
@delete_req.should have_been_requested
|
99
|
+
end
|
100
|
+
|
101
|
+
# evaluate results
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "#create_account" do
|
105
|
+
it "creates an account when API returns Success" do
|
106
|
+
stub_request(:post, api_url).
|
107
|
+
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123" }).
|
108
|
+
to_return(body: { createaccount: body_base.merge({ result: "Success" }) }.to_json )
|
109
|
+
|
110
|
+
subject.create_account("Test", "qwe123").should be true
|
111
|
+
end
|
112
|
+
|
113
|
+
context "when API returns NeedToken" do
|
114
|
+
before do
|
115
|
+
headers = { "Set-Cookie" => "prefixSession=789; path=/; domain=localhost; HttpOnly" }
|
116
|
+
|
117
|
+
stub_request(:post, api_url).
|
118
|
+
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123" }).
|
119
|
+
to_return(
|
120
|
+
body: { createaccount: body_base.merge({ result: "NeedToken", token: "456" }) }.to_json,
|
121
|
+
headers: { "Set-Cookie" => "prefixSession=789; path=/; domain=localhost; HttpOnly" }
|
122
|
+
)
|
123
|
+
|
124
|
+
@success_req = stub_request(:post, api_url).
|
125
|
+
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123", token: "456" }).
|
126
|
+
with(headers: { "Cookie" => "prefixSession=789" }).
|
127
|
+
to_return(body: { createaccount: body_base.merge({ result: "Success" }) }.to_json )
|
128
|
+
end
|
129
|
+
|
130
|
+
it "creates an account" do
|
131
|
+
subject.create_account("Test", "qwe123").should be true
|
132
|
+
end
|
133
|
+
|
134
|
+
it "sends second request with token and cookies" do
|
135
|
+
subject.create_account "Test", "qwe123"
|
136
|
+
@success_req.should have_been_requested
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# docs don't specify other results, but who knows
|
141
|
+
# http://www.mediawiki.org/wiki/API:Account_creation
|
142
|
+
context "when API returns neither Success nor NeedToken" do
|
143
|
+
before do
|
144
|
+
stub_request(:post, api_url).
|
145
|
+
with(body: { format: "json", action: "createaccount", name: "Test", password: "qwe123" }).
|
146
|
+
to_return(body: { createaccount: body_base.merge({ result: "WhoKnows" }) }.to_json )
|
147
|
+
end
|
148
|
+
|
149
|
+
it "raises error with proper message" do
|
150
|
+
expect { subject.create_account "Test", "qwe123" }.to raise_error MediawikiApi::CreateAccountError, "WhoKnows"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,56 +1,127 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mediawiki_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amir Aharoni
|
8
8
|
- Chris McMahon
|
9
9
|
- Jeff Hall
|
10
|
+
- Juliusz Gonera
|
10
11
|
- Zeljko Filipin
|
11
12
|
autorequire:
|
12
13
|
bindir: bin
|
13
14
|
cert_chain: []
|
14
|
-
date: 2014-
|
15
|
+
date: 2014-03-13 00:00:00.000000000 Z
|
15
16
|
dependencies:
|
16
17
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
18
|
+
name: faraday
|
18
19
|
requirement: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
20
21
|
- - "~>"
|
21
22
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
23
|
-
|
23
|
+
version: 0.9.0
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
24
29
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
30
|
+
version: 0.9.0
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: faraday-cookie_jar
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - "~>"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.0.6
|
26
38
|
type: :runtime
|
27
39
|
prerelease: false
|
28
40
|
version_requirements: !ruby/object:Gem::Requirement
|
29
41
|
requirements:
|
30
42
|
- - "~>"
|
31
43
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
44
|
+
version: 0.0.6
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: bundler
|
47
|
+
requirement: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - "~>"
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '1.3'
|
52
|
+
type: :development
|
53
|
+
prerelease: false
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - "~>"
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '1.3'
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rake
|
61
|
+
requirement: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
33
63
|
- - ">="
|
34
64
|
- !ruby/object:Gem::Version
|
35
|
-
version:
|
36
|
-
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: rspec
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.14.1
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - "~>"
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 2.14.1
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: webmock
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - "~>"
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.17.2
|
94
|
+
type: :development
|
95
|
+
prerelease: false
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - "~>"
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 1.17.2
|
101
|
+
description: Uses adapter-agnostic Faraday gem to talk to the API.
|
37
102
|
email:
|
38
103
|
- amir.aharoni@mail.huji.ac.il
|
39
104
|
- cmcmahon@wikimedia.org
|
40
105
|
- jhall@wikimedia.org
|
106
|
+
- jgonera@wikimedia.org
|
41
107
|
- zeljko.filipin@gmail.com
|
42
108
|
executables: []
|
43
109
|
extensions: []
|
44
110
|
extra_rdoc_files: []
|
45
111
|
files:
|
46
112
|
- ".gitreview"
|
113
|
+
- ".rspec"
|
47
114
|
- CREDITS
|
48
115
|
- Gemfile
|
116
|
+
- Gemfile.lock
|
49
117
|
- LICENSE.txt
|
50
118
|
- README.md
|
51
119
|
- lib/mediawiki_api.rb
|
120
|
+
- lib/mediawiki_api/client.rb
|
52
121
|
- lib/mediawiki_api/version.rb
|
53
122
|
- mediawiki_api.gemspec
|
123
|
+
- spec/client_spec.rb
|
124
|
+
- spec/spec_helper.rb
|
54
125
|
homepage: https://github.com/zeljkofilipin/mediawiki_api
|
55
126
|
licenses:
|
56
127
|
- GPL-2
|
@@ -74,5 +145,7 @@ rubyforge_project:
|
|
74
145
|
rubygems_version: 2.2.2
|
75
146
|
signing_key:
|
76
147
|
specification_version: 4
|
77
|
-
summary:
|
78
|
-
test_files:
|
148
|
+
summary: A library for interacting with MediaWiki API from Ruby.
|
149
|
+
test_files:
|
150
|
+
- spec/client_spec.rb
|
151
|
+
- spec/spec_helper.rb
|