Class: Messenger::Bot::Client
- Inherits:
-
Object
- Object
- Messenger::Bot::Client
- Defined in:
- lib/messenger/bot/client.rb
Constant Summary collapse
- TIMEOUT_EXCEPTIONS =
[Timeout::Error]
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_updates ⇒ Object
-
#initialize(token, h = {}) ⇒ Client
constructor
A new instance of Client.
- #listen(&block) ⇒ Object
- #run {|_self| ... } ⇒ Object
Constructor Details
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
7 8 9 |
# File 'lib/messenger/bot/client.rb', line 7 def api @api end |
#logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/messenger/bot/client.rb', line 8 def logger @logger end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
7 8 9 |
# File 'lib/messenger/bot/client.rb', line 7 def offset @offset end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/messenger/bot/client.rb', line 7 def timeout @timeout end |
Class Method Details
.run(*args, &block) ⇒ Object
10 11 12 |
# File 'lib/messenger/bot/client.rb', line 10 def self.run(*args, &block) new(*args).run(&block) end |
Instance Method Details
#fetch_updates ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/messenger/bot/client.rb', line 34 def fetch_updates response = api.getUpdates(offset: offset, timeout: timeout) return unless response['ok'] response['result'].each do |data| update = Types::Update.new(data) @offset = update.update_id.next = (update) () yield end rescue *TIMEOUT_EXCEPTIONS retry end |
#listen(&block) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/messenger/bot/client.rb', line 26 def listen(&block) logger.info('Starting bot') running = true Signal.trap('INT') { running = false } fetch_updates(&block) while running exit end |
#run {|_self| ... } ⇒ Object
22 23 24 |
# File 'lib/messenger/bot/client.rb', line 22 def run yield self end |