Class: KermitPFC

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

Overview

Masterclass that adapts the rakefile into a class to use the commander gem

See Also:

Author:

  • Daniel Machado Fernandez

Version:

  • 1.0

Instance Method Summary collapse

Constructor Details

#initializeKermitPFC

Load the config file



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
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
76
77
# File 'lib/kermitpfc.rb', line 18

def initialize
  
  begin

    cleaning_the_house
    
    puts 'pooling'
    Logging::logger.info('Pooling')
    pool = []
    pool << Thread.new { initialize_websocket_server }
    
    puts 'websocket started'
    Logging::logger.info('WebSocket started')

    for i in 1..Logging::Settings.twitter.streams

      puts "adding twitter adapter ##{i}"
      Logging::logger.info("adding twitter adapter ##{i}")

      pool << Thread.new { twitter_adapter i }
      sleep 1

    end

    puts 'twitter adapters added'
    Logging::logger.info('twitter adapters added')

    if Logging::Settings.rpg
    
      pool << Thread.new { random_adapter }
      puts 'random adapter added'
      Logging::logger.info('random adapter added')
    
      pool << Thread.new { random_converter }
      puts 'random converter added'
      Logging::logger.info('random converter added')
    
    end

    pool << Thread.new { twitter_converter }
    puts 'twitter converter added'
    Logging::logger.info('twitter converter added')
      
     if pool.class != NilClass 
      pool.each do |thread|
        thread.join
      end
    end
  rescue Exception => e
    
    Logging::logger.error(e)
    puts e
    if pool.class != NilClass
      pool.each do |thread|
        Thread.kill(thread)     
      end
    end
  end

end

Instance Method Details

#cleaning_the_houseObject

Deletes the db keys previously used



80
81
82
83
84
85
# File 'lib/kermitpfc.rb', line 80

def cleaning_the_house

  dao = DAO.new
  dao.clean

end

#initialize_websocket_serverObject

Starts the WebSocket Server



88
89
90
91
92
93
# File 'lib/kermitpfc.rb', line 88

def initialize_websocket_server

  ws = WebSocketServer.new
  ws.start

end

#random_adapter(stream = 1) ⇒ Object

Starts the Random Adapter

Parameters:

  • stream (Integer) (defaults to: 1)

    the number of the stream



108
109
110
111
112
113
# File 'lib/kermitpfc.rb', line 108

def random_adapter stream=1

  ra = RandomAdapter.new
  ra.connect_stream stream

end

#random_converterObject

Starts the Random Converter



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kermitpfc.rb', line 152

def random_converter

  dao = DAO.new 'rpg'

  while true

    status = dao.get_status
    puts 'Status retrieved'

    while status != nil

      rc = RandomConverter.new
      usmf = rc.to_usmf status

      dao.publish usmf.to_hash.to_json
      puts 'Published'

      status = dao.get_status

    end

    puts 'All the statuses were been sent'
    sleep 2

  end

end

#twitter_adapter(stream) ⇒ Object

Starts the Twitter Adapter

Parameters:

  • stream (Integer)

    the number of the stream



98
99
100
101
102
103
# File 'lib/kermitpfc.rb', line 98

def twitter_adapter stream

  ta = TwitterAdapter.new
  ta.connect_stream stream

end

#twitter_converterObject

Starts the Twitter Converter



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/kermitpfc.rb', line 116

def twitter_converter

  dao = DAO.new 'twitter'
  z = 0
  while true

    Logging::logger.info "TWEETS: #{z}"

    status = dao.get_status

    while status != nil
      
      puts 'Status retrieved'
      tc = TwitterConverter.new
      usmf = tc.to_usmf status
      json = usmf.to_hash.to_json
      
      if json != 'null'
        dao.publish json
        puts 'Published'
        z = z + 1
      end
      
      status = dao.get_status

    end


    puts 'All the statuses were been sent'
    sleep 2

  end

end