telegrammer 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6f66035f6ea3cd2d7f626c10b8463a1b9664d18
4
- data.tar.gz: e7c72d53b04afd5f9e7fc00f68ccbed4ae3ab41f
3
+ metadata.gz: 93c3f40e3cac5da380be28df9f3e8764fb391c48
4
+ data.tar.gz: 6e3d816aa9669bc7efe93d773025f68fa4886e85
5
5
  SHA512:
6
- metadata.gz: 3e4ca47906a59aae2806c82dec52710c8703540488890770db7b8c548e821e83f41a3be04088562eec255a988d72bcf9803c4b14ea4c0369dbe26193571d46a6
7
- data.tar.gz: 86cb5e06a25dbecd827fee880c471fbe32611d3c5344087a72053d41eb2e0fbb0e94be95f5a9a6fa91ecb51afeb11fd874ee0123205ad6be1e8dbc281298ba5f
6
+ metadata.gz: 9e6543b008d751c428095168dade2d35696030dc1c3255145bfc66fd3c7ad69de63c83b39bbc5634998429b0fa268bb5ffa536f984e2c9ff60fe098c0c3cedb9
7
+ data.tar.gz: 1905d67b7f5454d079ae4188711d9d98a818cd0689b3debb364c686bd68573f895f3ab2de4f159ef85c3dcb08df837eebb6a316a04e08b5c97950036f4c0aba3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ v0.5.0
2
+ ======
3
+
4
+ * Option to fail silently on get_updates method. Thanks to TheZ3ro!
5
+
1
6
  v0.4.1
2
7
  ======
3
8
 
data/lib/telegrammer.rb CHANGED
@@ -56,5 +56,12 @@ module Telegrammer
56
56
  super("Telegram API Service unavailable (HTTP error code #{status_code})")
57
57
  end
58
58
  end
59
+
60
+ # Error returned when HTTPClient raise a timeout (?)
61
+ class TimeoutError < StandardError
62
+ def initialize(message)
63
+ super("Timeout reached. Message: #{message}")
64
+ end
65
+ end
59
66
  end
60
67
  end
@@ -4,7 +4,7 @@ module Telegrammer
4
4
  attr_reader :result
5
5
  attr_reader :success
6
6
 
7
- def initialize(response)
7
+ def initialize(response,fail_silently = false)
8
8
  if response.status < 500
9
9
  @body = response.body
10
10
 
@@ -14,10 +14,14 @@ module Telegrammer
14
14
  if @success
15
15
  @result = data['result']
16
16
  else
17
- fail Telegrammer::Errors::BadRequestError, data['error_code'], data['description']
17
+ if !fail_silently
18
+ fail Telegrammer::Errors::BadRequestError, data['error_code'], data['description']
19
+ end
18
20
  end
19
21
  else
20
- fail Telegrammer::Errors::ServiceUnavailableError, response.status
22
+ if !fail_silently
23
+ fail Telegrammer::Errors::ServiceUnavailableError, response.status
24
+ end
21
25
  end
22
26
  end
23
27
 
@@ -18,6 +18,7 @@ module Telegrammer
18
18
  @api_token = api_token
19
19
  @offset = 0
20
20
  @timeout = 60
21
+ @fail_silently = false
21
22
  @connection = HTTPClient.new
22
23
 
23
24
  @me = get_me
@@ -25,6 +26,11 @@ module Telegrammer
25
26
 
26
27
  # Get incoming updates using long polling
27
28
  #
29
+ # @param [Hash] opts Options when getting updates
30
+ # @option params [Integer] :fail_silently Optional. Ignore every Connection Error. Default: false
31
+ # @option params [Integer] :offset Optional. Sequential number of the first photo to be returned. By default, all photos are returned.
32
+ # @option params [Integer] :timeout Optional. Timeout in minutes. 0 for short polling, Default: 60.
33
+ #
28
34
  # @example
29
35
  # bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
30
36
  #
@@ -36,11 +42,23 @@ module Telegrammer
36
42
  # # To learn more about commands, see https://core.telegram.org/bots#commands
37
43
  # end
38
44
  #
45
+ # bot.get_updates({fail_silently:true, timeout:20}) do |message|
46
+ # puts "In chat #{message.chat.id}, @#{message.from.username} said: #{message.text}"
47
+ # bot.send_message(chat_id: message.chat.id, text: "You said: #{message.text}")
48
+ #
49
+ # # Here you can also process message text to detect user commands
50
+ # # To learn more about commands, see https://core.telegram.org/bots#commands
51
+ # end
52
+ #
39
53
  # @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
40
54
  # @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
41
- def get_updates(&_block)
55
+ # @raise [Telegrammer::Errors::TimeoutError] if HTTPClient connection goes timeout
56
+ def get_updates(opts={}, &_block)
42
57
  loop do
43
- response = api_request('getUpdates', { offset: @offset, timeout: @timeout }, nil)
58
+ if opts[:fail_silently]
59
+ @fail_silently = true
60
+ end
61
+ response = api_request('getUpdates', { offset: opts[:offset] || @offset, timeout: opts[:timeout] || @timeout }, nil)
44
62
 
45
63
  response.result.each do |raw_update|
46
64
  update = Telegrammer::DataTypes::Update.new(raw_update)
@@ -440,14 +458,20 @@ module Telegrammer
440
458
  end
441
459
  end
442
460
 
443
- response = @connection.post(
444
- "#{API_ENDPOINT}/#{api_uri}",
445
- validated_params,
446
- 'User-Agent' => "Telegrammer/#{Telegrammer::VERSION}",
447
- 'Accept' => 'application/json'
448
- )
449
-
450
- ApiResponse.new(response)
461
+ begin
462
+ response = @connection.post(
463
+ "#{API_ENDPOINT}/#{api_uri}",
464
+ validated_params,
465
+ 'User-Agent' => "Telegrammer/#{Telegrammer::VERSION}",
466
+ 'Accept' => 'application/json'
467
+ )
468
+
469
+ ApiResponse.new(response,@fail_silently)
470
+ rescue HTTPClient::ReceiveTimeoutError => e
471
+ if !@fail_silently
472
+ fail Telegrammer::Errors::TimeoutError, e.to_s
473
+ end
474
+ end
451
475
  end
452
476
 
453
477
  def send_something(object_kind, params, extra_params_validation = {})
@@ -1,3 +1,3 @@
1
1
  module Telegrammer
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegrammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luis Mayoral
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-16 00:00:00.000000000 Z
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient