diff --git a/.travis.yml b/.travis.yml index 0d16897..de50b22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,8 @@ language: ruby cache: bundler rvm: - 2.2.3 +env: + - RAILS=4 + - RAILS=5 notifications: email: false diff --git a/Gemfile b/Gemfile index ca27cba..d88e257 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,13 @@ source 'https://rubygems.org' gemspec +case ENV['RAILS'] +when '5' + gem 'actionpack', '5.0.0.rc1' +when '4' + gem 'actionpack', '~> 4.2' +end + group :development do gem 'sdoc', '~> 0.4.1' gem 'pry', '~> 0.10.1' diff --git a/lib/telegram/bot/middleware.rb b/lib/telegram/bot/middleware.rb index 087069c..70bbd30 100644 --- a/lib/telegram/bot/middleware.rb +++ b/lib/telegram/bot/middleware.rb @@ -1,6 +1,8 @@ require 'active_support/concern' +require 'active_support/core_ext/hash/indifferent_access' +require 'active_support/json' require 'action_dispatch/http/mime_type' -require 'action_dispatch/middleware/params_parser' +require 'action_dispatch/http/request' module Telegram module Bot @@ -13,7 +15,8 @@ module Telegram end def call(env) - update = env['action_dispatch.request.request_parameters'] + request = ActionDispatch::Request.new(env) + update = request.request_parameters controller.dispatch(bot, update) [200, {}, ['']] end diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index 571b701..ae7528d 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -63,7 +63,7 @@ module Telegram include AbstractController::Callbacks # Redefine callbacks with default terminator. - if ActiveSupport.gem_version >= Gem::Version.new('5') + if ActiveSupport::VERSION::MAJOR >= 5 define_callbacks :process_action, skip_after_callbacks_if_terminated: true else diff --git a/spec/telegram/bot/middleware_spec.rb b/spec/telegram/bot/middleware_spec.rb index ab4b605..dff57d6 100644 --- a/spec/telegram/bot/middleware_spec.rb +++ b/spec/telegram/bot/middleware_spec.rb @@ -1,3 +1,5 @@ +require 'rack/mock' + RSpec.describe Telegram::Bot::Middleware do let(:instance) { described_class.new bot, controller } let(:bot) { double(:bot) } @@ -6,10 +8,25 @@ RSpec.describe Telegram::Bot::Middleware do describe '#call' do subject { instance.call(env) } let(:env) { {'action_dispatch.request.request_parameters' => json_body} } - let(:json_body) { double(:json_body) } + let(:update) { {'message' => {'id' => 1}} } + let(:env) do + Rack::MockRequest.env_for('/', + method: :post, + input: JSON.dump(update), + 'CONTENT_TYPE' => 'application/json', + ) + end + + require 'action_pack/version' + if ActionPack::VERSION::MAJOR < 5 + # Before Rails 5, params are parsed in middleware. + # In Rails 5, they are parsed in Request#request_parameters. + require 'action_dispatch/middleware/params_parser' + let(:instance) { ActionDispatch::ParamsParser.new(super()) } + end it 'calls dispatch on controller' do - expect(controller).to receive(:dispatch).with(bot, json_body) + expect(controller).to receive(:dispatch).with(bot, update) subject end diff --git a/spec/telegram/bot/updates_controller_spec.rb b/spec/telegram/bot/updates_controller_spec.rb index f498efd..04e6a5c 100644 --- a/spec/telegram/bot/updates_controller_spec.rb +++ b/spec/telegram/bot/updates_controller_spec.rb @@ -225,12 +225,12 @@ RSpec.describe Telegram::Bot::UpdatesController do it { should change(controller, :acted).to true } its(:call) { should eq [nil, nil, args] } - context 'when callback returns false' do + context 'when callback halts chain' do before do controller_class.prepend(Module.new do def hook super - false + ActiveSupport::VERSION::MAJOR >= 5 ? throw(:abort) : false end end) end