Class: Peatio::Ranger::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/peatio/ranger.rb

Instance Method Summary collapse

Constructor Details

#initialize(authenticator, socket, logger) ⇒ Connection

Returns a new instance of Connection.


3
4
5
6
7
# File 'lib/peatio/ranger.rb', line 3

def initialize(authenticator, socket, logger)
  @authenticator = authenticator
  @socket = socket
  @logger = logger
end

Instance Method Details

#authenticate(jwt) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/peatio/ranger.rb', line 15

def authenticate(jwt)
  payload = {}
  authorized = false
  begin
    payload = @authenticator.authenticate!(jwt)
    authorized = true
  rescue Peatio::Auth::Error => error
    @logger.error error.message
  end
  return [authorized, payload]
end

#handle(msg) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/peatio/ranger.rb', line 49

def handle(msg)
  begin
    data = JSON.parse(msg)

    case data["event"]
    when "subscribe"
      subscribe data["streams"]
    when "unsubscribe"
      unsubscribe data["streams"]
    end

  rescue JSON::ParserError => error
    @logger.debug { "#{error}, msg: `#{msg}`" }
  end
end

#handshake(hs) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/peatio/ranger.rb', line 65

def handshake(hs)
  @client = Peatio::MQ::Events::Client.new(@socket)

  query = URI::decode_www_form(hs.query_string)
  subscribe(query.map {|item| item.last if item.first == "stream"})
  @logger.info "ranger: WebSocket connection openned"

  if hs.headers_downcased.key?("authorization")
    authorized, payload = authenticate(hs.headers["authorization"])

    if !authorized
      @logger.info "ranger: #{@client.user} authentication failed"
      raise EM::WebSocket::HandshakeError, "Authorization failed"
    else
      @logger.info [authorized, payload].inspect
      @client.user = payload[:uid]
      @client.authorized = true
      @logger.info "ranger: user #{@client.user} authenticated #{@client.streams}"
    end
  end
end

#send(method, data) ⇒ Object


9
10
11
12
13
# File 'lib/peatio/ranger.rb', line 9

def send(method, data)
  payload = JSON.dump(method => data)
  @logger.debug { payload }
  @socket.send payload
end

#subscribe(streams) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/peatio/ranger.rb', line 31

def subscribe(streams)
  raise "Streams must be an array of strings" unless streams.is_a?(Array)
  streams.each do |stream|
    next if stream.nil?
    @client.streams[stream] = true
  end
  send :success, message: "subscribed", streams: @client.streams.keys
end

#unsubscribe(streams) ⇒ Object


40
41
42
43
44
45
46
47
# File 'lib/peatio/ranger.rb', line 40

def unsubscribe(streams)
  raise "Streams must be an array of strings" unless streams.is_a?(Array)
  streams.each do |stream|
    next if stream.nil?
    @client.streams.delete(stream)
  end
  send :success, message: "unsubscribed", streams: @client.streams.keys
end

#update_streamsObject


27
28
29
# File 'lib/peatio/ranger.rb', line 27

def update_streams
  @socket.instance_variable_set(:@connection_handler, @client)
end