telegram-bot 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a045d870c213b5136f08e93b6f6e243b9276c6bb
4
- data.tar.gz: f83f3bfd325fbb5014939ea0c4db049c2d8a228a
3
+ metadata.gz: 8bb8d3d1265ef9c71cb7dd500ca18ca98e8bdbc6
4
+ data.tar.gz: 478eb9dde35d1ef651f1e889b66125017674c21d
5
5
  SHA512:
6
- metadata.gz: ed51cc7ddcb72060182cc6f22ef8e0ab11c7ac07223a03c6c24b301db7a46bab88800b1a00044558d40d17a87fde09bdfa0b7c310039c97a31b7bb172d9f0e56
7
- data.tar.gz: 5617505f1d36c7967777bb27c0b1fd1025f7f0617629c884a67ca8092702642760526295ea21683b7d924b2d9f2e5f8350b847600a3506f289b225511fc73d5f
6
+ metadata.gz: 9168018578598dc1ba6cefa04695ce7b2f98de74591cc82f301a8440c31fbef66cf5e7a51ec545f3937ef25a68278d573dfbc6aa22cbf54f9f79d2d520629f11
7
+ data.tar.gz: 7489fc137abd149da5d7dc8819f736363753c3a6aeccc0c5c925d541656f846f03455a60d0c8db4553d7a2e5cfaf4edd5c3c18217dfdd7dd1b6cb48f4cc58270
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.6.0
2
+
3
+ - StaleChat error.
4
+ - Encode arrays as json in request body.
5
+
1
6
  # 0.5.0
2
7
 
3
8
  - MessageContext.
data/README.md CHANGED
@@ -89,6 +89,9 @@ bot.extend Telegram::Bot::Client::TypedResponse
89
89
  bot.get_me.class # => Telegram::Bot::Types::User
90
90
  ```
91
91
 
92
+ Any API request error will raise `Telegram::Bot::Error` with description in its message.
93
+ Special `Telegram::Bot::StaleChat` is raised when bot can't post messages to the chat anymore.
94
+
92
95
  ### Controller
93
96
 
94
97
  ```ruby
data/lib/telegram/bot.rb CHANGED
@@ -7,6 +7,25 @@ module Telegram
7
7
  class Error < StandardError; end
8
8
  class NotFound < Error; end
9
9
 
10
+ # Error class for events when chat is not available anymore for bot.
11
+ # While Telegram has same error codes for different messages and there is no
12
+ # official docs for this error codes it uses `description` to
13
+ # check response.
14
+ class StaleChat < Error
15
+ DESCRIPTIONS = [
16
+ 'bot was kicked',
17
+ "can't write to",
18
+ 'group chat is deactivated',
19
+ ].freeze
20
+
21
+ class << self
22
+ def match_response?(response)
23
+ description = response['description'].to_s
24
+ DESCRIPTIONS.any? { |x| description[x] }
25
+ end
26
+ end
27
+ end
28
+
10
29
  autoload :Client, 'telegram/bot/client'
11
30
  autoload :ClientStub, 'telegram/bot/client_stub'
12
31
  autoload :Middleware, 'telegram/bot/middleware'
@@ -34,8 +34,9 @@ module Telegram
34
34
 
35
35
  # Encodes nested hashes as json.
36
36
  def prepare_body(body)
37
+ body = body.dup
37
38
  body.each do |k, val|
38
- body[k] = val.to_json if val.is_a?(Hash)
39
+ body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
39
40
  end
40
41
  end
41
42
  end
@@ -67,14 +68,17 @@ module Telegram
67
68
  client.debug_dev = nil
68
69
  end
69
70
 
70
- def request(action, body = {})
71
+ def request(action, body = {}) # rubocop:disable PerceivedComplexity
71
72
  res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
72
73
  status = res.status
73
74
  return JSON.parse(res.body) if 300 > status
74
75
  result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
75
76
  err_msg = "#{res.reason}: #{result && result['description'] || '-'}"
76
- # NotFound is raised only for valid responses from Telegram
77
- raise NotFound, err_msg if 404 == status && result
77
+ if result
78
+ # NotFound is raised only for valid responses from Telegram
79
+ raise NotFound, err_msg if 404 == status
80
+ raise StaleChat, err_msg if StaleChat.match_response?(result)
81
+ end
78
82
  raise Error, err_msg
79
83
  end
80
84
 
@@ -1,6 +1,6 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.5.0'.freeze
3
+ VERSION = '0.6.0'.freeze
4
4
 
5
5
  def self.gem_version
6
6
  Gem::Version.new VERSION
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.5.0
4
+ version: 0.6.0
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-03-14 00:00:00.000000000 Z
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -145,3 +145,4 @@ signing_key:
145
145
  specification_version: 4
146
146
  summary: Library for building Telegram Bots with Rails integration
147
147
  test_files: []
148
+ has_rdoc: