From 1963533e33d99a38cf59fd6fd705c5bf9e168645 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Thu, 24 Mar 2016 23:57:14 +0300 Subject: [PATCH] Encode arrays as json in requests --- lib/telegram/bot/client.rb | 3 ++- spec/telegram/bot/client_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index ae058b3..d9a2872 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -34,8 +34,9 @@ module Telegram # Encodes nested hashes as json. def prepare_body(body) + body = body.dup body.each do |k, val| - body[k] = val.to_json if val.is_a?(Hash) + body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array) end end end diff --git a/spec/telegram/bot/client_spec.rb b/spec/telegram/bot/client_spec.rb index d967d4b..e9e27f8 100644 --- a/spec/telegram/bot/client_spec.rb +++ b/spec/telegram/bot/client_spec.rb @@ -60,7 +60,7 @@ RSpec.describe Telegram::Bot::Client do subject { described_class.prepare_body(input) } context 'when plain hash is given' do - let(:input) { {a: 1, b: '2', c: [1, 2]} } + let(:input) { {a: 1, b: '2', c: nil} } it { should eq input } end @@ -69,7 +69,7 @@ RSpec.describe Telegram::Bot::Client do it 'encodes nested hashes to json' do expected = input.dup - %i(d e).each { |x| expected[x] = expected[x].to_json } + %i(c d e).each { |x| expected[x] = expected[x].to_json } should eq expected end end