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.



11
12
13
14
15
16
17
18
19
# File 'lib/flex_bullet/listener.rb', line 11

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.



9
10
11
# File 'lib/flex_bullet/listener.rb', line 9

def args
  @args
end

#commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/flex_bullet/listener.rb', line 9

def command
  @command
end

#last_messageObject

Returns the value of attribute last_message.



9
10
11
# File 'lib/flex_bullet/listener.rb', line 9

def last_message
  @last_message
end

#senderObject

Returns the value of attribute sender.



9
10
11
# File 'lib/flex_bullet/listener.rb', line 9

def sender
  @sender
end

Instance Method Details

#get_1h_historyObject



26
27
28
29
30
31
32
33
# File 'lib/flex_bullet/listener.rb', line 26

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



21
22
23
24
# File 'lib/flex_bullet/listener.rb', line 21

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



35
36
37
38
39
40
41
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
# File 'lib/flex_bullet/listener.rb', line 35

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