Class: Telegem::API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/api/client.rb

Constant Summary collapse

BASE_URL =
'https://api.telegram.org'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, **options) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
# File 'lib/api/client.rb', line 11

def initialize(token, **options)
  @token = token
  @logger = options[:logger] || Logger.new($stdout)
  @timeout = options[:timeout] || 30
  @retries = options[:retries] || 3
  @retry_delay = options[:retry_delay] || 1  # seconds
  
  @endpoint = Async::HTTP::Endpoint.parse(BASE_URL, timeout: @timeout)
  @client = Async::HTTP::Client.new(@endpoint)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/api/client.rb', line 9

def logger
  @logger
end

#tokenObject (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



22
23
24
25
26
# File 'lib/api/client.rb', line 22

def call(method, params = {})
  with_retry do
    make_request(method, params)
  end
end

#call!(method, params = {}, &callback) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/api/client.rb', line 28

def call!(method, params = {}, &callback)
  return unless callback
  begin
    result = call(method, params)
    callback.call(result, nil)
  rescue => error
    callback.call(nil, error)
  end
end

#closeObject



88
89
90
# File 'lib/api/client.rb', line 88

def close
  @client.close
end

#download(file_id, destination_path = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/api/client.rb', line 57

def download(file_id, destination_path = nil)
  with_retry do
    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
end

#get_updates(offset: nil, timeout: 30, limit: 100, allowed_updates: nil) ⇒ Object



81
82
83
84
85
86
# File 'lib/api/client.rb', line 81

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/api/client.rb', line 38

def upload(method, params)
  with_retry do
    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
end