se-realtime 0.0.0.beta0 → 0.0.0.beta1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 712132141551c830aa22ec30a8c73bd689d860e2
4
- data.tar.gz: b4ccff6e2106244445eb633177870e8802d8b938
3
+ metadata.gz: 99186f6f66fdcc6578e58711019984ec0fd9911e
4
+ data.tar.gz: 5f92c4c7cbcde8e943b79165eeec8722ad4eb415
5
5
  SHA512:
6
- metadata.gz: 4e591185c80ea2836d5b0a7c371c44883a81ca6328f364b1888c720b492f333d920d9b3b06e6c72dcceb3f67eaa498d5528fb06f0e3586ebe0b7629f52e6c061
7
- data.tar.gz: 86ffbf8259c9d7ffcc4e2caea6c107f734d4da0146c3cb7d16ab7af287e9a6c55cefeef3813ad557fa9af20c49cf4f538249837424d1030a45b0a3f8c5f14cad
6
+ metadata.gz: 0a7bc67b138ac70d88044fb55e42e37fba688e95de796f39d8ffb0e331540168ddad382bb531ae0a71490f396ef5283bb3a139ef403e5e981778f0dd4df91777
7
+ data.tar.gz: 0610f196988de9099cb0f09849b101d78276e6e9a43d0886ee9ab4cea94617b45be2411a285e677bcc026cdd707cd57e557d4489ad047963e928a0f9a60887d6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- se-realtime (0.0.0.beta0)
4
+ se-realtime (0.0.0.beta1)
5
5
  mechanize (~> 2.7)
6
6
  nokogiri (~> 1.8)
7
7
  permessage_deflate (~> 0.1)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Se::Realtime
1
+ # SE Realtime
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/se/realtime`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ SE Realtime is a ruby API for the [Stack Exchange real time questions feed](https://stackexchange.com/questions?realtime). It allows various methods of retrieving questions from that page.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,66 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ First of all, require it with `require 'se/realtime'`.
24
+
25
+ Then, pick a retrevial method:
26
+
27
+ ### Retrieveal methods
28
+
29
+ #### Raw
30
+
31
+ To simply get exactly what the websocket is spitting out, use the raw retrevial method:
32
+
33
+ ```ruby
34
+
35
+ SE::Realtime.ws do |data|
36
+ puts data
37
+ end
38
+ ```
39
+
40
+ This will print out exactly what comes out of the websocket (`JSON.parse`d).
41
+
42
+ > **Warning:**
43
+ >
44
+ > The only relevant part here is the data field of the json, which has a string value (because reasons), and so you must `JSON.parse(data['data'])` to get the real data.
45
+
46
+ #### JSON
47
+
48
+ To get a neatly formatted version of the post with nicer keys, use this method. The keys are all symbols:
49
+
50
+ - site: The simple name of the site
51
+ - body: A truncated version of the body of the post (with "..." appended)
52
+ - title: The title of the post
53
+ - last_active: The timestamp from SE of the last activity.
54
+ - site_url: The base url of the site that the post came from
55
+ - url: The url of the post
56
+ - owner_url: The url of the profile of the owner
57
+ - owner_display_name: The display name of the owner
58
+ - id: The ID of the post
59
+
60
+ Syntax:
61
+
62
+ ```ruby
63
+ SE::Realtime.json do |data|
64
+ puts data
65
+ end
66
+ ```
67
+
68
+ This also accepts a `site: 'sitename'` parameter to filter by site.
69
+
70
+ #### Batched
71
+
72
+ This is identical to the JSON method, except it takes a batch size and returns an array of Hashes of the size passed. It passes all hash parameters on to json, so you can use filters such as `site: 'sitename'`.
73
+
74
+ Syntax:
75
+
76
+ ```ruby
77
+ SE::Realtime.batch 10, site: 'stackoverflow' do |data|
78
+ puts data
79
+ end
80
+ ```
81
+
82
+ This will return arrays of 10 stackoverflow posts every time 10 have been retrieved.
26
83
 
27
84
  ## Development
28
85
 
@@ -32,7 +89,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
89
 
33
90
  ## Contributing
34
91
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/se-realtime.
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/izwick-schachter/se-realtime.
36
93
 
37
94
  ## License
38
95
 
data/lib/se/realtime.rb CHANGED
@@ -5,14 +5,55 @@ module SE
5
5
  module Realtime
6
6
  class << self
7
7
  def on_post(&handler)
8
- WSClient.new("https://qa.sockets.stackexchange.com", cookies) do |e|
8
+ ws do |e|
9
9
  data = JSON.parse e['data']
10
10
  handler.call(data)
11
11
  end
12
12
  end
13
13
 
14
+ def json(site: nil, &handler)
15
+ ws do |e|
16
+ data = clean_keys(JSON.parse(e['data']))
17
+ handler.call(data) if data[:site] == site || site.nil?
18
+ end
19
+ end
20
+
21
+ def batch(size, **opts, &handler)
22
+ posts = []
23
+ json(**opts) do |e|
24
+ posts << e
25
+ if posts.length >= size
26
+ handler.call(posts)
27
+ posts = []
28
+ end
29
+ end
30
+ end
31
+
32
+ def ws(&block)
33
+ WSClient.new("https://qa.sockets.stackexchange.com", cookies, &block)
34
+ end
35
+
14
36
  private
15
37
 
38
+ def clean_keys(json)
39
+ {
40
+ 'apiSiteParameter' => :site,
41
+ 'titleEncodedFancy' => :title,
42
+ 'bodySummary' => :body,
43
+ 'lastActivityDate' => :last_active,
44
+ 'siteBaseHostAddress' => :site_url
45
+ }.each do |old_key, new_key|
46
+ json[new_key] = json.delete(old_key) if json.key?(old_key)
47
+ end
48
+ json.map do |k,v|
49
+ if k.is_a? String
50
+ [k.gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase.to_sym,v]
51
+ else
52
+ [k.to_sym,v]
53
+ end
54
+ end.to_h
55
+ end
56
+
16
57
  def cookies
17
58
  agent = Mechanize.new
18
59
  agent.get("https://stackexchange.com/questions?realtime")
@@ -1,5 +1,5 @@
1
1
  module SE
2
2
  module Realtime
3
- VERSION = "0.0.0.beta0".freeze
3
+ VERSION = "0.0.0.beta1".freeze
4
4
  end
5
5
  end
@@ -17,6 +17,7 @@ module SE
17
17
  @driver = WebSocket::Driver.client(self)
18
18
  @socket = TCPSocket.new(@uri.host, 80)
19
19
  @handler = handler
20
+ @logger = Logger.new "realtime-#{DateTime.now.strftime('%Q')}.log"
20
21
 
21
22
  @driver.add_extension PermessageDeflate
22
23
  @driver.set_header "Cookies", cookies if cookies
@@ -30,7 +31,13 @@ module SE
30
31
  end
31
32
 
32
33
  @driver.on :message do |e|
33
- @handler.call(JSON.parse(e.data))
34
+ @logger.info("Read: #{e.data}")
35
+ data = JSON.parse(e.data)
36
+ if data["action"] == "hb"
37
+ send "hb"
38
+ else
39
+ @handler.call(data)
40
+ end
34
41
  end
35
42
 
36
43
  @driver.on :close, ->(_e) { puts "WebSocket is closed!"}
@@ -57,6 +64,8 @@ module SE
57
64
  end
58
65
 
59
66
  def send(message)
67
+ puts "Lub dub" if message == "hb"
68
+ @logger.info("Wrote: #{message}")
60
69
  @driver.text(message)
61
70
  end
62
71
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: se-realtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.beta0
4
+ version: 0.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thesecretmaster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-10 00:00:00.000000000 Z
11
+ date: 2017-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler