Class: ChatworkTo::Process

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

Constant Summary collapse

CONTINUOUS_ERROR_LIMIT =
10

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Process

Returns a new instance of Process.



4
5
6
7
# File 'lib/chatwork_to/process.rb', line 4

def initialize(opts = {})
  @options = opts
  prepare
end

Instance Method Details

#runObject



9
10
11
12
13
14
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
# File 'lib/chatwork_to/process.rb', line 9

def run
  info('Info: ChatworkTo start.')
  loop do
    @rooms.each do |rid, data|
      @response.store(rid, @client.load_chat(rid, @response.last_chat_id(rid)))
      if @response.success?(rid)
        if @response.notify?(rid)
          notify(
            {
              'chat_list' => @response.chat_list(rid),
              'room' => {
                'id' => rid,
                'name' => data['n']
              },
              'users' => @all_users
            }
          )
        end
        @error_count = 0
      else
        init_client
        info('Warn: Restart client')
        @error_count += 1
        break
      end
    end

    sleep @interval
    if @error_count > CONTINUOUS_ERROR_LIMIT
      info("Error: Exit caused by #{CONTINUOUS_ERROR_LIMIT} continuous error has occurred.")
      break
    end
  end
end