Class: FlexBullet::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/flex_bullet/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Listener

Returns a new instance of Listener.



18
19
20
21
22
23
24
25
26
# File 'lib/flex_bullet/listener.rb', line 18

def initialize (args)
  @user_obj =
      if args.is_a?(FlexBullet::User)
        args
      else
        FlexBullet::User.new(args[:email], args[:auth], args[:name])
      end
  @tasker   = Tasker.new(@user_obj)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



16
17
18
# File 'lib/flex_bullet/listener.rb', line 16

def args
  @args
end

#commandObject

Returns the value of attribute command.



16
17
18
# File 'lib/flex_bullet/listener.rb', line 16

def command
  @command
end

#last_messageObject

Returns the value of attribute last_message.



16
17
18
# File 'lib/flex_bullet/listener.rb', line 16

def last_message
  @last_message
end

#senderObject

Returns the value of attribute sender.



16
17
18
# File 'lib/flex_bullet/listener.rb', line 16

def sender
  @sender
end

Instance Method Details

#get_1h_historyObject



33
34
35
36
37
38
39
40
# File 'lib/flex_bullet/listener.rb', line 33

def get_1h_history
  get_1h_history = Curl::Easy.new("https://api.pushbullet.com/v2/pushes?modified_after=#{(Time.now - 3600).to_i}") { |curl|
    curl.headers['Authorization'] = "Bearer #{@user_obj.auth}"
    curl.verbose                  = false
  }
  get_1h_history.perform
  get_1h_history.body_str
end

#get_last_messageObject



28
29
30
31
# File 'lib/flex_bullet/listener.rb', line 28

def get_last_message
  last_message = JSON::parse(get_1h_history)['pushes'].first
  Message.new(:parsed_json => last_message)
end

#stream(tb_config = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flex_bullet/listener.rb', line 42

def stream (tb_config = nil)

  FlexConfig.new('/Users/YCL/Documents/RubyProjects/EL_library/tunnel_blick/config', tb_config) unless tb_config.nil?

  EM.run {
    ws = Faye::WebSocket::Client.new("wss://stream.pushbullet.com/websocket/#{@user_obj.auth}")

    ws.on :open do |event|
      p [:open]
    end

    ws.on :message do |event|
      response = JSON::parse(event.data)

      if response['type'] == 'nop'
        print '.'
      end

      if response['type'] == 'tickle' && response['subtype'] == 'push'
        message = get_last_message

        if message.email != @user_obj.email
          @tasker.incoming(message)
        end

      end
    end

    ws.on :close do |event|
      p [:close, event.code, event.reason]
      ws = nil
    end
  }
end