diff --git a/.gitignore b/.gitignore index f927afa..b18192f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /.bundle/ /.yardoc /Gemfile.lock +/gemfiles/*.lock /_yardoc/ /coverage/ /doc/ diff --git a/.rubocop.yml b/.rubocop.yml index 3cebf67..3364cd5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,30 +1,52 @@ Rails: {Enabled: true} -Style/Alias: {Enabled: false} -Style/AlignParameters: +Layout/AlignParameters: # Disable, till rubocop supports combination of styles. # Use one of this styles where appropriate, keep it clean, compact and readable. Enabled: false # EnforcedStyle: # - with_first_parameter # - with_fixed_indentation + +# Breaks +# +# I18n.t(key, +# param: val, +# # ... +# ) +Layout/ClosingParenthesisIndentation: {Enabled: false} +Layout/DotPosition: {EnforcedStyle: trailing} +Layout/FirstParameterIndentation: {EnforcedStyle: consistent} +# Same as Layout/ClosingParenthesisIndentation +Layout/MultilineMethodCallBraceLayout: {Enabled: false} +Layout/MultilineMethodCallIndentation: {EnforcedStyle: indented} +Layout/MultilineOperationIndentation: {EnforcedStyle: indented} +Layout/SpaceInsideHashLiteralBraces: {EnforcedStyle: no_space} + +# Offences named scopes and `expect {}.to change {}`. +Lint/AmbiguousBlockAssociation: {Enabled: false} + +Naming/PredicateName: {Enabled: false} +Naming/VariableNumber: {EnforcedStyle: snake_case} + +Style/Alias: {Enabled: false} Style/AndOr: {EnforcedStyle: conditionals} -Style/ClosingParenthesisIndentation: {Enabled: false} Style/Documentation: {Enabled: false} -Style/DotPosition: {EnforcedStyle: trailing} -Style/FirstParameterIndentation: {EnforcedStyle: consistent} Style/IfUnlessModifier: {Enabled: false} + +# Consistent to other definitions. +Style/EmptyMethod: {EnforcedStyle: expanded} + Style/ModuleFunction: {Enabled: false} -Style/MultilineMethodCallIndentation: {EnforcedStyle: indented} -Style/MultilineOperationIndentation: {EnforcedStyle: indented} Style/NestedParenthesizedCalls: {Enabled: false} -Style/PredicateName: {Enabled: false} Style/SignalException: {EnforcedStyle: only_raise} -Style/SpaceInsideHashLiteralBraces: {EnforcedStyle: no_space} Style/TrailingCommaInArguments: {Enabled: false} Style/TrailingCommaInLiteral: {EnforcedStyleForMultiline: comma} Metrics/AbcSize: {Max: 21} +# Other metrics are just enough. +# This one offences all specs, routes and some initializers. +Metrics/BlockLength: {Enabled: false} Metrics/LineLength: {Max: 100} Metrics/MethodLength: {Max: 30} Metrics/CyclomaticComplexity: {Max: 8} diff --git a/.travis.yml b/.travis.yml index a6dcec5..4ac3c53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,9 @@ language: ruby cache: bundler rvm: - 2.2.3 -env: - - RAILS=4 - - RAILS=5 - - RAILS=5_1 +gemfile: + - gemfiles/rails_42.gemfile + - gemfiles/rails_50.gemfile + - gemfiles/rails_51.gemfile notifications: email: false diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..2a17e94 --- /dev/null +++ b/Appraisals @@ -0,0 +1,14 @@ +appraise 'rails-51' do + gem 'actionpack', '~> 5.1' + gem 'railties', '~> 5.1' +end + +appraise 'rails-50' do + gem 'actionpack', '~> 5.0' + gem 'railties', '~> 5.0' +end + +appraise 'rails-42' do + gem 'actionpack', '~> 4.2' + gem 'railties', '~> 4.2' +end diff --git a/Gemfile b/Gemfile index b2ef288..db13674 100644 --- a/Gemfile +++ b/Gemfile @@ -2,24 +2,11 @@ source 'https://rubygems.org' gemspec group :development do - case ENV['RAILS'] - when '5_1' - gem 'railties', '5.1.0' - gem 'actionpack', '5.1.0' - when '5' - gem 'railties', '5.0.2' - gem 'actionpack', '5.0.2' - when '4' - gem 'railties', '~> 4.2' - gem 'actionpack', '~> 4.2' - else - gem 'railties' - gem 'actionpack' - end + gem 'appraisal', '~> 2.2' - gem 'sdoc', '~> 0.4.1' gem 'pry', '~> 0.10.1' gem 'pry-byebug', '~> 3.2.0' + gem 'sdoc', '~> 0.4.1' gem 'telegram-bot-types', '~> 0.3.0' @@ -27,7 +14,7 @@ group :development do gem 'rspec-its', '~> 1.1.0' gem 'rspec-rails', '~> 3.5.0' - gem 'rubocop', '~> 0.37.0' + gem 'rubocop', '~> 0.51.0' gem 'coveralls', '~> 0.8.2', require: false end diff --git a/README.md b/README.md index cd95aff..9ce6f35 100644 --- a/README.md +++ b/README.md @@ -507,7 +507,7 @@ Yes, it's threadsafe too. ## Development After checking out the repo, run `bin/setup` to install dependencies. -Then, run `rake spec` to run the tests. +Then, run `appraisal rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. @@ -515,16 +515,6 @@ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). -### Different Rails versions - -To setup development for specific major Rails version use: - -``` -RAILS=5 bundle install -# or -RAILS=5 bundle update -``` - ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/telegram-bot-rb/telegram-bot. diff --git a/bin/setup b/bin/setup index fe07d3b..b8ec1ce 100755 --- a/bin/setup +++ b/bin/setup @@ -3,6 +3,7 @@ set -euo pipefail IFS=$'\n\t' bundle install +appraisal install bin/install_git_hooks # Do any other automated setup that you need to do here diff --git a/gemfiles/rails_42.gemfile b/gemfiles/rails_42.gemfile new file mode 100644 index 0000000..6a5d7d1 --- /dev/null +++ b/gemfiles/rails_42.gemfile @@ -0,0 +1,20 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "actionpack", "~> 4.2" +gem "railties", "~> 4.2" + +group :development do + gem "pry", "~> 0.10.1" + gem "pry-byebug", "~> 3.2.0" + gem "sdoc", "~> 0.4.1" + gem "telegram-bot-types", "~> 0.3.0" + gem "rspec", "~> 3.5.0" + gem "rspec-its", "~> 1.1.0" + gem "rspec-rails", "~> 3.5.0" + gem "rubocop", "~> 0.51.0" + gem "coveralls", "~> 0.8.2", require: false +end + +gemspec path: "../" diff --git a/gemfiles/rails_50.gemfile b/gemfiles/rails_50.gemfile new file mode 100644 index 0000000..e944c26 --- /dev/null +++ b/gemfiles/rails_50.gemfile @@ -0,0 +1,20 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "actionpack", "~> 5.0" +gem "railties", "~> 5.0" + +group :development do + gem "pry", "~> 0.10.1" + gem "pry-byebug", "~> 3.2.0" + gem "sdoc", "~> 0.4.1" + gem "telegram-bot-types", "~> 0.3.0" + gem "rspec", "~> 3.5.0" + gem "rspec-its", "~> 1.1.0" + gem "rspec-rails", "~> 3.5.0" + gem "rubocop", "~> 0.51.0" + gem "coveralls", "~> 0.8.2", require: false +end + +gemspec path: "../" diff --git a/gemfiles/rails_51.gemfile b/gemfiles/rails_51.gemfile new file mode 100644 index 0000000..3af389a --- /dev/null +++ b/gemfiles/rails_51.gemfile @@ -0,0 +1,20 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "actionpack", "~> 5.1" +gem "railties", "~> 5.1" + +group :development do + gem "pry", "~> 0.10.1" + gem "pry-byebug", "~> 3.2.0" + gem "sdoc", "~> 0.4.1" + gem "telegram-bot-types", "~> 0.3.0" + gem "rspec", "~> 3.5.0" + gem "rspec-its", "~> 1.1.0" + gem "rspec-rails", "~> 3.5.0" + gem "rubocop", "~> 0.51.0" + gem "coveralls", "~> 0.8.2", require: false +end + +gemspec path: "../" diff --git a/lib/telegram/bot/botan.rb b/lib/telegram/bot/botan.rb index 75b3697..b135e6c 100644 --- a/lib/telegram/bot/botan.rb +++ b/lib/telegram/bot/botan.rb @@ -35,7 +35,7 @@ module Telegram def request(method, uri, query = {}, body = nil) res = http_request(method, uri, query.merge(token: token), body) status = res.status - return JSON.parse(res.body) if 300 > status + return JSON.parse(res.body) if status < 300 result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier err_msg = "#{res.reason}: #{result && result['info'] || '-'}" raise Error, err_msg diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index a8006ea..4220625 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -49,7 +49,7 @@ module Telegram def request(action, body = {}) res = http_request("#{base_uri}#{action}", self.class.prepare_body(body)) status = res.status - return JSON.parse(res.body) if 300 > status + return JSON.parse(res.body) if status < 300 result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier err_msg = result && result['description'] || '-' if result @@ -63,7 +63,7 @@ module Telegram end # Splited to the sections similar to API docs. - %w( + %w[ deleteWebhook getUpdates getWebhookInfo @@ -124,7 +124,7 @@ module Telegram getGameHighScores sendGame setGameScore - ).each do |method| + ].each do |method| define_method(method.underscore) { |*args| request(method, *args) } end diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index a633e69..5f3509c 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -78,7 +78,7 @@ module Telegram autoload :TypedUpdate, 'telegram/bot/updates_controller/typed_update' - PAYLOAD_TYPES = %w( + PAYLOAD_TYPES = %w[ message edited_message channel_post @@ -88,7 +88,7 @@ module Telegram callback_query shipping_query pre_checkout_query - ).freeze + ].freeze CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i CONFLICT_CMD_REGEX = Regexp.new("^(#{PAYLOAD_TYPES.join('|')}|\\d)") diff --git a/lib/telegram/bot/updates_controller/instrumentation.rb b/lib/telegram/bot/updates_controller/instrumentation.rb index 99988bf..a2d4ce8 100644 --- a/lib/telegram/bot/updates_controller/instrumentation.rb +++ b/lib/telegram/bot/updates_controller/instrumentation.rb @@ -39,7 +39,7 @@ module Telegram Instrumentation.instrument(:respond_with, type: type) { super } end - %i(answer_callback_query answer_inline_query).each do |type| + %i[answer_callback_query answer_inline_query].each do |type| define_method(type) do |*args| Instrumentation.instrument(:respond_with, type: type) { super(*args) } end diff --git a/lib/telegram/bot/updates_controller/log_subscriber.rb b/lib/telegram/bot/updates_controller/log_subscriber.rb index eb7be10..60fe07f 100644 --- a/lib/telegram/bot/updates_controller/log_subscriber.rb +++ b/lib/telegram/bot/updates_controller/log_subscriber.rb @@ -17,7 +17,7 @@ module Telegram payload = event.payload additions = UpdatesController.log_process_action(payload) message = "Completed in #{event.duration.round}ms" - message << " (#{additions.join(' | ')})" unless additions.blank? + message << " (#{additions.join(' | ')})" if additions.present? message end end diff --git a/lib/telegram/bot/updates_controller/testing.rb b/lib/telegram/bot/updates_controller/testing.rb index 8441aae..dd58d80 100644 --- a/lib/telegram/bot/updates_controller/testing.rb +++ b/lib/telegram/bot/updates_controller/testing.rb @@ -2,7 +2,7 @@ module Telegram module Bot class UpdatesController module Testing - IVARS_TO_KEEP = %i(@_session).freeze + IVARS_TO_KEEP = %i[@_session].freeze # Perform multiple dispatches on same instance. def dispatch_again(bot = nil, update = nil) diff --git a/lib/telegram/bot/updates_poller.rb b/lib/telegram/bot/updates_poller.rb index 8794980..5980c0f 100644 --- a/lib/telegram/bot/updates_poller.rb +++ b/lib/telegram/bot/updates_poller.rb @@ -50,7 +50,7 @@ module Telegram end rescue Interrupt @running = false - rescue => e + rescue StandardError => e logger.error { ([e.message] + e.backtrace).join("\n") } if logger end end diff --git a/spec/integration_helper.rb b/spec/integration_helper.rb index b625597..908eb0c 100644 --- a/spec/integration_helper.rb +++ b/spec/integration_helper.rb @@ -27,7 +27,7 @@ end Rails.application.initialize! # # Controllers -%w(default other named).each do |bot_name| +%w[default other named].each do |bot_name| controller = Class.new(Telegram::Bot::UpdatesController) do define_method :start do |*| respond_with :message, text: "from #{bot_name}" diff --git a/spec/telegram/bot/client_spec.rb b/spec/telegram/bot/client_spec.rb index caf9c63..396fee6 100644 --- a/spec/telegram/bot/client_spec.rb +++ b/spec/telegram/bot/client_spec.rb @@ -19,7 +19,7 @@ RSpec.describe Telegram::Bot::Client do it 'encodes nested hashes to json' do expected = input.dup - %i(c 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 @@ -35,7 +35,7 @@ RSpec.describe Telegram::Bot::Client do subject { described_class.new(*args) } context 'when multiple args are given' do - let(:args) { %w(secret superbot) } + let(:args) { %w[secret superbot] } its(:token) { should eq args[0] } its(:username) { should eq args[1] } its(:base_uri) { should include args[0] } diff --git a/spec/telegram/bot/client_stub_spec.rb b/spec/telegram/bot/client_stub_spec.rb index 2fe12cd..bdfe47b 100644 --- a/spec/telegram/bot/client_stub_spec.rb +++ b/spec/telegram/bot/client_stub_spec.rb @@ -45,7 +45,7 @@ RSpec.describe Telegram::Bot::ClientStub do end context 'when username and token are given' do - let(:args) { %w(token superbot) } + let(:args) { %w[token superbot] } its(:token) { should eq args[0] } its(:username) { should eq args[1] } end diff --git a/spec/telegram/bot/rspec/integration_spec.rb b/spec/telegram/bot/rspec/integration_spec.rb index c456ef1..8caa9bc 100644 --- a/spec/telegram/bot/rspec/integration_spec.rb +++ b/spec/telegram/bot/rspec/integration_spec.rb @@ -5,7 +5,8 @@ require 'action_dispatch/testing/integration' RSpec.describe 'Integrations helper', :telegram_bot do include ActionDispatch::Integration::Runner - def reset_template_assertion; end + def reset_template_assertion + end let(:app) do app = Telegram::Bot::Middleware.new(bot, controller) @@ -38,7 +39,7 @@ RSpec.describe 'Integrations helper', :telegram_bot do describe '#dispatch_message' do subject { -> { dispatch_message "/start #{args.join ' '}", options } } - let(:args) { %w(asd qwe) } + let(:args) { %w[asd qwe] } let(:options) { {} } it { should respond_with_message "Start: #{args.inspect}, option: " } @@ -59,7 +60,7 @@ RSpec.describe 'Integrations helper', :telegram_bot do it { should respond_with_message "Start: #{args.inspect}, option: " } context 'with args' do - let(:args) { %w(asd qwe) } + let(:args) { %w[asd qwe] } it { should respond_with_message "Start: #{args.inspect}, option: " } end diff --git a/spec/telegram/bot/updates_controller/message_context_spec.rb b/spec/telegram/bot/updates_controller/message_context_spec.rb index 6a3bdc0..ff0248a 100644 --- a/spec/telegram/bot/updates_controller/message_context_spec.rb +++ b/spec/telegram/bot/updates_controller/message_context_spec.rb @@ -65,7 +65,7 @@ RSpec.describe Telegram::Bot::UpdatesController::MessageContext do context 'when message has new command' do let(:text) { '/action a s d' } - its(:call) { should eq [:action_result, *%w(a s d)] } + its(:call) { should eq [:action_result, 'a', 's', 'd'] } it { should change { session[:context] }.to nil } end end diff --git a/spec/telegram/bot/updates_controller/typed_update_spec.rb b/spec/telegram/bot/updates_controller/typed_update_spec.rb index 2bca1d6..70e3a40 100644 --- a/spec/telegram/bot/updates_controller/typed_update_spec.rb +++ b/spec/telegram/bot/updates_controller/typed_update_spec.rb @@ -9,11 +9,11 @@ RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do context 'when `update` is a virtus model' do subject { controller } - %w( + %w[ message inline_query chosen_inline_result - ).each do |type| + ].each do |type| context "with #{type}" do type_class = Telegram::Bot::Types.const_get(type.camelize) let(:payload_type) { type } diff --git a/spec/telegram/bot/updates_controller_spec.rb b/spec/telegram/bot/updates_controller_spec.rb index 0491866..4389dfb 100644 --- a/spec/telegram/bot/updates_controller_spec.rb +++ b/spec/telegram/bot/updates_controller_spec.rb @@ -107,7 +107,7 @@ RSpec.describe Telegram::Bot::UpdatesController do it { should eq [false, :unsupported_payload_type, []] } end - %w(message channel_post).each do |type| + %w[message channel_post].each do |type| context "when payload is edited_#{type}" do let(:payload_type) { "edited_#{type}" } it { should eq [false, payload_type, [payload]] } @@ -123,11 +123,11 @@ RSpec.describe Telegram::Bot::UpdatesController do context 'with command' do let(:text) { "/test#{"@#{mention}" if mention} arg 1 2" } let(:mention) {} - it { should eq [true, 'test', %w(arg 1 2)] } + it { should eq [true, 'test', %w[arg 1 2]] } context 'with mention' do let(:mention) { bot.username } - it { should eq [true, 'test', %w(arg 1 2)] } + it { should eq [true, 'test', %w[arg 1 2]] } end context 'with mention for other bot' do @@ -143,7 +143,7 @@ RSpec.describe Telegram::Bot::UpdatesController do end end - custom_payload_types = %w( + custom_payload_types = %w[ message edited_message channel_post @@ -151,7 +151,7 @@ RSpec.describe Telegram::Bot::UpdatesController do inline_query chosen_inline_result callback_query - ) + ] (described_class::PAYLOAD_TYPES - custom_payload_types).each do |type| context "when payload is #{type}" do let(:payload_type) { type } @@ -163,11 +163,11 @@ RSpec.describe Telegram::Bot::UpdatesController do context 'when `update` is a virtus model' do subject { controller } let(:update) { Telegram::Bot::Types::Update.new(super()) } - %w( + %w[ message inline_query chosen_inline_result - ).each do |type| + ].each do |type| context "with #{type}" do type_class = Telegram::Bot::Types.const_get(type.camelize) let(:payload_type) { type } @@ -194,7 +194,7 @@ RSpec.describe Telegram::Bot::UpdatesController do describe '#process' do subject { -> { controller.process(:action, *args) } } - let(:args) { [:arg1, :arg2] } + let(:args) { %i[arg1 arg2] } let(:controller_class) do Class.new(described_class) do attr_reader :acted, :hooked diff --git a/telegram-bot.gemspec b/telegram-bot.gemspec index 09ea73c..427a3f7 100644 --- a/telegram-bot.gemspec +++ b/telegram-bot.gemspec @@ -1,4 +1,3 @@ -# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'telegram/bot/version' @@ -20,9 +19,10 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '~> 2.0' - spec.add_dependency 'activesupport', '>= 4.0', '< 5.2' spec.add_dependency 'actionpack', '>= 4.0', '< 5.2' + spec.add_dependency 'activesupport', '>= 4.0', '< 5.2' spec.add_dependency 'httpclient', '~> 2.7' + spec.add_development_dependency 'bundler', '~> 1.11' spec.add_development_dependency 'rake', '~> 10.0' end