infoboxer 0.1.2.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -87,7 +87,7 @@ module Infoboxer
87
87
  # @!method fetch
88
88
  # Fetches by name(s) variables for all templates inside.
89
89
  #
90
- # See {Templates::Base#fetch} for explanation.
90
+ # See {Tree::Template#fetch} for explanation.
91
91
 
92
92
  [
93
93
  :prev_siblings, :next_siblings, :siblings,
@@ -101,7 +101,7 @@ module Infoboxer
101
101
  # By list of variable names, fetches hashes of `{name => value}`
102
102
  # from all templates inside.
103
103
  #
104
- # See {Templates::Base#fetch_hash} for explanation.
104
+ # See {Tree::Template#fetch_hash} for explanation.
105
105
  #
106
106
  # @return [Array<Hash>]
107
107
  def fetch_hashes(*args)
@@ -39,6 +39,7 @@ module Infoboxer
39
39
  @closed = true
40
40
  else
41
41
  [splitter, *other.children].each do |c|
42
+ c.parent = self
42
43
  @children << c
43
44
  end
44
45
  @closed = other.closed?
@@ -29,7 +29,7 @@ module Infoboxer
29
29
  rows.first : nil
30
30
  end
31
31
 
32
- # For now, returns all table rows except {heading_row}
32
+ # For now, returns all table rows except {#heading_row}
33
33
  def body_rows
34
34
  rows.first.children.all?(&call(matches?: TableHeading)) ?
35
35
  rows[1..-1] :
@@ -117,6 +117,15 @@ module Infoboxer
117
117
  variables.map{|var| var.to_tree(level+1)}.join
118
118
  end
119
119
 
120
+ # Represents entire template as hash of `String => String`,
121
+ # where keys are variable names and values are text representation
122
+ # of variables contents.
123
+ #
124
+ # @return [Hash{String => String}]
125
+ def to_h
126
+ variables.map{|var| [var.name, var.text]}.to_h
127
+ end
128
+
120
129
  # Returns list of template variables with numeric names (which
121
130
  # are treated as "unnamed" variables by MediaWiki templates, see
122
131
  # {Template class docs} for explanation).
@@ -1,4 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Infoboxer
3
- VERSION = '0.1.2.1'
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ PATCH = 0
6
+ VERSION = [MAJOR, MINOR, PATCH].join('.')
4
7
  end
data/lib/infoboxer.rb CHANGED
@@ -35,6 +35,19 @@ require 'backports/2.1.0/array/to_h'
35
35
  # * {MediaWiki} client class;
36
36
  # * {Parser} -- which, you know, parses.
37
37
  #
38
+ # **NB** `Infoboxer` module can also be included in other classes, like
39
+ # this:
40
+ #
41
+ # ```ruby
42
+ # class MyDataGrabber
43
+ # include Infoboxer
44
+ #
45
+ # def initialize
46
+ # wikipedia.get('Argentina')
47
+ # end
48
+ # end
49
+ # ```
50
+ #
38
51
  module Infoboxer
39
52
  private # hiding constants from YARD
40
53
 
@@ -56,8 +69,15 @@ module Infoboxer
56
69
  species: 'species.wikimedia.org',
57
70
  }
58
71
 
59
- class << self
72
+ public
73
+
74
+ # Includeable version of {Infoboxer.wiki}
75
+ def wiki(api_url, options = {})
76
+ MediaWiki.new(api_url, options || {})
77
+ end
60
78
 
79
+ class << self
80
+ # @!method wiki(api_url, options = {})
61
81
  # Default method for creating MediaWiki API client.
62
82
  #
63
83
  # @param api_url should be URL of api.php for your MediaWiki
@@ -70,10 +90,7 @@ module Infoboxer
70
90
  # ```ruby
71
91
  # Infoboxer.wiki('some_url').get('Some page title')
72
92
  # ```
73
- def wiki(api_url, options = {})
74
- MediaWiki.new(api_url, options || {})
75
- end
76
-
93
+
77
94
  # @!method wikipedia(lang = 'en', options = {})
78
95
  # Shortcut for creating Wikipedia client.
79
96
  #
@@ -128,24 +145,7 @@ module Infoboxer
128
145
  # See {wikipedia} for params explanation.
129
146
  # @return [MediaWiki]
130
147
 
131
- WIKIMEDIA_PROJECTS.each do |name, domain|
132
- define_method name do |lang = 'en', options = {}|
133
- if lang.is_a?(Hash)
134
- lang, options = 'en', lang
135
- end
136
-
137
- wiki("http://#{lang}.#{domain}/w/api.php", options)
138
- end
139
- end
140
-
141
- alias_method :wp, :wikipedia
142
-
143
- WIKIMEDIA_COMMONS.each do |name, domain|
144
- define_method name do |options = {}|
145
- wiki("http://#{domain}/w/api.php", options)
146
- end
147
- end
148
-
148
+ # @!method wikia(*domains)
149
149
  # Performs request to wikia.com wikis.
150
150
  #
151
151
  # @overload wikia(*domains)
@@ -165,22 +165,74 @@ module Infoboxer
165
165
  # (see {wiki} for list of options)
166
166
  #
167
167
  # @return [MediaWiki]
168
- def wikia(*domains)
169
- options = domains.last.is_a?(Hash) ? domains.pop : {}
170
- wiki(WIKIA_API_URL % domains.reverse.join('.'), options)
168
+ end
169
+
170
+ WIKIMEDIA_PROJECTS.each do |name, domain|
171
+ define_method name do |lang = 'en', options = {}|
172
+ if lang.is_a?(Hash)
173
+ lang, options = 'en', lang
174
+ end
175
+
176
+ wiki("https://#{lang}.#{domain}/w/api.php", options)
171
177
  end
178
+ end
172
179
 
173
- # Sets user agent string globally. Default user agent is
174
- # {MediaWiki::UA}.
175
- #
176
- # User agent can also be rewriten as an option to {wiki} method (and
177
- # its shortcuts like {wikipedia}), or by using {MediaWiki#initialize}
178
- # explicitly.
179
- #
180
- def user_agent=(ua)
181
- MediaWiki.user_agent = ua
180
+ alias_method :wp, :wikipedia
181
+
182
+ WIKIMEDIA_COMMONS.each do |name, domain|
183
+ define_method name do |options = {}|
184
+ wiki("https://#{domain}/w/api.php", options)
182
185
  end
183
186
  end
187
+
188
+ # @!method wikipedia(lang = 'en', options = {})
189
+ # Includeable version of {Infoboxer.wikipedia}
190
+
191
+ # @!method commons(options = {})
192
+ # Includeable version of {Infoboxer.commons}
193
+
194
+ # @!method wikibooks(lang = 'en', options = {})
195
+ # Includeable version of {Infoboxer.wikibooks}
196
+
197
+ # @!method wikiquote(lang = 'en', options = {})
198
+ # Includeable version of {Infoboxer.wikiquote}
199
+
200
+ # @!method wikiversity(lang = 'en', options = {})
201
+ # Includeable version of {Infoboxer.wikiversity}
202
+
203
+ # @!method wikisource(lang = 'en', options = {})
204
+ # Includeable version of {Infoboxer.wikisource}
205
+
206
+ # @!method wikivoyage(lang = 'en', options = {})
207
+ # Includeable version of {Infoboxer.wikivoyage}
208
+
209
+ # @!method wikinews(lang = 'en', options = {})
210
+ # Includeable version of {Infoboxer.wikinews}
211
+
212
+ # @!method species(options = {})
213
+ # Includeable version of {Infoboxer.species}
214
+
215
+ # @!method wiktionary(lang = 'en', options = {})
216
+ # Includeable version of {Infoboxer.wiktionary}
217
+
218
+ # Includeable version of {Infoboxer.wikia}
219
+ def wikia(*domains)
220
+ options = domains.last.is_a?(Hash) ? domains.pop : {}
221
+ wiki(WIKIA_API_URL % domains.reverse.join('.'), options)
222
+ end
223
+
224
+ # Sets user agent string globally. Default user agent is
225
+ # {MediaWiki::UA}.
226
+ #
227
+ # User agent can also be rewriten as an option to {wiki} method (and
228
+ # its shortcuts like {wikipedia}), or by using {MediaWiki#initialize}
229
+ # explicitly.
230
+ #
231
+ def Infoboxer.user_agent=(ua)
232
+ MediaWiki.user_agent = ua
233
+ end
234
+
235
+ extend self
184
236
  end
185
237
 
186
238
  require_relative 'infoboxer/version'