зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-07 11:25:52 +03:00
Encodes nested hashes in request body as json
Этот коммит содержится в:
@@ -28,6 +28,13 @@ module Telegram
|
|||||||
def typed_response!
|
def typed_response!
|
||||||
prepend TypedResponse
|
prepend TypedResponse
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Encodes nested hashes as json.
|
||||||
|
def prepare_body(body)
|
||||||
|
body.each do |k, val|
|
||||||
|
body[k] = val.to_json if val.is_a?(Hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :client, :token, :username, :base_uri
|
attr_reader :client, :token, :username, :base_uri
|
||||||
@@ -47,8 +54,8 @@ module Telegram
|
|||||||
client.debug_dev = nil
|
client.debug_dev = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def request(action, data = {})
|
def request(action, body = {})
|
||||||
res = http_request("#{base_uri}#{action}", data)
|
res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
||||||
status = res.status
|
status = res.status
|
||||||
return JSON.parse(res.body) if 300 > status
|
return JSON.parse(res.body) if 300 > status
|
||||||
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
||||||
|
|||||||
@@ -44,4 +44,23 @@ RSpec.describe Telegram::Bot::Client do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.prepare_body' do
|
||||||
|
subject { described_class.prepare_body(input) }
|
||||||
|
|
||||||
|
context 'when plain hash is given' do
|
||||||
|
let(:input) { {a: 1, b: '2', c: [1, 2]} }
|
||||||
|
it { should eq input }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when nested hash is given' do
|
||||||
|
let(:input) { {a: 1, b: '2', c: [1, 2], d: {a: 1}, e: {b: []}} }
|
||||||
|
|
||||||
|
it 'encodes nested hashes to json' do
|
||||||
|
expected = input.dup
|
||||||
|
%i(d e).each { |x| expected[x] = expected[x].to_json }
|
||||||
|
should eq expected
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user