Sha256: fd9434aac736c66f0c75ed072dc6dc9f32b9f454bf153fb3cc50b6c9ac488cb8
Contents?: true
Size: 1.66 KB
Versions: 3
Compression:
Stored size: 1.66 KB
Contents
require "se/realtime/version" require "se/realtime/websocket" module SE module Realtime class << self def on_post(&handler) ws do |e| data = JSON.parse e['data'] handler.call(data) end end def json(site: nil, &handler) ws do |e| data = clean_keys(JSON.parse(e['data'])) handler.call(data) if data[:site] == site || site.nil? end end def batch(size, **opts, &handler) posts = [] json(**opts) do |e| posts << e if posts.length >= size handler.call(posts) posts = [] end end end def ws(&block) WSClient.new("https://qa.sockets.stackexchange.com", cookies, &block) end private def clean_keys(json) { 'apiSiteParameter' => :site, 'titleEncodedFancy' => :title, 'bodySummary' => :body, 'lastActivityDate' => :last_active, 'siteBaseHostAddress' => :site_url }.each do |old_key, new_key| json[new_key] = json.delete(old_key) if json.key?(old_key) end json.map do |k,v| if k.is_a? String [k.gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase.to_sym,v] else [k.to_sym,v] end end.to_h end def cookies agent = Mechanize.new agent.get("https://stackexchange.com/questions?realtime") cookie_array = agent.cookies.map do |cookie| "#{cookie.name}=#{cookie.value}" if cookie.domain.end_with? "stackexchange.com" end (cookie_array - [nil]).join("; ") end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
se-realtime-0.0.1 | lib/se/realtime.rb |
se-realtime-0.0.0.beta2 | lib/se/realtime.rb |
se-realtime-0.0.0.beta1 | lib/se/realtime.rb |