Class: EurekaBot::Tg::Client
- Inherits:
-
Object
- Object
- EurekaBot::Tg::Client
- Includes:
- Instrumentation
- Defined in:
- lib/eureka_bot/tg/client.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #extract_result(response) ⇒ Object
- #get_file_url(file_id) ⇒ Object
-
#initialize(token:, url: 'https://api.telegram.org') ⇒ Client
constructor
A new instance of Client.
- #make_request(route, method: :get, **rest) ⇒ Object
- #parse_response(response) ⇒ Object
- #resource ⇒ Object
Constructor Details
#initialize(token:, url: 'https://api.telegram.org') ⇒ Client
Returns a new instance of Client.
8 9 10 11 |
# File 'lib/eureka_bot/tg/client.rb', line 8 def initialize(token:, url: 'https://api.telegram.org') @url = url @token = token end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
6 7 8 |
# File 'lib/eureka_bot/tg/client.rb', line 6 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/eureka_bot/tg/client.rb', line 6 def url @url end |
Instance Method Details
#extract_result(response) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/eureka_bot/tg/client.rb', line 22 def extract_result(response) if response['ok'] response['result'] else raise EurekaBot::Tg::Client::Error.new(error_code: response['error_code'], description: response['description']) end end |
#get_file_url(file_id) ⇒ Object
13 14 15 16 |
# File 'lib/eureka_bot/tg/client.rb', line 13 def get_file_url(file_id) file_path = make_request('getFile', payload: {file_id: file_id}.to_json)['file_path'] [url, '/file', '/bot', token, '/', file_path].join end |
#make_request(route, method: :get, **rest) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/eureka_bot/tg/client.rb', line 30 def make_request(route, method: :get, **rest) full_route = Array.wrap(route).join('/') instrument 'eureka-bot.tg.request', route: full_route, method: method, rest: JSON.generate(rest) do res = resource[full_route] = rest.clone payload = .delete(:payload) headers = (res.[:headers] || {}).merge() request = .merge( method: method, url: res.url, headers: headers ) request[:payload] = payload if payload if request[:payload].is_a?(Hash) request[:headers].delete('Content-Type') request[:multipart] = true end response = nil begin response = parse_response( RestClient::Request.execute(request).body ) rescue RestClient::Exception => e EurekaBot.exception_handler(e, e.class, custom_params: {error: e, http_code: e.http_code, http_body: e.http_body}.to_json) response = parse_response(e.http_body) end if response instrument 'eureka-bot.tg.response', response: response extract_result(response) end end end |
#parse_response(response) ⇒ Object
18 19 20 |
# File 'lib/eureka_bot/tg/client.rb', line 18 def parse_response(response) ActiveSupport::JSON.decode(response) if response.present? end |
#resource ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/eureka_bot/tg/client.rb', line 70 def resource @resource ||= RestClient::Resource.new( [ url, ['bot', token].join ].to_param, headers: { 'Content-Type' => 'application/JSON' }, logger: EurekaBot.logger, timeout: 30 ) end |