Skip to content

Commit

Permalink
Check attributes types (#295)
Browse files Browse the repository at this point in the history
* Small improvements for Types specs, especially variables naming

* Remake `Object.const_get` into `described_class.const_get` with `false` argument

* Dump more data from Telegram API docs

About attribute type and required value (if there is such).

* Add specs for attributes Types (and required data)

Avoid `default` value for now as a work-around.

* Populate `type_attributes` YAML file for specs

* Add `constrained(eql: '...')` alongside to `default` where it's necessary

* Allow `Integer` as `chat_id` for some places alongside with `String`

* Require `True` value instead of `Boolean`

It's still questionable for me, but so says the Telegram API docs.

It seems like there should be `true` value or no value at all.

I'm not sure if they accept `false` value. But they don't response, as I checked.

* Fix stupid error with `Integer` instead of `Bool`

* Fix a small error with `Array.of(String)` instead of `(Types::String)`

* Resolve attributes types specs with the new changes

---------

Co-authored-by: Alexander Tipugin <[email protected]>
  • Loading branch information
AlexWayfer and atipugin authored Dec 31, 2023
1 parent e58bdd7 commit f47aa75
Show file tree
Hide file tree
Showing 52 changed files with 1,098 additions and 79 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ Metrics/BlockLength:
Metrics/ClassLength:
Exclude:
- lib/telegram/bot/api/endpoints.rb
Metrics/MethodLength:
Exclude:
- spec/**/*

Layout/LineLength:
Max: 120

RSpec/MultipleMemoizedHelpers:
Enabled: false
RSpec/NestedGroups:
Enabled: false
7 changes: 5 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ task :dump_type_attributes do
cells = el.children.select { |c| c.name == 'td' }
{
'name' => cells[0].text,
'required' => !cells[2].text.start_with?('Optional.')
}
'type' => cells[1].text,
'required' => !cells[2].text.start_with?('Optional.'),
'required_value' =>
cells[2].text.match(/^.+, (?:must be (?<found_type>\w+)|always “(?<found_type>\w+)”)$/)&.[](:found_type)
}.compact
end

[type, attributes]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class BotCommandScopeAllChatAdministrators < Base
attribute :type, Types::String.default('all_chat_administrators')
attribute :type, Types::String.constrained(eql: 'all_chat_administrators').default('all_chat_administrators')
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class BotCommandScopeAllGroupChats < Base
attribute :type, Types::String.default('all_group_chats')
attribute :type, Types::String.constrained(eql: 'all_group_chats').default('all_group_chats')
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class BotCommandScopeAllPrivateChats < Base
attribute :type, Types::String.default('all_private_chats')
attribute :type, Types::String.constrained(eql: 'all_private_chats').default('all_private_chats')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/telegram/bot/types/bot_command_scope_chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Telegram
module Bot
module Types
class BotCommandScopeChat < Base
attribute :type, Types::String.default('chat')
attribute :chat_id, Types::String
attribute :type, Types::String.constrained(eql: 'chat').default('chat')
attribute :chat_id, Types::Integer | Types::String
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Telegram
module Bot
module Types
class BotCommandScopeChatAdministrators < Base
attribute :type, Types::String.default('chat_administrators')
attribute :chat_id, Types::String
attribute :type, Types::String.constrained(eql: 'chat_administrators').default('chat_administrators')
attribute :chat_id, Types::Integer | Types::String
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/telegram/bot/types/bot_command_scope_chat_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Telegram
module Bot
module Types
class BotCommandScopeChatMember < Base
attribute :type, Types::String.default('chat_member')
attribute :chat_id, Types::String
attribute :type, Types::String.constrained(eql: 'chat_member').default('chat_member')
attribute :chat_id, Types::Integer | Types::String
attribute :user_id, Types::Integer
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/bot_command_scope_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class BotCommandScopeDefault < Base
attribute :type, Types::String.default('default')
attribute :type, Types::String.constrained(eql: 'default').default('default')
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/telegram/bot/types/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ class Chat < Base
attribute? :username, Types::String
attribute? :first_name, Types::String
attribute? :last_name, Types::String
attribute? :is_forum, Types::Bool
attribute? :is_forum, Types::True
attribute? :photo, ChatPhoto
attribute? :active_usernames, Types::Array.of(Types::String)
attribute? :emoji_status_custom_emoji_id, Types::String
attribute? :emoji_status_expiration_date, Types::Integer
attribute? :bio, Types::String
attribute? :has_private_forwards, Types::Bool
attribute? :has_restricted_voice_and_video_messages, Types::Bool
attribute? :join_to_send_messages, Types::Bool
attribute? :join_by_request, Types::Bool
attribute? :has_private_forwards, Types::True
attribute? :has_restricted_voice_and_video_messages, Types::True
attribute? :join_to_send_messages, Types::True
attribute? :join_by_request, Types::True
attribute? :description, Types::String
attribute? :invite_link, Types::String
attribute? :pinned_message, Message
attribute? :permissions, ChatPermissions
attribute? :slow_mode_delay, Types::Integer
attribute? :message_auto_delete_time, Types::Integer
attribute? :has_aggressive_anti_spam_enabled, Types::Bool
attribute? :has_hidden_members, Types::Bool
attribute? :has_protected_content, Types::Bool
attribute? :has_aggressive_anti_spam_enabled, Types::True
attribute? :has_hidden_members, Types::True
attribute? :has_protected_content, Types::True
attribute? :sticker_set_name, Types::String
attribute? :can_set_sticker_set, Types::Bool
attribute? :can_set_sticker_set, Types::True
attribute? :linked_chat_id, Types::Integer
attribute? :location, ChatLocation
end
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/force_reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class ForceReply < Base
attribute :force_reply, Types::Bool
attribute :force_reply, Types::True
attribute? :input_field_placeholder, Types::String
attribute? :selective, Types::Bool.default(false)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultArticle < Base
attribute :type, Types::String.default('article')
attribute :type, Types::String.constrained(eql: 'article').default('article')
attribute :id, Types::String
attribute :title, Types::String
attribute :input_message_content, InputMessageContent
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultAudio < Base
attribute :type, Types::String.default('audio')
attribute :type, Types::String.constrained(eql: 'audio').default('audio')
attribute :id, Types::String
attribute :audio_url, Types::String
attribute :title, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_cached_audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedAudio < Base
attribute :type, Types::String.default('audio')
attribute :type, Types::String.constrained(eql: 'audio').default('audio')
attribute :id, Types::String
attribute :audio_file_id, Types::String
attribute? :caption, Types::String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedDocument < Base
attribute :type, Types::String.default('document')
attribute :type, Types::String.constrained(eql: 'document').default('document')
attribute :id, Types::String
attribute :title, Types::String
attribute :document_file_id, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_cached_gif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedGif < Base
attribute :type, Types::String.default('gif')
attribute :type, Types::String.constrained(eql: 'gif').default('gif')
attribute :id, Types::String
attribute :gif_file_id, Types::String
attribute? :title, Types::String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedMpeg4Gif < Base
attribute :type, Types::String.default('mpeg4_gif')
attribute :type, Types::String.constrained(eql: 'mpeg4_gif').default('mpeg4_gif')
attribute :id, Types::String
attribute :mpeg4_file_id, Types::String
attribute? :title, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_cached_photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedPhoto < Base
attribute :type, Types::String.default('photo')
attribute :type, Types::String.constrained(eql: 'photo').default('photo')
attribute :id, Types::String
attribute :photo_file_id, Types::String
attribute? :title, Types::String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedSticker < Base
attribute :type, Types::String.default('sticker')
attribute :type, Types::String.constrained(eql: 'sticker').default('sticker')
attribute :id, Types::String
attribute :sticker_file_id, Types::String
attribute? :reply_markup, InlineKeyboardMarkup
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_cached_video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedVideo < Base
attribute :type, Types::String.default('video')
attribute :type, Types::String.constrained(eql: 'video').default('video')
attribute :id, Types::String
attribute :video_file_id, Types::String
attribute :title, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_cached_voice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultCachedVoice < Base
attribute :type, Types::String.default('voice')
attribute :type, Types::String.constrained(eql: 'voice').default('voice')
attribute :id, Types::String
attribute :voice_file_id, Types::String
attribute :title, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultContact < Base
attribute :type, Types::String.default('contact')
attribute :type, Types::String.constrained(eql: 'contact').default('contact')
attribute :id, Types::String
attribute :phone_number, Types::String
attribute :first_name, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultDocument < Base
attribute :type, Types::String.default('document')
attribute :type, Types::String.constrained(eql: 'document').default('document')
attribute :id, Types::String
attribute :title, Types::String
attribute? :caption, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultGame < Base
attribute :type, Types::String.default('game')
attribute :type, Types::String.constrained(eql: 'game').default('game')
attribute :id, Types::String
attribute :game_short_name, Types::String
attribute? :reply_markup, InlineKeyboardMarkup
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_gif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultGif < Base
attribute :type, Types::String.default('gif')
attribute :type, Types::String.constrained(eql: 'gif').default('gif')
attribute :id, Types::String
attribute :gif_url, Types::String
attribute? :gif_width, Types::Integer
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultLocation < Base
attribute :type, Types::String.default('location')
attribute :type, Types::String.constrained(eql: 'location').default('location')
attribute :id, Types::String
attribute :latitude, Types::Float
attribute :longitude, Types::Float
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_mpeg4_gif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultMpeg4Gif < Base
attribute :type, Types::String.default('mpeg4_gif')
attribute :type, Types::String.constrained(eql: 'mpeg4_gif').default('mpeg4_gif')
attribute :id, Types::String
attribute :mpeg4_url, Types::String
attribute? :mpeg4_width, Types::Integer
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultPhoto < Base
attribute :type, Types::String.default('photo')
attribute :type, Types::String.constrained(eql: 'photo').default('photo')
attribute :id, Types::String
attribute :photo_url, Types::String
attribute :thumbnail_url, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_venue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultVenue < Base
attribute :type, Types::String.default('venue')
attribute :type, Types::String.constrained(eql: 'venue').default('venue')
attribute :id, Types::String
attribute :latitude, Types::Float
attribute :longitude, Types::Float
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultVideo < Base
attribute :type, Types::String.default('video')
attribute :type, Types::String.constrained(eql: 'video').default('video')
attribute :id, Types::String
attribute :video_url, Types::String
attribute :mime_type, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/inline_query_result_voice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InlineQueryResultVoice < Base
attribute :type, Types::String.default('voice')
attribute :type, Types::String.constrained(eql: 'voice').default('voice')
attribute :id, Types::String
attribute :voice_url, Types::String
attribute :title, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/input_media_animation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InputMediaAnimation < Base
attribute :type, Types::String.default('animation')
attribute :type, Types::String.constrained(eql: 'animation').default('animation')
attribute :media, Types::String
attribute? :thumbnail, Types::String
attribute? :caption, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/input_media_audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InputMediaAudio < Base
attribute :type, Types::String.default('audio')
attribute :type, Types::String.constrained(eql: 'audio').default('audio')
attribute :media, Types::String
attribute? :thumbnail, Types::String
attribute? :caption, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/input_media_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InputMediaDocument < Base
attribute :type, Types::String.default('document')
attribute :type, Types::String.constrained(eql: 'document').default('document')
attribute :media, Types::String
attribute? :thumbnail, Types::String
attribute? :caption, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/input_media_photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InputMediaPhoto < Base
attribute :type, Types::String.default('photo')
attribute :type, Types::String.constrained(eql: 'photo').default('photo')
attribute :media, Types::String
attribute? :caption, Types::String
attribute? :parse_mode, Types::String
Expand Down
2 changes: 1 addition & 1 deletion lib/telegram/bot/types/input_media_video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Telegram
module Bot
module Types
class InputMediaVideo < Base
attribute :type, Types::String.default('video')
attribute :type, Types::String.constrained(eql: 'video').default('video')
attribute :media, Types::String
attribute? :thumbnail, Types::String
attribute? :caption, Types::String
Expand Down
Loading

0 comments on commit f47aa75

Please sign in to comment.