slanger 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of slanger might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa3af74396ad01a665097cd94f9520c3f9e93e35
4
- data.tar.gz: beac501fa1ec42ba40096fe2854654200c7436ce
3
+ metadata.gz: cd65a213b5685b79b1fd958a1ee446cd9dac9986
4
+ data.tar.gz: 428f76b4006c574491bcb308a2547b2bcb983d7f
5
5
  SHA512:
6
- metadata.gz: dcf153eb18832d068ada988ca15d9c11cd21c06d712c0a38b1cdef1502becc64a19d106f6834819582b82b083e7e872578d6b8bd7290314c4827da17995036f8
7
- data.tar.gz: 9c72e7738e139b245d474d4d007ca1897f9bfa585c642d369274cc6d877b2f74f75190a6cbaadba3ede607c0d1b708a7281f1e2bdf953c4e6d82337616f0ade7
6
+ metadata.gz: b90d2d727f37da70e2e88a05f0cba997c43ddbd4a62472b6aba89d73914e8d0ef5b6aeb22bf12c152329a8e28d4cc15b3438521cde04434a3854a7a71fa78297
7
+ data.tar.gz: dbcd5c1d8070327d26adda0881766151024e0148d4a7f69afb9a16aa27c2a64933bc11dcf3a56101c65e55a43603409226e64866d5441244e79165e0459f9ce7
data/README.md CHANGED
@@ -161,12 +161,14 @@ Of course you could proxy all requests to `ws.example.com` to port 8080 of your
161
161
 
162
162
  # Configuration Options
163
163
 
164
- Slanger supports several configuration options, which can be supplied as command line arguments at invocation.
164
+ Slanger supports several configuration options, which can be supplied as command line arguments at invocation. You can also supply a yaml file containing config options. If you use the config file in combination with other configuration options, the values passed on the command line will win. Allows running multiple instances with only a few differences easy.
165
165
 
166
166
  ```
167
- -k or --app_key This is the Pusher app key you want to use. This is a required argument
167
+ -k or --app_key This is the Pusher app key you want to use. This is a required argument on command line or in optional config file
168
168
 
169
- -s or --secret This is your Pusher secret. This is a required argument
169
+ -s or --secret This is your Pusher secret. This is a required argument on command line or in optional config file
170
+
171
+ -C or --config_file Path to Yaml file that can contain all or some of the configuration options, including required arguments
170
172
 
171
173
  -r or --redis_address An address where there is a Redis server running. This is an optional argument and defaults to redis://127.0.0.1:6379/0
172
174
 
data/bin/slanger CHANGED
@@ -4,11 +4,10 @@
4
4
  require 'optparse'
5
5
  require 'bundler/setup'
6
6
  require 'eventmachine'
7
+ require 'yaml'
8
+ require 'active_support/core_ext/hash'
7
9
 
8
- options = {
9
- api_host: '0.0.0.0', api_port: '4567', websocket_host: '0.0.0.0',
10
- websocket_port: '8080', debug: false, redis_address: 'redis://0.0.0.0:6379/0'
11
- }
10
+ options = {}
12
11
 
13
12
 
14
13
  OptionParser.new do |opts|
@@ -17,14 +16,18 @@ OptionParser.new do |opts|
17
16
  exit
18
17
  end
19
18
 
20
- opts.on '-k', '--app_key APP_KEY', "Pusher application key. This parameter is required." do |k|
19
+ opts.on '-k', '--app_key APP_KEY', "Pusher application key. This parameter is required on command line or in optional config file." do |k|
21
20
  options[:app_key] = k
22
21
  end
23
22
 
24
- opts.on '-s', '--secret SECRET', "Pusher application secret. This parameter is required." do |k|
23
+ opts.on '-s', '--secret SECRET', "Pusher application secret. This parameter is required on command line or in optional config file." do |k|
25
24
  options[:secret] = k
26
25
  end
27
26
 
27
+ opts.on '-C', '--config_file FILE', "Path to Yaml file that can contain all configuration options, including required ones." do |k|
28
+ options[:config_file] = k
29
+ end
30
+
28
31
  opts.on '-r', '--redis_address URL', "Address to bind to (Default: redis://127.0.0.1:6379/0)" do |h|
29
32
  options[:redis_address] = h
30
33
  end
@@ -70,6 +73,11 @@ OptionParser.new do |opts|
70
73
 
71
74
  opts.parse!
72
75
 
76
+ if options[:config_file] and File.exists? options[:config_file]
77
+ config_file_contents = YAML::load(File.open(options[:config_file]))
78
+ options.reverse_merge! config_file_contents.deep_symbolize_keys!
79
+ end
80
+
73
81
  %w<app_key secret>.each do |parameter|
74
82
  unless options[parameter.to_sym]
75
83
  puts "--#{parameter} STRING is a required argument. Use your Pusher #{parameter}.\n"
@@ -123,6 +131,6 @@ EM.run do
123
131
  puts "Running Slanger v.#{Slanger::VERSION}"
124
132
  puts "\n"
125
133
 
126
- puts "Slanger API server listening on port #{options[:api_port]}"
127
- puts "Slanger WebSocket server listening on port #{options[:websocket_port]}"
134
+ puts "Slanger API server listening on port #{Slanger::Config.api_port}"
135
+ puts "Slanger WebSocket server listening on port #{Slanger::Config.websocket_port}"
128
136
  end
@@ -1,13 +1,15 @@
1
+ require 'oj'
2
+
1
3
  module Slanger
2
4
  module Api
3
5
  class Event < Struct.new :name, :data, :socket_id
4
6
  def payload(channel_id)
5
- {
7
+ Oj.dump({
6
8
  event: name,
7
9
  data: data,
8
10
  channel: channel_id,
9
11
  socket_id: socket_id
10
- }.select { |_,v| v }.to_json
12
+ }.select { |_,v| v }, mode: :compat)
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ require 'oj'
2
+
1
3
  module Slanger
2
4
  module Api
3
5
  class RequestValidation < Struct.new :raw_body, :raw_params, :path_info
@@ -10,7 +12,7 @@ module Slanger
10
12
  end
11
13
 
12
14
  def data
13
- @data ||= JSON.parse(body["data"] || params["data"])
15
+ @data ||= Oj.load(body["data"] || params["data"])
14
16
  end
15
17
 
16
18
  def body
@@ -85,8 +87,8 @@ module Slanger
85
87
  end
86
88
 
87
89
  def assert_valid_json!(string)
88
- JSON.parse(string)
89
- rescue JSON::ParserError
90
+ Oj.load(string)
91
+ rescue Oj::ParserError
90
92
  raise Slanger::InvalidRequest.new("Invalid request body: #{raw_body}")
91
93
  end
92
94
 
@@ -8,6 +8,7 @@ require 'em-hiredis'
8
8
  require 'rack'
9
9
  require 'fiber'
10
10
  require 'rack/fiber_pool'
11
+ require 'oj'
11
12
 
12
13
  module Slanger
13
14
  module Api
@@ -31,7 +32,7 @@ module Slanger
31
32
  EventPublisher.publish(valid_request.channels, event)
32
33
 
33
34
  status 202
34
- return {}.to_json
35
+ return Oj.dump({}, mode: :compat)
35
36
  end
36
37
 
37
38
  post '/apps/:app_id/channels/:channel_id/events' do
@@ -41,7 +42,7 @@ module Slanger
41
42
  EventPublisher.publish(valid_request.channels, event)
42
43
 
43
44
  status 202
44
- return {}.to_json
45
+ return Oj.dump({}, mode: :compat)
45
46
  end
46
47
 
47
48
  def valid_request
@@ -7,6 +7,7 @@
7
7
 
8
8
  require 'eventmachine'
9
9
  require 'forwardable'
10
+ require 'oj'
10
11
 
11
12
  module Slanger
12
13
  class Channel
@@ -75,13 +76,13 @@ module Slanger
75
76
  # Only events to channels requiring authentication (private or presence)
76
77
  # are accepted. Public channels only get events from the API.
77
78
  def send_client_message(message)
78
- Slanger::Redis.publish(message['channel'], message.to_json) if authenticated?
79
+ Slanger::Redis.publish(message['channel'], Oj.dump(message, mode: :compat)) if authenticated?
79
80
  end
80
81
 
81
82
  # Send an event received from Redis to the EventMachine channel
82
83
  # which will send it to subscribed clients.
83
84
  def dispatch(message, channel)
84
- push(message.to_json) unless channel =~ /\Aslanger:/
85
+ push(Oj.dump(message, mode: :compat)) unless channel =~ /\Aslanger:/
85
86
  end
86
87
 
87
88
  def authenticated?
@@ -1,3 +1,5 @@
1
+ require 'oj'
2
+
1
3
  module Slanger
2
4
  class Connection
3
5
  attr_accessor :socket, :socket_id
@@ -7,9 +9,9 @@ module Slanger
7
9
  end
8
10
 
9
11
  def send_message m
10
- msg = JSON.parse m
12
+ msg = Oj.load m
11
13
  s = msg.delete 'socket_id'
12
- socket.send msg.to_json unless s == socket_id
14
+ socket.send Oj.dump(msg, mode: :compat) unless s == socket_id
13
15
  end
14
16
 
15
17
  def send_payload *args
@@ -36,9 +38,9 @@ module Slanger
36
38
  private
37
39
 
38
40
  def format(channel_id, event_name, payload = {})
39
- body = { event: event_name, data: payload.to_json }
41
+ body = { event: event_name, data: Oj.dump(payload, mode: :compat) }
40
42
  body[:channel] = channel_id if channel_id
41
- body.to_json
43
+ Oj.dump(body, mode: :compat)
42
44
  end
43
45
  end
44
46
  end
@@ -6,6 +6,7 @@ require 'securerandom'
6
6
  require 'signature'
7
7
  require 'fiber'
8
8
  require 'rack'
9
+ require 'oj'
9
10
 
10
11
  module Slanger
11
12
  class Handler
@@ -24,9 +25,9 @@ module Slanger
24
25
  # Dispatches message handling to method with same name as
25
26
  # the event name
26
27
  def onmessage(msg)
27
- msg = JSON.parse(msg)
28
+ msg = Oj.load(msg)
28
29
 
29
- msg['data'] = JSON.parse(msg['data']) if msg['data'].is_a? String
30
+ msg['data'] = Oj.load(msg['data']) if msg['data'].is_a? String
30
31
 
31
32
  event = msg['event'].gsub(/\Apusher:/, 'pusher_')
32
33
 
@@ -44,10 +45,14 @@ module Slanger
44
45
  end
45
46
 
46
47
  def onclose
47
- @subscriptions.select { |k,v| k && v }.
48
- each do |channel_id, subscription_id|
49
- Channel.unsubscribe channel_id, subscription_id
50
- end
48
+
49
+ subscriptions = @subscriptions.select { |k,v| k && v }
50
+
51
+ subscriptions.each_key do |channel_id|
52
+ subscription_id = subscriptions[channel_id]
53
+ Channel.unsubscribe channel_id, subscription_id
54
+ end
55
+
51
56
  end
52
57
 
53
58
  def authenticate
@@ -8,6 +8,7 @@
8
8
  require 'eventmachine'
9
9
  require 'forwardable'
10
10
  require 'fiber'
11
+ require 'oj'
11
12
 
12
13
  module Slanger
13
14
  class PresenceChannel < Channel
@@ -20,7 +21,7 @@ module Slanger
20
21
  # subscriptions. Update our subscribers accordingly.
21
22
  update_subscribers message
22
23
  else
23
- push message.to_json
24
+ push Oj.dump(message, mode: :compat)
24
25
  end
25
26
  end
26
27
 
@@ -31,7 +32,7 @@ module Slanger
31
32
  end
32
33
 
33
34
  def subscribe(msg, callback, &blk)
34
- channel_data = JSON.parse msg['data']['channel_data']
35
+ channel_data = Oj.load msg['data']['channel_data']
35
36
  public_subscription_id = SecureRandom.uuid
36
37
 
37
38
  # Send event about the new subscription to the Redis slanger:connection_notification Channel.
@@ -97,7 +98,7 @@ module Slanger
97
98
  def publish_connection_notification(payload, retry_count=0)
98
99
  # Send a subscription notification to the global slanger:connection_notification
99
100
  # channel.
100
- Slanger::Redis.publish('slanger:connection_notification', payload.to_json).
101
+ Slanger::Redis.publish('slanger:connection_notification', Oj.dump(payload, mode: :compat)).
101
102
  tap { |r| r.errback { publish_connection_notification payload, retry_count.succ unless retry_count == 5 } }
102
103
  end
103
104
 
@@ -133,7 +134,7 @@ module Slanger
133
134
  end
134
135
 
135
136
  def payload(event_name, payload = {})
136
- { channel: channel_id, event: event_name, data: payload }.to_json
137
+ Oj.dump({ channel: channel_id, event: event_name, data: payload }, mode: :compat)
137
138
  end
138
139
  end
139
140
  end
data/lib/slanger/redis.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  # Interface with Redis.
3
3
 
4
4
  require 'forwardable'
5
+ require 'oj'
5
6
 
6
7
  module Slanger
7
8
  module Redis
@@ -24,7 +25,7 @@ module Slanger
24
25
  def subscriber
25
26
  @subscriber ||= new_connection.pubsub.tap do |c|
26
27
  c.on(:message) do |channel, message|
27
- message = JSON.parse message
28
+ message = Oj.load(message)
28
29
  c = Channel.from message['channel']
29
30
  c.dispatch message, channel
30
31
  end
@@ -1,3 +1,3 @@
1
1
  module Slanger
2
- VERSION = '0.4.3'
2
+ VERSION = '0.4.4'
3
3
  end
@@ -1,14 +1,17 @@
1
1
  require 'fiber'
2
2
  require 'em-http-request'
3
+ require 'oj'
3
4
 
4
5
  module Slanger
5
6
  module Webhook
6
7
  def post payload
7
8
  return unless Slanger::Config.webhook_url
8
9
 
9
- payload = {
10
+ payload ={
10
11
  time_ms: Time.now.strftime('%s%L'), events: [payload]
11
- }.to_json
12
+ }
13
+
14
+ payload = Oj.dump(payload, mode: :compat)
12
15
 
13
16
  digest = OpenSSL::Digest::SHA256.new
14
17
  hmac = OpenSSL::HMAC.hexdigest(digest, Slanger::Config.secret, payload)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slanger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stevie Graham
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-30 00:00:00.000000000 Z
12
+ date: 2015-07-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -151,6 +151,20 @@ dependencies:
151
151
  - - "~>"
152
152
  - !ruby/object:Gem::Version
153
153
  version: 0.3.0
154
+ - !ruby/object:Gem::Dependency
155
+ name: oj
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 2.12.9
161
+ type: :runtime
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 2.12.9
154
168
  - !ruby/object:Gem::Dependency
155
169
  name: rspec
156
170
  requirement: !ruby/object:Gem::Requirement