telegram-bot 0.9.0.alpha1 → 0.9.0.alpha2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/telegram/bot/async.rb +40 -16
- data/lib/telegram/bot/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36cad9c75339c3e1119aec4f14645a7cdcb1c868
|
4
|
+
data.tar.gz: 0817596572b5667c47642cb2ad30dd5e51690a32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d488af6c423477bc93fe182d73f9b104c45886097896dbd400bd29c2d5eb1455c6237a1eabd206397f9ba5799798b6d1b83936662e655b2606b0998136c5f228
|
7
|
+
data.tar.gz: 21468fbab556f7c31594e0b68be13f75b51465b6418b075e4441613435bb7dc5fe02a77ef6a3c6241475fd026792d02e55a1b106b0ea89e540a522157a47281c
|
data/lib/telegram/bot/async.rb
CHANGED
@@ -30,12 +30,18 @@ module Telegram
|
|
30
30
|
# client.send_message(message)
|
31
31
|
# client.async(false) { client.send_message(other_one) }
|
32
32
|
#
|
33
|
+
# `#async=` sets global value for all threads,
|
34
|
+
# while `#async(val, &block)` is thread-safe.
|
35
|
+
#
|
33
36
|
# It can be set with custom job class or classname. By default it defines
|
34
37
|
# job classes for every client class, inherited from ApplicationRecord, which
|
35
38
|
# can be accessed via `.default_async_job`. You can integrate it with any
|
36
39
|
# other job provider by defining a class with `.perform_later(bot_id, *args)`
|
37
40
|
# method. See Async::Job for implemetation.
|
38
41
|
module Async
|
42
|
+
# Used to track missing key in a hash in local variable.
|
43
|
+
MISSING_VALUE = Object.new.freeze
|
44
|
+
|
39
45
|
module Job
|
40
46
|
class << self
|
41
47
|
def included(base)
|
@@ -78,6 +84,16 @@ module Telegram
|
|
78
84
|
def prepare_async_args(*args)
|
79
85
|
args
|
80
86
|
end
|
87
|
+
|
88
|
+
# Returns default_async_job if `true` is given,
|
89
|
+
# treats String as a constant name, or bypasses any other values.
|
90
|
+
def prepare_async_val(val)
|
91
|
+
case val
|
92
|
+
when true then default_async_job
|
93
|
+
when String then Object.const_get(val)
|
94
|
+
else val
|
95
|
+
end
|
96
|
+
end
|
81
97
|
end
|
82
98
|
|
83
99
|
class << self
|
@@ -91,38 +107,46 @@ module Telegram
|
|
91
107
|
hash = hash.dup
|
92
108
|
hash.each { |key, val| hash[key] = val.to_s if val.is_a?(Symbol) }
|
93
109
|
end
|
110
|
+
|
111
|
+
# Thread-local hash to store async config for every client.
|
112
|
+
def thread_store
|
113
|
+
Thread.current[:telegram_bot_async] ||= {}
|
114
|
+
end
|
94
115
|
end
|
95
116
|
|
96
117
|
attr_reader :id
|
97
118
|
|
98
|
-
def initialize(*, id: nil, async: nil, **
|
119
|
+
def initialize(*, id: nil, async: nil, **)
|
99
120
|
@id = id
|
100
121
|
self.async = async
|
101
122
|
super
|
102
123
|
end
|
103
124
|
|
104
|
-
# Sets
|
105
|
-
#
|
106
|
-
# Pass custom job class to perform async calls with.
|
125
|
+
# Sets default async value for all threads.
|
126
|
+
# Uses `self.class.prepare_async_val` to prepare value.
|
107
127
|
def async=(val)
|
108
|
-
@async =
|
109
|
-
case val
|
110
|
-
when true then self.class.default_async_job
|
111
|
-
when String then const_get(val)
|
112
|
-
else val
|
113
|
-
end
|
128
|
+
@async = self.class.prepare_async_val(val)
|
114
129
|
end
|
115
130
|
|
116
|
-
#
|
117
|
-
#
|
131
|
+
# Sets async value in a thread-safe way for the block.
|
132
|
+
# Uses `self.class.prepare_async_val` to prepare value.
|
133
|
+
#
|
134
|
+
# If no block is given returns previously set value or the global one,
|
135
|
+
# set by #async=.
|
118
136
|
def async(val = true)
|
119
|
-
|
137
|
+
thread_key = object_id
|
138
|
+
thread_store = Async.thread_store
|
139
|
+
return thread_store.fetch(thread_key) { @async } unless block_given?
|
120
140
|
begin
|
121
|
-
old_val =
|
122
|
-
|
141
|
+
old_val = thread_store.fetch(thread_key) { MISSING_VALUE }
|
142
|
+
thread_store[thread_key] = self.class.prepare_async_val(val)
|
123
143
|
yield
|
124
144
|
ensure
|
125
|
-
|
145
|
+
if MISSING_VALUE == old_val
|
146
|
+
thread_store.delete(thread_key)
|
147
|
+
else
|
148
|
+
thread_store[thread_key] = old_val
|
149
|
+
end
|
126
150
|
end
|
127
151
|
end
|
128
152
|
|
data/lib/telegram/bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram-bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.0.
|
4
|
+
version: 0.9.0.alpha2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Melentiev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: 1.3.1
|
161
161
|
requirements: []
|
162
162
|
rubyforge_project:
|
163
|
-
rubygems_version: 2.
|
163
|
+
rubygems_version: 2.4.6
|
164
164
|
signing_key:
|
165
165
|
specification_version: 4
|
166
166
|
summary: Library for building Telegram Bots with Rails integration
|