1
0
Fork 0

Remove unnecessary code

This commit is contained in:
Alex Kotov 2018-11-30 00:43:29 +05:00
parent a4b08d98c3
commit 43e4368fcd
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
7 changed files with 1 additions and 237 deletions

View File

@ -8,8 +8,6 @@ class TelegramBotUpdatesController < ApplicationController
def create
logger.info params.inspect
handle_message params[:message] if params[:message]
render status: :no_content, json: {}
end
@ -22,21 +20,4 @@ private
def verify_telegram_bot_secret
raise NotAuthorizedError unless params[:secret] == @telegram_bot.secret
end
def handle_message(message)
handle_user message[:from] if message[:from]
end
def handle_user(user)
telegram_user =
TelegramUser.find_or_initialize_by remote_telegram_id: user[:id]
telegram_user.is_bot = user[:is_bot]
telegram_user.first_name = user[:first_name]
telegram_user.last_name = user[:last_name]
telegram_user.username = user[:username]
telegram_user.language_code = user[:language_code]
telegram_user.save!
end
end

View File

@ -1,12 +0,0 @@
# frozen_string_literal: true
class TelegramUser < ApplicationRecord
validates :remote_telegram_id, presence: true
validates :first_name, presence: true
before_validation do
self.last_name = nil if last_name.blank?
self.username = nil if username.blank?
self.language_code = nil if language_code.blank?
end
end

View File

@ -1,17 +0,0 @@
# frozen_string_literal: true
class CreateTelegramUsers < ActiveRecord::Migration[5.2]
def change
create_table :telegram_users do |t|
t.timestamps null: false
t.integer :remote_telegram_id, null: false
t.boolean :is_bot, null: false
t.string :first_name, null: false
t.string :last_name
t.string :username
t.string :language_code
t.index :remote_telegram_id, unique: true
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_11_29_155235) do
ActiveRecord::Schema.define(version: 2018_11_29_141112) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -37,16 +37,4 @@ ActiveRecord::Schema.define(version: 2018_11_29_155235) do
t.string "api_token", null: false
end
create_table "telegram_users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "remote_telegram_id", null: false
t.boolean "is_bot", null: false
t.string "first_name", null: false
t.string "last_name"
t.string "username"
t.string "language_code"
t.index ["remote_telegram_id"], name: "index_telegram_users_on_remote_telegram_id", unique: true
end
end

View File

@ -1,12 +0,0 @@
# frozen_string_literal: true
FactoryBot.define do
factory :telegram_user do
remote_telegram_id { rand 1..1_000_000 }
is_bot { [false, true].sample }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
username { Faker::Internet.username }
language_code { I18n.available_locales.sample.to_s }
end
end

View File

@ -1,67 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe TelegramUser, type: :model do
subject { create :telegram_user }
it { is_expected.to validate_presence_of :remote_telegram_id }
it { is_expected.to validate_presence_of :first_name }
it { is_expected.not_to validate_presence_of :last_name }
it { is_expected.not_to validate_presence_of :username }
it { is_expected.not_to validate_presence_of :language_code }
describe '#last_name' do
context 'when it is empty' do
subject { create :telegram_user, last_name: '' }
specify do
expect(subject.last_name).to eq nil
end
end
context 'when it is blank' do
subject { create :telegram_user, last_name: ' ' }
specify do
expect(subject.last_name).to eq nil
end
end
end
describe '#username' do
context 'when it is empty' do
subject { create :telegram_user, username: '' }
specify do
expect(subject.username).to eq nil
end
end
context 'when it is blank' do
subject { create :telegram_user, username: ' ' }
specify do
expect(subject.username).to eq nil
end
end
end
describe '#language_code' do
context 'when it is empty' do
subject { create :telegram_user, language_code: '' }
specify do
expect(subject.language_code).to eq nil
end
end
context 'when it is blank' do
subject { create :telegram_user, language_code: ' ' }
specify do
expect(subject.language_code).to eq nil
end
end
end
end

View File

@ -40,101 +40,4 @@ RSpec.describe 'POST /telegram_bot_updates' do
expect(response).to have_http_status :unauthorized
end
end
context 'when message "from" attribute is set' do
def make_request
post '/telegram_bot_updates',
params: { telegram_bot_id: telegram_bot.id,
secret: telegram_bot.secret,
message: { from: telegram_user_attributes } }
end
let :telegram_user_attributes do
{
id: remote_telegram_id,
is_bot: is_bot,
first_name: first_name,
last_name: last_name,
username: username,
language_code: language_code,
}
end
let(:remote_telegram_id) { rand 1..1_000_000 }
let(:is_bot) { [false, true].sample }
let(:first_name) { Faker::Name.first_name }
let(:last_name) { Faker::Name.last_name }
let(:username) { Faker::Internet.username }
let(:language_code) { I18n.available_locales.sample.to_s }
specify do
expect { make_request }.to change(TelegramUser, :count).from(0).to(1)
end
context 'after request' do
before { make_request }
specify do
expect(TelegramUser.last).to have_attributes(
remote_telegram_id: remote_telegram_id,
is_bot: is_bot,
first_name: first_name,
last_name: last_name,
username: username,
language_code: language_code,
)
end
end
end
context 'when message "from" attribute is set and ' \
'Telegram user already exist' do
let! :telegram_user do
create :telegram_user, remote_telegram_id: remote_telegram_id
end
def make_request
post '/telegram_bot_updates',
params: { telegram_bot_id: telegram_bot.id,
secret: telegram_bot.secret,
message: { from: telegram_user_attributes } }
end
let :telegram_user_attributes do
{
id: remote_telegram_id,
is_bot: is_bot,
first_name: first_name,
last_name: last_name,
username: username,
language_code: language_code,
}
end
let(:remote_telegram_id) { rand 1..1_000_000 }
let(:is_bot) { [false, true].sample }
let(:first_name) { Faker::Name.first_name }
let(:last_name) { Faker::Name.last_name }
let(:username) { Faker::Internet.username }
let(:language_code) { I18n.available_locales.sample.to_s }
specify do
expect { make_request }.not_to change(TelegramUser, :count).from(1)
end
context 'after request' do
before { make_request }
specify do
expect(TelegramUser.last).to have_attributes(
remote_telegram_id: remote_telegram_id,
is_bot: is_bot,
first_name: first_name,
last_name: last_name,
username: username,
language_code: language_code,
)
end
end
end
end