sunflower 0.5.4 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +1 -1
  2. data/lib/sunflower/core.rb +38 -7
  3. metadata +2 -2
data/README CHANGED
@@ -1,4 +1,4 @@
1
- Version: 0.5.4 alpha
1
+ Version: 0.5.5 alpha
2
2
 
3
3
  >>> English:
4
4
 
@@ -3,6 +3,29 @@ require 'rest-client'
3
3
  require 'json'
4
4
  require 'cgi'
5
5
 
6
+ class Hash
7
+ # Recursively, destructively merges two hashes that might contain further hashes and arrays.
8
+ # Hashes are merged using #merge!; arrays are merged using #concat.
9
+ #
10
+ # Named like this to prevent monkey-patching conflicts; is a monkey-patch because it is convenient.
11
+ # Should be considered private to Sunflower and might disappear at any time.
12
+ # Used in Sunflower#API_continued.
13
+ #
14
+ # From http://stackoverflow.com/a/2277713 , slightly modified.
15
+ def sunflower_recursive_merge!(other)
16
+ other.keys.each do |k|
17
+ if self[k].is_a?(Array) && other[k].is_a?(Array)
18
+ self[k].concat(other[k])
19
+ elsif self[k].is_a?(Hash) && other[k].is_a?(Hash)
20
+ self[k].recursive_merge!(other[k])
21
+ else
22
+ self[k] = other[k]
23
+ end
24
+ end
25
+ self
26
+ end
27
+ end
28
+
6
29
  # Main class. To start working, you have to create new Sunflower:
7
30
  # s = Sunflower.new('en.wikipedia.org')
8
31
  # And then log in:
@@ -17,7 +40,7 @@ require 'cgi'
17
40
  #
18
41
  # You can use multiple Sunflowers at once, to work on multiple wikis.
19
42
  class Sunflower
20
- VERSION = '0.5.4'
43
+ VERSION = '0.5.5'
21
44
 
22
45
  INVALID_CHARS = %w(# < > [ ] | { })
23
46
  INVALID_CHARS_REGEX = Regexp.union *INVALID_CHARS
@@ -108,6 +131,9 @@ class Sunflower
108
131
  return type_map[type].sub 'XX', lang
109
132
  end
110
133
 
134
+ # Used by #initialize to cache siteinfo data.
135
+ @@siteinfo = {}
136
+
111
137
  # Initialize a new Sunflower working on a wiki with given URL, for ex. "pl.wikipedia.org".
112
138
  # url can also be a shorthand identifier such as "b:pl" - see Sunflower.resolve_wikimedia_id for details.
113
139
  #
@@ -141,7 +167,8 @@ class Sunflower
141
167
  @cookies = {}
142
168
 
143
169
  siprop = 'general|namespaces|namespacealiases|specialpagealiases|magicwords|interwikimap|dbrepllag|statistics|usergroups|extensions|fileextensions|rightsinfo|languages|skins|extensiontags|functionhooks|showhooks|variables'
144
- @siteinfo = self.API(action: 'query', meta: 'siteinfo', siprop: siprop)['query']
170
+ @@siteinfo[@api_endpoint] ||= self.API(action: 'query', meta: 'siteinfo', siprop: siprop)['query']
171
+ @siteinfo = @@siteinfo[@api_endpoint]
145
172
 
146
173
  _build_ns_map
147
174
  end
@@ -193,8 +220,9 @@ class Sunflower
193
220
  # Assumes action=query.
194
221
  #
195
222
  # By default returns an array of all API responses. Attempts to merge the responses
196
- # into a response that would have been returned if the limit was infinite. merge_on is
197
- # the key of response['query'] to merge consecutive responses on.
223
+ # into a response that would have been returned if the limit was infinite
224
+ # (merges the response hashes recursively using Hash#sunflower_recursive_merge!).
225
+ # merge_on is the key of response["query-continue"] that contains the continuation data.
198
226
  #
199
227
  # If limit given, will perform no more than this many API calls before returning.
200
228
  # If limit is 1, behaves exactly like #API.
@@ -221,15 +249,18 @@ class Sunflower
221
249
 
222
250
  # merge
223
251
  merged = out[0]
224
- meth = (merged['query'][merge_on].is_a?(Hash) ? :merge! : :concat)
225
-
226
252
  out.drop(1).each do |cur|
227
- merged['query'][merge_on].send meth, cur['query'][merge_on]
253
+ merged.recursive_merge! cur
228
254
  end
229
255
 
230
256
  return merged
231
257
  end
232
258
 
259
+ # Returns a Sunflower::Page with the given title belonging to this Sunflower.
260
+ def page title
261
+ Sunflower::Page.new title, self
262
+ end
263
+
233
264
  # Log in using given info.
234
265
  def login user='', password=''
235
266
  if user=='' || password==''
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunflower
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-12 00:00:00.000000000 Z
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json