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.



112
113
114
115
116
117
118
119
120
121
# File 'lib/api/client.rb', line 112

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

  @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.



110
111
112
# File 'lib/api/client.rb', line 110

def logger
  @logger
end

#tokenObject (readonly)

Returns the value of attribute token.



110
111
112
# File 'lib/api/client.rb', line 110

def token
  @token
end

Instance Method Details

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



123
124
125
126
127
# File 'lib/api/client.rb', line 123

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

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/api/client.rb', line 129

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



202
203
204
# File 'lib/api/client.rb', line 202

def close
  @client.close
end

#download(file_id, destination_path = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/api/client.rb', line 165

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



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/api/client.rb', line 190

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/api/client.rb', line 140

def upload(method, params)
  with_retry do
    url = "/bot#{@token}/#{method}"

    form = MultipartForm.new

    params.each do |key, value|
      form.add(key.to_s, value)
    end

    body = form.body

    response = @client.post(
      url,
      {
        "content-type" => form.content_type,
        "content-length" => body.bytesize.to_s
      },
      body
    )

    handle_response(response)
  end
end