Class: Account

Inherits:
Object
  • Object
show all
Defined in:
lib/raphael/account.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, token) ⇒ Account

Returns a new instance of Account.



8
9
10
11
12
13
# File 'lib/raphael/account.rb', line 8

def initialize(tag, token)
  @tag = tag
  @rest = Twitter::REST::Client.new(token)
  @stream = Twitter::Streaming::Client.new(token)
  @screen_name = @rest.user.screen_name
end

Class Attribute Details

.accountsObject (readonly)

Returns the value of attribute accounts.



49
50
51
# File 'lib/raphael/account.rb', line 49

def accounts
  @accounts
end

Instance Attribute Details

#restObject (readonly)

Returns the value of attribute rest.



4
5
6
# File 'lib/raphael/account.rb', line 4

def rest
  @rest
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



4
5
6
# File 'lib/raphael/account.rb', line 4

def screen_name
  @screen_name
end

#streamObject (readonly)

Returns the value of attribute stream.



4
5
6
# File 'lib/raphael/account.rb', line 4

def stream
  @stream
end

#tagObject (readonly)

Returns the value of attribute tag.



4
5
6
# File 'lib/raphael/account.rb', line 4

def tag
  @tag
end

#threadObject

Returns the value of attribute thread.



5
6
7
# File 'lib/raphael/account.rb', line 5

def thread
  @thread
end

Class Method Details

.allObject



55
56
57
# File 'lib/raphael/account.rb', line 55

def all
  Array(@accounts)
end

.find(screen_name) ⇒ Object



59
60
61
# File 'lib/raphael/account.rb', line 59

def find screen_name
  Account.all.find{|a|a.screen_name == screen_name}
end

.load_yaml(path) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/raphael/account.rb', line 67

def load_yaml path
  YAML.load_file(path).each do |tag, token|
    Account.register(tag, token)
  end
rescue
  Error.callback($!)
end

.register(*args) ⇒ Object



51
52
53
# File 'lib/raphael/account.rb', line 51

def register *args
  @accounts << self.new(*args)
end

.threadsObject



63
64
65
# File 'lib/raphael/account.rb', line 63

def threads
  Account.all.map{|a|a.thread}
end

Instance Method Details

#start_streamObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/raphael/account.rb', line 15

def start_stream
  self.stream.user replies: 'all' do |obj|
    begin
      case obj
      when Twitter::Tweet
        Tweet.callback obj, self

        obj.text =~ /^@\w+?\s+?(.+)/
        if $1
          obj.args = $1.split
        end
        Command.all.each do |command|
          if obj.text =~ /^@(?:nkroid)\s+#{command.arg}/
            command.proc.call obj, self
          end
        end
      when Twitter::Streaming::FriendList
        FriendList.callback obj, self
      when Twitter::Streaming::Event
        Event.callback obj, self
      when Twitter::Streaming::DeletedTweet
        DeletedTweet.callback obj, self
      end
    rescue
      Error.callback($!)
      next
    end
  end
rescue
  Error.callback($!)
  retry
end