diff --git a/app/controllers/piratas_controller.rb b/app/controllers/piratas_controller.rb index c1df84e..6453e46 100644 --- a/app/controllers/piratas_controller.rb +++ b/app/controllers/piratas_controller.rb @@ -26,7 +26,7 @@ class PiratasController < ApplicationController # # Obtener el perfil propio # - # @return [Hash] { id: @int, nick: @string, email: @string } + # @return [Hash] { id: @int, nick: @string, email: @string, telegram_url: @string } def yo @pirata = current_pirata end diff --git a/app/models/pirata.rb b/app/models/pirata.rb index 5f1a229..91d9a0f 100644 --- a/app/models/pirata.rb +++ b/app/models/pirata.rb @@ -23,7 +23,14 @@ class Pirata < ApplicationRecord before_create :telegram_token! - # Asigna un token a cada pirata + # Devuelve la URL de Telegram + # @return [String] + def telegram_url + 'https://t.me/' + Telegram.bots[:default].username + '?start=' + telegram_token + end + + # Asigna un token a cada pirata si no lo tiene + # @return [TrueClass] Los callbacks tienen que terminar en true def telegram_token! self.telegram_token = SecureRandom.hex unless telegram_token diff --git a/app/views/piratas/_pirata.json.jbuilder b/app/views/piratas/_pirata.json.jbuilder index 9dbae73..7ea5292 100644 --- a/app/views/piratas/_pirata.json.jbuilder +++ b/app/views/piratas/_pirata.json.jbuilder @@ -1,3 +1,3 @@ # frozen_string_literal: true -json.call(pirata, :id, :nick, :email) +json.call(pirata, :id, :nick, :email, :telegram_url) diff --git a/test/models/pirata_test.rb b/test/models/pirata_test.rb index 27e35ba..9c6ce69 100644 --- a/test/models/pirata_test.rb +++ b/test/models/pirata_test.rb @@ -8,4 +8,10 @@ class PirataTest < ActiveSupport::TestCase assert_equal true, pirata.valid? end + + test 'tiene un token de telegram' do + pirate = create :pirata + + assert pirate.telegram_token + end end