Skip to content

Commit

Permalink
Merge pull request #23 from dot-black/fix/missed-min-api-version
Browse files Browse the repository at this point in the history
Fix/missed min api version
  • Loading branch information
vikdotdev authored Aug 22, 2023
2 parents b8556f6 + 1f7581b commit e7d3ecd
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
### Deprecated
- None

## v0.3.6

### Fixed
- `Response` instance now includes required `min_api_version` in the root of request params taken from inputs.

## v0.3.5

### Added
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
viberroo (0.3.5)
viberroo (0.3.6)
recursive-open-struct (~> 1.1.1)

GEM
Expand Down
26 changes: 25 additions & 1 deletion lib/viberroo/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ def remove_webhook
# @see https://developers.viber.com/docs/api/rest-bot-api/#keyboards
#
def send_message(message, keyboard: {})
request(URL::MESSAGE, { receiver: @response.user_id }.merge(message, keyboard))
request(
URL::MESSAGE,
{ receiver: @response.user_id }.merge(message, keyboard).tap do |hash|
hash.merge!(min_api_version_hash(keyboard)) unless hash[:min_api_version]
end
)
end

# @deprecated Use {#send_message} instead.
Expand Down Expand Up @@ -249,5 +254,24 @@ def compact(params)
def caller_name
caller[1][/`.*'/][1..-2]
end

##
# @!visibility private
#
# @note Iterates each keyboard input to identify the maximum required :min_api_version param.
#
# @return [Hash || Empty Hash]
#
def min_api_version_hash(keyboard)
buttons_array = keyboard.dig(:keyboard, :Buttons)

return {} unless buttons_array

min_api_version = buttons_array.map { |btn| btn[:min_api_version] }.compact.max

return {} unless min_api_version

{ min_api_version: min_api_version }
end
end
end
2 changes: 1 addition & 1 deletion lib/viberroo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Viberroo
VERSION = '0.3.5'.freeze
VERSION = '0.3.6'.freeze
end
84 changes: 84 additions & 0 deletions spec/bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,90 @@
let(:response) { Viberroo::Response.new(event: 'message', sender: { id: '01234=' }) }
let(:bot) { Viberroo::Bot.new(token: token, response: response) }

describe '#send_message' do
subject { bot.send_message(message, keyboard: keyboard) }

let(:message) do
{
text: 'hello',
event: 'message',
sender: { id: '1234=' },
receiver: response.user_id
}
end

context 'when keyboard does not contain inputs' do
let(:keyboard) { {} }

it 'does not contain :min_api_version attr in the params root' do
expect(bot).to receive(:request).with(
Viberroo::URL::MESSAGE,
{ receiver: response.user_id }.merge(message)
)
subject
end
end

context 'when keyboard contain single input with api version' do
let(:button) { Viberroo::Input.share_phone_button({}) }
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button]) }
let(:min_api_version) { button[:min_api_version] }

it 'sets correct :min_api_version attr in the params root' do
expect(bot).to receive(:request).with(
Viberroo::URL::MESSAGE,
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
)
subject
end
end

context 'when keyboard contain single input with api version but min_api-version was set before' do
let(:min_api_version) { 7 }
let(:message) { { min_api_version: min_api_version } }
let(:button) { Viberroo::Input.share_phone_button({}) }
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button]) }

it 'sets correct :min_api_version attr in the params root' do
expect(bot).to receive(:request).with(
Viberroo::URL::MESSAGE,
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
)
subject
end
end

context 'when keyboard contain multiple inputs one of which has no api versions' do
let(:button_one) { Viberroo::Input.share_phone_button({}) }
let(:button_two) { Viberroo::Input.reply_button({}) }
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button_one, button_two]) }
let(:min_api_version) { button_one[:min_api_version] }

it 'sets correct :min_api_version attr in the params root' do
expect(bot).to receive(:request).with(
Viberroo::URL::MESSAGE,
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
)
subject
end
end

context 'when keyboard contain multiple inputs with different api versions' do
let(:button_one) { Viberroo::Input.share_phone_button({ min_api_version: 3 }) }
let(:button_two) { Viberroo::Input.reply_button({ min_api_version: 7 }) }
let(:keyboard) { Viberroo::Input.keyboard(Buttons: [button_one, button_two]) }
let(:min_api_version) { button_two[:min_api_version] }

it 'sets correct :min_api_version attr in the params root' do
expect(bot).to receive(:request).with(
Viberroo::URL::MESSAGE,
{ receiver: response.user_id }.merge(message, keyboard, min_api_version: min_api_version)
)
subject
end
end
end

describe 'setting a webhook' do
let(:bot) { Viberroo::Bot.new(token: token) }
let!(:body) do
Expand Down

0 comments on commit e7d3ecd

Please sign in to comment.