Class: GrubY::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, session: Session.new, polling_timeout: 30) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
# File 'lib/gruubY/client.rb', line 13

def initialize(token, session: Session.new, polling_timeout: 30)
  @bot = Bot.new(token, session: session)
  @api = @bot.api
  @session = session
  @group_manager = GroupManager.new(@api, session: @session)
  @polling_timeout = polling_timeout
  @polling_thread = nil
  @running = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



215
216
217
218
219
# File 'lib/gruubY/client.rb', line 215

def method_missing(name, *args, **kwargs, &block)
  return super unless @api.respond_to?(name)

  @api.public_send(name, *args, **kwargs, &block)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



11
12
13
# File 'lib/gruubY/client.rb', line 11

def api
  @api
end

#botObject (readonly)

Returns the value of attribute bot.



11
12
13
# File 'lib/gruubY/client.rb', line 11

def bot
  @bot
end

#group_managerObject (readonly)

Returns the value of attribute group_manager.



11
12
13
# File 'lib/gruubY/client.rb', line 11

def group_manager
  @group_manager
end

#sessionObject (readonly)

Returns the value of attribute session.



11
12
13
# File 'lib/gruubY/client.rb', line 11

def session
  @session
end

Class Method Details

.compose(*clients, &block) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/gruubY/client.rb', line 192

def self.compose(*clients, &block)
  clients.each(&:start)
  block.call if block
  clients.each(&:idle)
ensure
  clients.each(&:stop)
end

Instance Method Details

#add_handler(handler_or_event, filter = nil, group: 0, &block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/gruubY/client.rb', line 65

def add_handler(handler_or_event, filter=nil, group: 0, &block)
  if handler_or_event.is_a?(GrubY::Handlers::BaseHandler)
    register_handler_object(handler_or_event)
  else
    @bot.on(handler_or_event, filter, group: group, &block)
  end
end

#build_chat(data) ⇒ Object



176
177
178
# File 'lib/gruubY/client.rb', line 176

def build_chat(data)
  GrubY::Chat.new(data, api: @api, client: self)
end

#build_message(data) ⇒ Object



172
173
174
# File 'lib/gruubY/client.rb', line 172

def build_message(data)
  GrubY::Message.new(data, api: @api, client: self)
end

#build_user(data) ⇒ Object



180
181
182
# File 'lib/gruubY/client.rb', line 180

def build_user(data)
  GrubY::User.new(data, api: @api, client: self)
end

#connectObject



47
48
49
# File 'lib/gruubY/client.rb', line 47

def connect
  start
end

#disconnectObject



51
52
53
# File 'lib/gruubY/client.rb', line 51

def disconnect
  stop
end

#export_session_stringObject



142
143
144
# File 'lib/gruubY/client.rb', line 142

def export_session_string
  @session.to_h.to_json
end

#get_dc_option(dc_id) ⇒ Object



160
161
162
# File 'lib/gruubY/client.rb', line 160

def get_dc_option(dc_id)
  @session.get("dc:#{dc_id}")
end

#get_file(file_id) ⇒ Object



168
169
170
# File 'lib/gruubY/client.rb', line 168

def get_file(file_id)
  @api.get_file(file_id)
end

#get_parse_modeObject



151
152
153
# File 'lib/gruubY/client.rb', line 151

def get_parse_mode
  @session.get("__parse_mode__")
end

#get_sessionObject



164
165
166
# File 'lib/gruubY/client.rb', line 164

def get_session
  @session
end

#idleObject



184
185
186
187
188
189
190
# File 'lib/gruubY/client.rb', line 184

def idle
  @idle = true
  trap_signals
  sleep 0.3 while @idle
ensure
  @idle = false
end

#on(event, filter = nil, &block) ⇒ Object



77
78
79
# File 'lib/gruubY/client.rb', line 77

def on(event, filter=nil, &block)
  add_handler(event, filter, &block)
end

#on_error(exceptions = nil, filters = nil, group: 0, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/gruubY/client.rb', line 115

def on_error(exceptions = nil, filters = nil, group: 0, &block)
  add_handler(
    GrubY::Handlers::ErrorHandler.new(
      block,
      filters: filters,
      group: group,
      exceptions: exceptions
    )
  )
end

#process_update(update) ⇒ Object



134
135
136
# File 'lib/gruubY/client.rb', line 134

def process_update(update)
  @bot.process_update(update)
end

#remove_handler(event, handler_id = nil, &block) ⇒ Object



73
74
75
# File 'lib/gruubY/client.rb', line 73

def remove_handler(event, handler_id = nil, &block)
  @bot.remove_handler(event, handler_id, &block)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/gruubY/client.rb', line 221

def respond_to_missing?(name, include_private = false)
  @api.respond_to?(name) || super
end

#restartObject



42
43
44
45
# File 'lib/gruubY/client.rb', line 42

def restart
  stop
  start
end

#runObject



55
56
57
58
59
# File 'lib/gruubY/client.rb', line 55

def run
  start
  idle
  stop
end

#set_dc(dc_id, config = {}) ⇒ Object



155
156
157
158
# File 'lib/gruubY/client.rb', line 155

def set_dc(dc_id, config = {})
  @session.set("dc:#{dc_id}", config)
  config
end

#set_parse_mode(mode) ⇒ Object



146
147
148
149
# File 'lib/gruubY/client.rb', line 146

def set_parse_mode(mode)
  @session.set("__parse_mode__", mode.to_s)
  mode
end

#startObject



23
24
25
26
27
28
29
30
31
# File 'lib/gruubY/client.rb', line 23

def start
  return if @running

  @bot.emit(:connect)
  @running = true
  start_polling
  @bot.emit(:start)
  self
end

#stopObject



33
34
35
36
37
38
39
40
# File 'lib/gruubY/client.rb', line 33

def stop
  @bot.emit(:stop) if @running
  @running = false
  @polling_thread&.kill
  @polling_thread = nil
  @bot.emit(:disconnect)
  self
end

#stop_transmissionObject



138
139
140
# File 'lib/gruubY/client.rb', line 138

def stop_transmission
  true
end

#terminateObject



61
62
63
# File 'lib/gruubY/client.rb', line 61

def terminate
  stop
end

#use(&block) ⇒ Object



126
127
128
# File 'lib/gruubY/client.rb', line 126

def use(&block)
  @bot.use(&block)
end

#use_plugin(path) ⇒ Object



130
131
132
# File 'lib/gruubY/client.rb', line 130

def use_plugin(path)
  @bot.use_plugin(path)
end