Class: ChatworkTo::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(rooms) ⇒ Response

Returns a new instance of Response.



3
4
5
6
7
8
# File 'lib/chatwork_to/response.rb', line 3

def initialize(rooms)
  @responses = {}
  rooms.each do |rid|
    @responses[rid] = default_response
  end
end

Instance Method Details

#chat_list(room_id) ⇒ Object



50
51
52
# File 'lib/chatwork_to/response.rb', line 50

def chat_list(room_id)
  @responses[room_id]['chat_list']
end

#last_chat_id(room_id) ⇒ Object



46
47
48
# File 'lib/chatwork_to/response.rb', line 46

def last_chat_id(room_id)
  @responses[room_id]['last_chat_id']
end

#notify?(room_id) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/chatwork_to/response.rb', line 42

def notify?(room_id)
  @responses[room_id]['notify']
end

#store(room_id, response) ⇒ Object



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
# File 'lib/chatwork_to/response.rb', line 10

def store(room_id, response)
  old = @responses[room_id]

  current = default_response.merge({ 'success' => response['status']['success'] })
  if current['success']
    current['chat_list'] = response['result']['chat_list']
    if old['last_chat_id'].eql?(0)
      # init_load
      if current['chat_list'].present?
        current['last_chat_id'] = current['chat_list'].last.fetch('id')
      end
    else
      if current['chat_list'].present?
        # updated
        current['last_chat_id'] = current['chat_list'].last.fetch('id')
        current['notify'] = true
      else
        # not updated
        current['last_chat_id'] = old['last_chat_id']
      end
    end
  else
    current['error_message'] = response['status']['message']
  end

  @responses[room_id] = current
end

#success?(room_id) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/chatwork_to/response.rb', line 38

def success?(room_id)
  @responses[room_id]['success']
end