Class: Botomizer::Client

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

Instance Method Summary collapse

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, options = {})
  begin
    require "botomizer/#{client}".downcase

    @bot    = Object.const_get("Botomizer::#{client.capitalize}")
    @cache  = Botomizer::Cache.new(options[:cache] || nil, :botomizer_cmd)
    @name   = client
    @bot.options = options
    @bot.client = self
  rescue LoadError
    raise Botomizer::UnsupportedClient.new("Unsupported client #{client}")
  end
end

Instance Method Details

#botObject



32
33
34
# File 'lib/botomizer/client.rb', line 32

def bot
  @bot
end

#cacheObject



36
37
38
# File 'lib/botomizer/client.rb', line 36

def cache
  @cache
end

#chatObject



28
29
30
# File 'lib/botomizer/client.rb', line 28

def chat
  bot.chat
end

#clean_historyObject



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 delete_message(id = nil)
  bot.delete_message(id)
end

#delete_message!(id = nil) ⇒ Object



90
91
92
93
94
# File 'lib/botomizer/client.rb', line 90

def delete_message!(id = nil)
  result = delete_message(id)
  raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed
  result
end

#delete_previous_messageObject



96
97
98
99
100
# File 'lib/botomizer/client.rb', line 96

def delete_previous_message
  if @cache.exists?("prev_#{chat.id}")
    delete_message(@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 message.cmd?
    @cache.write(chat.id, message.cmd)
  else
    message.cmd = @cache.fetch(chat.id)
  end

  if handler.respond_to? "cmd_#{message.cmd}"
    handler.public_send("cmd_#{message.cmd}")
  else
    handler.cmd_fallback
  end
end

#messageObject



24
25
26
# File 'lib/botomizer/client.rb', line 24

def message
  bot.message
end

#nameObject



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 send_message(text, options = {})
  result = bot.send_message(text, options)

  @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 send_message!(text, options = {})
  result = send_message(text, options)
  raise Botomizer::ResponseError.new(result[:message]) if result[:status] == :failed
  result
end

#webhookObject



20
21
22
# File 'lib/botomizer/client.rb', line 20

def webhook
  @bot.webhook
end