mediawiki-keiki 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b786adda5c9f95e248abe9435bc3dbcd8a354460
4
+ data.tar.gz: 306adbb72d9dded52f103fbe802bb82d2d7fbd39
5
+ SHA512:
6
+ metadata.gz: 8a42136d3437afe3b46814552ad7e0a1437d918bc8f4c2b8761c9aaa1daf50698347ce08cdc4ebcfe80a2faebdaa7bf19e4ce276d240c340b4debc5cbb401997
7
+ data.tar.gz: 3c1ea82e75025f37d47c2ec17664aca48dc0892d291b27298e429bae0072fe8eca07c2825685ebe5540a81e79c41865dc66ad5ac7a9e052b69633bd6700fa976
data/Gemfile CHANGED
@@ -1,16 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
4
-
5
- # gem 'httparty'
6
- # gem 'wikicloth'
7
- # gem 'nokogiri'
8
-
9
- # group :test do
10
- # gem 'webmock'
11
- # gem 'vcr'
12
- # gem 'turn'
13
- # gem 'rake'
14
- # gem 'pry'
15
- # gem 'minitest-reporters'
16
- # end
3
+ gemspec
data/README.md CHANGED
@@ -4,11 +4,53 @@ A Ruby API client for the [MediaWiki API](http://www.mediawiki.org/wiki/API:Main
4
4
 
5
5
  ## Features
6
6
 
7
+ Query the WikiMedia API, returning JSON documents
8
+ - Multiple pages per query supported
7
9
 
8
- ## Links
10
+ Retrieve data from WikiMedia pages, including
11
+ - Title
12
+ - Full text
13
+ - Full text as HTML
14
+ - Short text summary
9
15
 
10
16
 
11
17
  ## Installation
12
18
 
19
+ Install as a ruby gem
13
20
 
14
- ## Usage
21
+ ```shell
22
+ $ gem install mediawiki-keiki
23
+ ```
24
+
25
+ Or add it to your application's `Gemfile`:
26
+
27
+ ```ruby
28
+ gem 'mediawiki-gateway'
29
+ ```
30
+
31
+
32
+ ## Usage
33
+
34
+ Create a simple query with a single page
35
+
36
+ ```ruby
37
+ query = WikiMedia::Query.new('foo')
38
+ ```
39
+
40
+ Create a query with multiple pages
41
+
42
+ ```ruby
43
+ query = WikiMedia::Query.new('foo|bar')
44
+ ```
45
+
46
+ Retrieve a page from a query
47
+
48
+ ```ruby
49
+ foo_page = query.pages['foo']
50
+ ```
51
+
52
+ Get the summary of a page
53
+
54
+ ```ruby
55
+ puts foo_page.summary
56
+ ```
@@ -31,22 +31,109 @@ module MediaWiki
31
31
  # Returns a hash filled with Pages
32
32
  def pages
33
33
 
34
- # Captures the original query and sorts it, for using as keys with the hash
35
- key_array = @query.split('|').sort
36
- i = 0
34
+ result_map = map_query_to_results
37
35
 
38
- # Creates a hash, using the original query as the keys and new Site objects as the values
39
36
  query_result["query"]["pages"].each do |key, value|
40
- @page_hash[key_array[i]] = MediaWiki::Page.new(value)
41
- i += 1
37
+
38
+ page_title = value["title"]
39
+ original_query = find_result_map_match_for_title(result_map, page_title)
40
+ @page_hash[original_query] = MediaWiki::Page.new(value)
41
+
42
42
  end
43
43
 
44
44
  @page_hash
45
45
  end
46
46
 
47
+ def find_result_map_match_for_title(result_map, page_title)
48
+ result_map.each do |h|
49
+ final_title = get_final_title(h)
50
+ if page_title == final_title
51
+ return h[:search_term]
52
+ end
53
+ end
54
+ nil
55
+ end
56
+
57
+ def get_final_title(result_map_hash)
58
+ if result_map_hash[:redirected]
59
+ final_title = result_map_hash[:redirected]
60
+ elsif result_map_hash[:normalized]
61
+ final_title = result_map_hash[:normalized]
62
+ else
63
+ final_title = result_map_hash[:search_term]
64
+ end
65
+ final_title
66
+ end
67
+
68
+
69
+ # Maps original query to results
70
+ def map_query_to_results
71
+
72
+ #Initalize map
73
+ result_map = initialize_map
74
+
75
+ # Apply the normalization to the result map
76
+ normalized = get_query_map("normalized")
77
+ if normalized
78
+ result_map = get_normalizations_for(result_map, normalized)
79
+ end
80
+
81
+ # Apply the redirects to the result map
82
+ redirects = get_query_map("redirects")
83
+ if redirects
84
+ result_map = get_redirects_for(result_map, redirects)
85
+ end
86
+
87
+ result_map
88
+
89
+ end
90
+
91
+ # Initially create an array of hashses with the original :search_terms
92
+ def initialize_map
93
+ result_map = []
94
+ original_terms = @query.split('|')
95
+
96
+ original_terms.each { |k| result_map << {:search_term => k } }
97
+ result_map
98
+ end
99
+
100
+ # Add :normalized key to the result map if the original search term was normalized
101
+ def get_normalizations_for(result_map, normalized)
102
+ result_map.each do |rm|
103
+ normalized.each do |n|
104
+ rm[:normalized] = n["to"] if n["from"] == rm[:search_term]
105
+ end
106
+ end
107
+ result_map
108
+ end
109
+
110
+ # Add :redirected key to the result map if the either the original search term or the normalized term were redirected
111
+ def get_redirects_for(result_map, redirects)
112
+ result_map.each do |rm|
113
+ redirects.each do |r|
114
+ rm[:redirected] = r["to"] if r["from"] == rm[:search_term] || rm[:normalized]
115
+ end
116
+ end
117
+ result_map
118
+ end
119
+
120
+ # Get maps (normalization and redirect) from the wiki query result
121
+ def get_query_map(map_type)
122
+ query_result["query"][map_type] # if query_result["query"][map_type]
123
+ end
124
+
125
+ # # Use the query maps to update the values of the result map
126
+ # def map_from_to(result_map, query_map)
127
+ # result_map.each do |key, value|
128
+ # query_map.each { |hash| value = hash["to"] if hash["from"] == value }
129
+ # end
130
+ # result_map
131
+ # end
132
+
133
+
47
134
  private
48
135
 
49
- # Private method that gets called if the query_result has not been retrieved yet
136
+ # Private method that gets called if the query_result has not been retrieved yet; gets query and parses as a hash.
50
137
  def get_query_result
51
138
  self.class.get URI.encode("/w/api.php?continue=&format=json&action=query&titles=#{@query}&prop=revisions&rvprop=content&redirects")
52
139
  end
@@ -63,19 +63,91 @@ describe MediaWiki::Query do
63
63
  describe "dynamic attributes" do
64
64
 
65
65
  before do
66
- wiki_query.query_result
66
+ # wiki_query.query_result
67
+ @initialized_result_map = [{:search_term => "foo"}]
68
+ @normalized_hash_array = [{"from"=>"foo", "to" =>"Foo"}]
69
+ @redirected_hash_array = [{"from" => "Foo", "to" => "Foobar"}]
70
+ @normalized_map = [{:search_term => "foo", :normalized => "Foo"}]
71
+ @redirected_map = [{:search_term => "foo", :normalized => "Foo", :redirected => "Foobar"}]
72
+ end
73
+
74
+ it "must initalize a result map with the search terms in the keys and values" do
75
+ wiki_query.initialize_map.must_equal @initialized_result_map
76
+ end
77
+
78
+ it "must get an array of a hash with normalizations" do
79
+ wiki_query.get_query_map("normalized").must_equal @normalized_hash_array
80
+ end
81
+
82
+ it "must get an array of a hash with redirects" do
83
+ wiki_query.get_query_map("redirects").must_equal @redirected_hash_array
84
+ end
85
+
86
+ it "must get normalizations for the original search terms" do
87
+ wiki_query.get_normalizations_for(@initialized_result_map, @normalized_hash_array).must_equal @normalized_map
88
+ end
89
+
90
+ it "must get redirects for the original or normalized search terms" do
91
+ wiki_query.get_redirects_for(@normalized_map, @redirected_hash_array).must_equal @redirected_map
92
+ end
93
+
94
+ # it "must apply the noramalization map to the results map" do
95
+ # wiki_query.map_from_to({"foo"=>"foo"}, @normalized_hash_array).must_equal @normalized_map
96
+ # end
97
+
98
+ # it "must apply the redirects map to the normalized result map" do
99
+ # wiki_query.map_from_to(@normalized_map,@redirected_hash_array).must_equal @redirected_map
100
+ # end
101
+
102
+ it "must map the original query to the normalized, redirected title" do
103
+ wiki_query.map_query_to_results.must_equal @redirected_map
104
+ end
105
+
106
+ describe "find result map match for title" do
107
+
108
+ before do
109
+ @result_map = wiki_query.map_query_to_results
110
+ @page_title = "Foobar"
111
+ end
112
+
113
+ it "must return the final title" do
114
+ wiki_query.get_final_title(@result_map[0]).must_equal "Foobar"
115
+ end
116
+
117
+ it "must return the original query that matches the page title" do
118
+ wiki_query.find_result_map_match_for_title(@result_map, @page_title).must_equal 'foo'
119
+ end
120
+
67
121
  end
68
122
 
69
123
  it "must return the pages as a hash" do
70
124
  wiki_query.pages.must_be_instance_of Hash
71
125
  end
72
126
 
73
- it "must use keys in pages that match the original search terms" do
74
- wiki_query.pages.keys[0].must_equal 'foo'
127
+ it "must store the pages as Page classes" do
128
+ wiki_query.pages['foo'].must_be_instance_of MediaWiki::Page
75
129
  end
76
130
 
77
- it "must store the pages as Site classes" do
78
- wiki_query.pages['foo'].must_be_instance_of MediaWiki::Page
131
+ end
132
+
133
+ describe "a page with no normalization or redirects" do
134
+
135
+ let(:wiki_query) { MediaWiki::Query.new('Main Page')}
136
+
137
+ before do
138
+ @final_result_map = [{:search_term => "Main Page"}]
139
+ end
140
+
141
+ it "should return nil for the normalization map" do
142
+ wiki_query.get_query_map("normalized").must_equal nil
143
+ end
144
+
145
+ it "should return nil for the redirects map" do
146
+ wiki_query.get_query_map("redirects").must_equal nil
147
+ end
148
+
149
+ it "should return a result map pointing to the original search term" do
150
+ wiki_query.map_query_to_results.must_equal @final_result_map
79
151
  end
80
152
 
81
153
  end
@@ -98,18 +170,28 @@ describe MediaWiki::Query do
98
170
 
99
171
  end
100
172
 
101
- describe "multiple sites" do
173
+ describe "multiple pages" do
174
+
175
+ let(:wiki_query) { MediaWiki::Query.new('Partners In Health|ThoughtWorks|Accion International') }
102
176
 
103
- let(:wiki_query) { MediaWiki::Query.new('foo|bar|camp') }
177
+ it "must get a result map for each page" do
178
+ wiki_query.map_query_to_results.length.must_equal 3
179
+ end
104
180
 
105
181
  it "must return all of the sites" do
106
182
  wiki_query.pages.length.must_equal 3
107
183
  end
108
184
 
109
- it "must tag the sites with the right query terms" do
110
- wiki_query.pages['foo'].title.must_equal 'Foobar'
111
- wiki_query.pages['bar'].title.must_equal 'Bar'
112
- wiki_query.pages['camp'].title.must_equal 'Camp'
185
+ it "must tag the first page with the right query term" do
186
+ wiki_query.pages['Partners In Health'].title.must_equal 'Partners In Health'
187
+ end
188
+
189
+ it "must tag the second page with the right query term" do
190
+ wiki_query.pages['ThoughtWorks'].title.must_equal 'ThoughtWorks'
191
+ end
192
+
193
+ it "must tag the last page with the right query term" do
194
+ wiki_query.pages['Accion International'].title.must_equal 'ACCION International'
113
195
  end
114
196
 
115
197
  end
metadata CHANGED
@@ -1,158 +1,193 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mediawiki-keiki
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.0.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Evan Waters
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-12-11 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: httparty
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ - - ">="
20
21
  - !ruby/object:Gem::Version
21
22
  version: 0.13.3
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.13'
30
+ - - ">="
28
31
  - !ruby/object:Gem::Version
29
32
  version: 0.13.3
30
33
  - !ruby/object:Gem::Dependency
31
34
  name: wikicloth
32
35
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
36
  requirements:
35
- - - ~>
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ - - ">="
36
41
  - !ruby/object:Gem::Version
37
42
  version: 0.8.1
38
43
  type: :runtime
39
44
  prerelease: false
40
45
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
46
  requirements:
43
- - - ~>
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.8'
50
+ - - ">="
44
51
  - !ruby/object:Gem::Version
45
52
  version: 0.8.1
46
53
  - !ruby/object:Gem::Dependency
47
54
  name: nokogiri
48
55
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
56
  requirements:
51
- - - ~>
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.6'
60
+ - - ">="
52
61
  - !ruby/object:Gem::Version
53
62
  version: 1.6.5
54
63
  type: :runtime
55
64
  prerelease: false
56
65
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
66
  requirements:
59
- - - ~>
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.6'
70
+ - - ">="
60
71
  - !ruby/object:Gem::Version
61
72
  version: 1.6.5
62
73
  - !ruby/object:Gem::Dependency
63
74
  name: webmock
64
75
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
76
  requirements:
67
- - - ~>
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.20'
80
+ - - ">="
68
81
  - !ruby/object:Gem::Version
69
82
  version: 1.20.4
70
83
  type: :runtime
71
84
  prerelease: false
72
85
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
86
  requirements:
75
- - - ~>
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.20'
90
+ - - ">="
76
91
  - !ruby/object:Gem::Version
77
92
  version: 1.20.4
78
93
  - !ruby/object:Gem::Dependency
79
94
  name: vcr
80
95
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
96
  requirements:
83
- - - ~>
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '2.9'
100
+ - - ">="
84
101
  - !ruby/object:Gem::Version
85
102
  version: 2.9.3
86
103
  type: :runtime
87
104
  prerelease: false
88
105
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
106
  requirements:
91
- - - ~>
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.9'
110
+ - - ">="
92
111
  - !ruby/object:Gem::Version
93
112
  version: 2.9.3
94
113
  - !ruby/object:Gem::Dependency
95
114
  name: turn
96
115
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
116
  requirements:
99
- - - ~>
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '0.9'
120
+ - - ">="
100
121
  - !ruby/object:Gem::Version
101
122
  version: 0.9.7
102
123
  type: :runtime
103
124
  prerelease: false
104
125
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
126
  requirements:
107
- - - ~>
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.9'
130
+ - - ">="
108
131
  - !ruby/object:Gem::Version
109
132
  version: 0.9.7
110
133
  - !ruby/object:Gem::Dependency
111
134
  name: rake
112
135
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
136
  requirements:
115
- - - ~>
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '10.4'
140
+ - - ">="
116
141
  - !ruby/object:Gem::Version
117
142
  version: 10.4.2
118
143
  type: :runtime
119
144
  prerelease: false
120
145
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
146
  requirements:
123
- - - ~>
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '10.4'
150
+ - - ">="
124
151
  - !ruby/object:Gem::Version
125
152
  version: 10.4.2
126
153
  - !ruby/object:Gem::Dependency
127
154
  name: pry
128
155
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
156
  requirements:
131
- - - ~>
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.10'
160
+ - - ">="
132
161
  - !ruby/object:Gem::Version
133
162
  version: 0.10.1
134
163
  type: :runtime
135
164
  prerelease: false
136
165
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
166
  requirements:
139
- - - ~>
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '0.10'
170
+ - - ">="
140
171
  - !ruby/object:Gem::Version
141
172
  version: 0.10.1
142
173
  - !ruby/object:Gem::Dependency
143
174
  name: minitest-reporters
144
175
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
176
  requirements:
147
- - - ~>
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: '0.14'
180
+ - - ">="
148
181
  - !ruby/object:Gem::Version
149
182
  version: 0.14.24
150
183
  type: :runtime
151
184
  prerelease: false
152
185
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
186
  requirements:
155
- - - ~>
187
+ - - "~>"
188
+ - !ruby/object:Gem::Version
189
+ version: '0.14'
190
+ - - ">="
156
191
  - !ruby/object:Gem::Version
157
192
  version: 0.14.24
158
193
  description: A client for the MediaWiki API
@@ -163,39 +198,38 @@ extensions: []
163
198
  extra_rdoc_files:
164
199
  - README.md
165
200
  files:
166
- - Rakefile
201
+ - Gemfile
167
202
  - LICENSE
168
203
  - README.md
169
- - Gemfile
204
+ - Rakefile
170
205
  - lib/mediawiki-keiki.rb
171
206
  - lib/mediawiki-keiki/page.rb
172
207
  - lib/mediawiki-keiki/query.rb
173
- - spec/spec_helper.rb
174
208
  - spec/lib/mediawiki-keiki/page_spec.rb
175
209
  - spec/lib/mediawiki-keiki/query_spec.rb
210
+ - spec/spec_helper.rb
176
211
  homepage: https://github.com/mkulumadzi/mediawiki-keiki
177
212
  licenses:
178
213
  - MIT
214
+ metadata: {}
179
215
  post_install_message:
180
216
  rdoc_options: []
181
217
  require_paths:
182
218
  - lib
183
219
  required_ruby_version: !ruby/object:Gem::Requirement
184
- none: false
185
220
  requirements:
186
- - - ! '>='
221
+ - - "~>"
187
222
  - !ruby/object:Gem::Version
188
- version: '0'
223
+ version: '2.0'
189
224
  required_rubygems_version: !ruby/object:Gem::Requirement
190
- none: false
191
225
  requirements:
192
- - - ! '>='
226
+ - - ">="
193
227
  - !ruby/object:Gem::Version
194
228
  version: '0'
195
229
  requirements: []
196
230
  rubyforge_project:
197
- rubygems_version: 1.8.23
231
+ rubygems_version: 2.4.5
198
232
  signing_key:
199
- specification_version: 3
233
+ specification_version: 4
200
234
  summary: MediaWiki Keiki
201
235
  test_files: []