Skip to content

Commit e7d3ecd

Browse files
authored
Merge pull request #23 from dot-black/fix/missed-min-api-version
Fix/missed min api version
2 parents b8556f6 + 1f7581b commit e7d3ecd

5 files changed

Lines changed: 116 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
2828
### Deprecated
2929
- None
3030

31+
## v0.3.6
32+
33+
### Fixed
34+
- `Response` instance now includes required `min_api_version` in the root of request params taken from inputs.
35+
3136
## v0.3.5
3237

3338
### Added

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
viberroo (0.3.5)
4+
viberroo (0.3.6)
55
recursive-open-struct (~> 1.1.1)
66

77
GEM

lib/viberroo/bot.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ def remove_webhook
141141
# @see https://developers.viber.com/docs/api/rest-bot-api/#keyboards
142142
#
143143
def send_message(message, keyboard: {})
144-
request(URL::MESSAGE, { receiver: @response.user_id }.merge(message, keyboard))
144+
request(
145+
URL::MESSAGE,
146+
{ receiver: @response.user_id }.merge(message, keyboard).tap do |hash|
147+
hash.merge!(min_api_version_hash(keyboard)) unless hash[:min_api_version]
148+
end
149+
)
145150
end
146151

147152
# @deprecated Use {#send_message} instead.
@@ -249,5 +254,24 @@ def compact(params)
249254
def caller_name
250255
caller[1][/`.*'/][1..-2]
251256
end
257+
258+
##
259+
# @!visibility private
260+
#
261+
# @note Iterates each keyboard input to identify the maximum required :min_api_version param.
262+
#
263+
# @return [Hash || Empty Hash]
264+
#
265+
def min_api_version_hash(keyboard)
266+
buttons_array = keyboard.dig(:keyboard, :Buttons)
267+
268+
return {} unless buttons_array
269+
270+
min_api_version = buttons_array.map { |btn| btn[:min_api_version] }.compact.max
271+
272+
return {} unless min_api_version
273+
274+
{ min_api_version: min_api_version }
275+
end
252276
end
253277
end

lib/viberroo/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Viberroo
2-
VERSION = '0.3.5'.freeze
2+
VERSION = '0.3.6'.freeze
33
end

spec/bot_spec.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,90 @@
77
let(:response) { Viberroo::Response.new(event: 'message', sender: { id: '01234=' }) }
88
let(:bot) { Viberroo::Bot.new(token: token, response: response) }
99

10+
describe '#send_message' do
11+
subject { bot.send_message(message, keyboard: keyboard) }
12+
13+
let(:message) do
14+
{
15+
text: 'hello',
16+
event: 'message',
17+
sender: { id: '1234=' },
18+
receiver: response.user_id
19+
}
20+
end
21+
22+
context 'when keyboard does not contain inputs' do
23+
let(:keyboard) { {} }
24+
25+
it 'does not contain :min_api_version attr in the params root' do
26+
expect(bot).to receive(:request).with(
27+
Viberroo::URL::MESSAGE,
28+
{ receiver: response.user_id }.merge(message)
29+
)
30+
subject
31+
end
32+
end
33+
34+
context 'when keyboard contain single input with api version' do
35+
let(:button) { Viberroo::Input.share_phone_button({}) }
36+
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button]) }
37+
let(:min_api_version) { button[:min_api_version] }
38+
39+
it 'sets correct :min_api_version attr in the params root' do
40+
expect(bot).to receive(:request).with(
41+
Viberroo::URL::MESSAGE,
42+
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
43+
)
44+
subject
45+
end
46+
end
47+
48+
context 'when keyboard contain single input with api version but min_api-version was set before' do
49+
let(:min_api_version) { 7 }
50+
let(:message) { { min_api_version: min_api_version } }
51+
let(:button) { Viberroo::Input.share_phone_button({}) }
52+
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button]) }
53+
54+
it 'sets correct :min_api_version attr in the params root' do
55+
expect(bot).to receive(:request).with(
56+
Viberroo::URL::MESSAGE,
57+
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
58+
)
59+
subject
60+
end
61+
end
62+
63+
context 'when keyboard contain multiple inputs one of which has no api versions' do
64+
let(:button_one) { Viberroo::Input.share_phone_button({}) }
65+
let(:button_two) { Viberroo::Input.reply_button({}) }
66+
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button_one, button_two]) }
67+
let(:min_api_version) { button_one[:min_api_version] }
68+
69+
it 'sets correct :min_api_version attr in the params root' do
70+
expect(bot).to receive(:request).with(
71+
Viberroo::URL::MESSAGE,
72+
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
73+
)
74+
subject
75+
end
76+
end
77+
78+
context 'when keyboard contain multiple inputs with different api versions' do
79+
let(:button_one) { Viberroo::Input.share_phone_button({ min_api_version: 3 }) }
80+
let(:button_two) { Viberroo::Input.reply_button({ min_api_version: 7 }) }
81+
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button_one, button_two]) }
82+
let(:min_api_version) { button_two[:min_api_version] }
83+
84+
it 'sets correct :min_api_version attr in the params root' do
85+
expect(bot).to receive(:request).with(
86+
Viberroo::URL::MESSAGE,
87+
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
88+
)
89+
subject
90+
end
91+
end
92+
end
93+
1094
describe 'setting a webhook' do
1195
let(:bot) { Viberroo::Bot.new(token: token) }
1296
let!(:body) do

0 commit comments

Comments
 (0)