Class: Telegem::API::Client
- Inherits:
-
Object
- Object
- Telegem::API::Client
- Defined in:
- lib/api/client.rb
Constant Summary collapse
- BASE_URL =
'https://api.telegram.org'
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #call(method, params = {}) ⇒ Object
- #call!(method, params = {}, &callback) ⇒ Object
- #close ⇒ Object
- #download(file_id, destination_path = nil) ⇒ Object
- #get_updates(offset: nil, timeout: 30, limit: 100, allowed_updates: nil) ⇒ Object
-
#initialize(token, **options) ⇒ Client
constructor
A new instance of Client.
- #upload(method, params) ⇒ Object
Constructor Details
#initialize(token, **options) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 |
# File 'lib/api/client.rb', line 11 def initialize(token, **) @token = token @logger = [:logger] || Logger.new($stdout) @timeout = [:timeout] || 30 @endpoint = Async::HTTP::Endpoint.parse(BASE_URL) @client = Async::HTTP::Client.new(@endpoint) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/api/client.rb', line 9 def logger @logger end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/api/client.rb', line 9 def token @token end |
Instance Method Details
#call(method, params = {}) ⇒ Object
20 21 22 |
# File 'lib/api/client.rb', line 20 def call(method, params = {}) make_request(method, params) end |
#call!(method, params = {}, &callback) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/api/client.rb', line 24 def call!(method, params = {}, &callback) return unless callback begin result = make_request(method, params) callback.call(result, nil) rescue => error callback.call(nil, error) end end |
#close ⇒ Object
80 81 82 |
# File 'lib/api/client.rb', line 80 def close @client.close end |
#download(file_id, destination_path = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/api/client.rb', line 51 def download(file_id, destination_path = nil) file_info = call('getFile', file_id: file_id) return nil unless file_info && file_info['file_path'] file_path = file_info['file_path'] download_url = "/file/bot#{@token}/#{file_path}" response = @client.get(download_url) if response.status == 200 content = response.read if destination_path File.binwrite(destination_path, content) destination_path else content end else raise NetworkError.new("Download failed: HTTP #{response.status}") end end |
#get_updates(offset: nil, timeout: 30, limit: 100, allowed_updates: nil) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/api/client.rb', line 73 def get_updates(offset: nil, timeout: 30, limit: 100, allowed_updates: nil) params = { timeout: timeout, limit: limit } params[:offset] = offset if offset params[:allowed_updates] = allowed_updates if allowed_updates call('getUpdates', params) end |
#upload(method, params) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/api/client.rb', line 33 def upload(method, params) url = "/bot#{@token}/#{method}" body = Async::HTTP::Body::Multipart.new params.each do |key, value| if file_object?(value) body.add(key.to_s, value, filename: File.basename(value)) else body.add(key.to_s, value.to_s) end end response = @client.post(url, {}, body) handle_response(response) end |