Class: Botomizer::Client
- Inherits:
-
Object
- Object
- Botomizer::Client
- Defined in:
- lib/botomizer/client.rb
Instance Method Summary collapse
- #bot ⇒ Object
- #cache ⇒ Object
- #chat ⇒ Object
- #clean_history ⇒ Object
- #delete_message(id = nil) ⇒ Object
- #delete_message!(id = nil) ⇒ Object
- #delete_previous_message ⇒ Object
- #handle(request, handler) ⇒ Object
-
#initialize(client, options = {}) ⇒ Client
constructor
A new instance of Client.
- #message ⇒ Object
- #name ⇒ Object
- #send(method, query = {}) ⇒ Object
- #send!(method, query = {}) ⇒ Object
- #send_message(text, options = {}) ⇒ Object
- #send_message!(text, options = {}) ⇒ Object
- #webhook ⇒ Object
Constructor Details
#initialize(client, options = {}) ⇒ Client
Returns a new instance of Client.
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/botomizer/client.rb', line 2 def initialize(client, = {}) begin require "botomizer/#{client}".downcase @bot = Object.const_get("Botomizer::#{client.capitalize}") @cache = Botomizer::Cache.new([:cache] || nil, :botomizer_cmd) @name = client @bot. = @bot.client = self rescue LoadError raise Botomizer::UnsupportedClient.new("Unsupported client #{client}") end end |
Instance Method Details
#bot ⇒ Object
32 33 34 |
# File 'lib/botomizer/client.rb', line 32 def bot @bot end |
#cache ⇒ Object
36 37 38 |
# File 'lib/botomizer/client.rb', line 36 def cache @cache end |
#chat ⇒ Object
28 29 30 |
# File 'lib/botomizer/client.rb', line 28 def chat bot.chat end |
#clean_history ⇒ Object
58 59 60 |
# File 'lib/botomizer/client.rb', line 58 def clean_history @cache.delete(chat.id) end |
#delete_message(id = nil) ⇒ Object
86 87 88 |
# File 'lib/botomizer/client.rb', line 86 def (id = nil) bot.(id) end |
#delete_message!(id = nil) ⇒ Object
90 91 92 93 94 |
# File 'lib/botomizer/client.rb', line 90 def (id = nil) result = (id) raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed result end |
#delete_previous_message ⇒ Object
96 97 98 99 100 |
# File 'lib/botomizer/client.rb', line 96 def if @cache.exists?("prev_#{chat.id}") (@cache.fetch("prev_#{chat.id}")) end end |
#handle(request, handler) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/botomizer/client.rb', line 40 def handle(request, handler) bot.parse_request(request) handler = handler.new(self) if .cmd? @cache.write(chat.id, .cmd) else .cmd = @cache.fetch(chat.id) end if handler.respond_to? "cmd_#{.cmd}" handler.public_send("cmd_#{.cmd}") else handler.cmd_fallback end end |
#message ⇒ Object
24 25 26 |
# File 'lib/botomizer/client.rb', line 24 def bot. end |
#name ⇒ Object
16 17 18 |
# File 'lib/botomizer/client.rb', line 16 def name @name end |
#send(method, query = {}) ⇒ Object
62 63 64 |
# File 'lib/botomizer/client.rb', line 62 def send(method, query = {}) bot.send(method, query) end |
#send!(method, query = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/botomizer/client.rb', line 66 def send!(method, query = {}) result = send(method, query) raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed result end |
#send_message(text, options = {}) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/botomizer/client.rb', line 72 def (text, = {}) result = bot.(text, ) @cache.write("prev_#{chat.id}", result[:message_id]) if result.has_key?(:message_id) result end |
#send_message!(text, options = {}) ⇒ Object
80 81 82 83 84 |
# File 'lib/botomizer/client.rb', line 80 def (text, = {}) result = (text, ) raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed result end |
#webhook ⇒ Object
20 21 22 |
# File 'lib/botomizer/client.rb', line 20 def webhook @bot.webhook end |