Class: Tgbot
- Inherits:
-
Object
- Object
- Tgbot
- Defined in:
- lib/tgbot.rb,
lib/tgbot/version.rb
Defined Under Namespace
Classes: Update
Constant Summary collapse
- APIDOC =
Psych.load_file File.join __dir__, 'api.yaml'
- VERSION =
'0.4.6'.freeze
Instance Attribute Summary collapse
-
#debug(msg) ⇒ Object
Returns the value of attribute debug.
Class Method Summary collapse
Instance Method Summary collapse
- #api_version ⇒ Object
- #finish(&blk) ⇒ Object
-
#initialize(token, proxy: nil, debug: false) ⇒ Tgbot
constructor
A new instance of Tgbot.
- #on(pattern = nil, name: nil, before_all: false, after_all: false, &blk) ⇒ Object
- #run(&blk) ⇒ Object
- #start(&blk) ⇒ Object
Constructor Details
#initialize(token, proxy: nil, debug: false) ⇒ Tgbot
Returns a new instance of Tgbot.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tgbot.rb', line 20 def initialize(token, proxy: nil, debug: false) @prefix = "/bot#{token}" @client = HTTP.persistent "https://api.telegram.org" if proxy addr, port = *proxy.split(':') @client = @client.via(addr, port.to_i) end @debug = debug @commands = [] @start = @finish = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object (private)
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/tgbot.rb', line 187 def method_missing meth, *args, &blk meth = meth.to_s.split('_').map. with_index { |x, i| i.zero? ? x : (x[0].upcase + x[1..-1]) }.join payload = make_payload meth, *args debug "/#{meth} #{payload}" result = @client.post("#@prefix/#{meth}", form: payload).to_s result = json_to_ostruct(result) debug "=> #{result}" blk ? blk.call(result) : result end |
Instance Attribute Details
#debug(msg) ⇒ Object
Returns the value of attribute debug.
18 19 20 |
# File 'lib/tgbot.rb', line 18 def debug @debug end |
Class Method Details
.run(*args, &blk) ⇒ Object
12 13 14 15 16 |
# File 'lib/tgbot.rb', line 12 def self.run(*args, &blk) bot = new(*args) bot.instance_exec(bot, &blk) if blk bot.run end |
Instance Method Details
#api_version ⇒ Object
181 182 183 |
# File 'lib/tgbot.rb', line 181 def api_version search_in_doc(/changes/i, '')[0].desc[0].content[/\d+\.\d+/] end |
#finish(&blk) ⇒ Object
36 37 38 |
# File 'lib/tgbot.rb', line 36 def finish &blk @finish = blk end |
#on(pattern = nil, name: nil, before_all: false, after_all: false, &blk) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/tgbot.rb', line 40 def on pattern=nil, name: nil, before_all: false, after_all: false, &blk if before_all && after_all raise ArgumentError, 'before_all and after_all can\'t both be true' end @commands << [pattern, name, before_all, after_all, blk] end |
#run(&blk) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/tgbot.rb', line 148 def run &blk @offset = 0 @timeout = 2 @updates = [] instance_exec(self, &@start) if @start loop do while x = @updates.shift u = Update === x ? x : Update.new(self, x) @offset = u.update_id if u.update_id && @offset < u.update_id @tasks = @commands.select { |pattern, *| u.match? pattern } .group_by { |e| e[2] ? :before : e[3] ? :after : nil } .values_at(:before, nil, :after).compact.flatten(1) while t = @tasks.shift debug ">> #{t[1] || t[0]}" u.instance_exec(u.match(t[0]), u, t, &t[4]) end end res = get_updates offset: @offset + 1, limit: 7, timeout: 15 if res.ok @updates.push *res.result else debug "#{res.error_code}: #{res.description}" end end rescue HTTP::ConnectionError debug "connect failed, check proxy?" retry rescue Interrupt instance_exec(self, &@finish) if @finish ensure @client.close end |
#start(&blk) ⇒ Object
32 33 34 |
# File 'lib/tgbot.rb', line 32 def start &blk @start = blk end |