Class: Peatio::Ranger::Connection
- Inherits:
-
Object
- Object
- Peatio::Ranger::Connection
- Defined in:
- lib/peatio/ranger.rb
Instance Method Summary collapse
- #authenticate(jwt) ⇒ Object
- #handle(msg) ⇒ Object
- #handshake(hs) ⇒ Object
-
#initialize(authenticator, socket, logger) ⇒ Connection
constructor
A new instance of Connection.
- #send(method, data) ⇒ Object
- #subscribe(streams) ⇒ Object
- #unsubscribe(streams) ⇒ Object
- #update_streams ⇒ Object
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 = {} = false begin payload = @authenticator.authenticate!(jwt) = true rescue Peatio::Auth::Error => error @logger.error error. end return [, 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") , payload = authenticate(hs.headers["authorization"]) if ! @logger.info "ranger: #{@client.user} authentication failed" raise EM::WebSocket::HandshakeError, "Authorization failed" else @logger.info [, payload].inspect @client.user = payload[:uid] @client. = 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_streams ⇒ Object
27 28 29 |
# File 'lib/peatio/ranger.rb', line 27 def update_streams @socket.instance_variable_set(:@connection_handler, @client) end |