class BBS::Wikipedia
Wikipedia’s Action API as a board asks it: a GET through BBS::HTTP that names the board in its User-Agent (Wikipedia may answer 403 without one), paced by a BBS::RateLimit so that all callers together send at most rate requests a second. A request that would wait longer than max_wait seconds for its turn is not sent, so a crowd on the board cannot get its address throttled there.
WIKIPEDIA = BBS::Wikipedia.new(user_agent: 'Alexandria BBS (+https://teletypegames.org)') WIKIPEDIA.get('en', action: 'query', list: 'random', rnnamespace: 0, rnlimit: 1) # => { "batchcomplete" => true, "query" => { "random" => [...] } }
Constants
- MAX_WAIT
-
Seconds a request waits for its turn at most, by default.
- RATE
-
Requests a second, all callers together, by default.
Public Class Methods
# File lib/bbs/wikipedia.rb, line 30 def initialize(user_agent:, rate: RATE, max_wait: MAX_WAIT) @user_agent = user_agent @pace = RateLimit.new(limit: rate, per: 1) @max_wait = max_wait end
Creates a client that sends user_agent, at most rate requests a second, each waiting at most max_wait seconds for its turn.
Public Instance Methods
Source
# File lib/bbs/wikipedia.rb, line 39 def get(lang, **params) raise Error, 'busy: too many Wikipedia requests at once' unless turn? response = HTTP.get(url(lang, params), user_agent: @user_agent, headers: { 'Accept-Encoding' => 'identity' }) raise Error, "Wikipedia answered #{response.status}" unless response.ok? JSON.parse(Text.utf8(response.body)) rescue JSON::ParserError => e raise Error, "Wikipedia answered no JSON: #{e.message[0, 80]}" end