Class: ChatbotHelper::Telegram::BaseResource
- Inherits:
-
Object
- Object
- ChatbotHelper::Telegram::BaseResource
- Defined in:
- lib/chatbot_helper/telegram/base_resource.rb
Overview
A base resource class for Telegram bot API resources.
Direct Known Subclasses
Animation, Audio, CallbackGame, CallbackQuery, Chat, ChatMember, ChosenInlineResult, CollectionResource, Contact, Document, File, ForceReply, Game, InlineKeyboardButton, InlineKeyboardMarkup, InlineQuery, KeyboardButton, Location, Message, MessageEntity, PhotoSize, ReplyKeyboardMarkup, ReplyKeyboardRemove, ResponseParameters, Sticker, Update, User, UserProfilePhotos, Venue, Video, Voice
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Class Method Summary collapse
-
.optional_arrays ⇒ Array<Hash>
Returns an array of hashes which represent the optional arrays for this resource.
-
.optional_fields ⇒ Array<String>
Returns an array of words which represent the optional fields for this resource.
-
.optional_objects ⇒ Array<Hash>
Returns an array of hashes which represent the optional objects for this resource.
-
.required_arrays ⇒ Array<Hash>
Returns an array of hashes which represent the required arrays for this resource.
-
.required_fields ⇒ Array<String>
Returns an array of words which represent the required fields for this resource.
-
.required_objects ⇒ Array<Hash>
Returns an array of hashes which represent the required objects for this resource.
-
.valid_resource!(resource) ⇒ Boolean
Checks a given Hash and returns true iff it is a valid bot Resource as defined in the Telegram bot documentation.
-
.valid_resource?(resource) ⇒ Boolean
Checks a given Hash and returns true iff it is a valid bot Resource as defined in the Telegram bot documentation.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#initialize(hash: nil, string: nil) ⇒ BaseResource
constructor
Initializes a new resource object with the given json String or hash.
- #to_s ⇒ Object (also: #to_json)
Constructor Details
#initialize(hash: nil, string: nil) ⇒ BaseResource
Initializes a new resource object with the given json String or hash.
Either hash
or string
should be set but not both. It is recommended to pass string
instead of an already deserialized hash (hash
) to the initializer.
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 165 def initialize(hash: nil, string: nil) @toolbox = ChatbotHelper::Telegram::Toolbox @hash = hash.nil? ? @toolbox.parse_json(string) : @toolbox.parse_json(@toolbox.generate_json(hash)) # Validate resource self.class.valid_resource!(@hash) # Implement getters implement_field_accessors implement_object_accessors implement_array_accessors end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
155 156 157 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 155 def hash @hash end |
Class Method Details
.optional_arrays ⇒ Array<Hash>
Returns an array of hashes which represent the optional arrays for this resource
The hashes must follow the following structure:
{ name: ‘field_name’, type: ‘ChatbotHelper::Telegram::FieldType’ }
where type
represents the type of the resulting objects in the array, which must be a subclass of ChatbotHelper::Telegram::BaseResource
Arrays are arrays of complex objects. If you have arrays of primitive data types see required_fields and optional_fields.
98 99 100 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 98 def optional_arrays [] end |
.optional_fields ⇒ Array<String>
Returns an array of words which represent the optional fields for this resource
Fields are primitive data types like String
, Integer
, Boolean
and Float
or arrays of primitive data types. If you have more complex types like Object
or arrays of Objects, see required_objects, optional_objects, required_arrays and optional_arrays.
30 31 32 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 30 def optional_fields %w[] end |
.optional_objects ⇒ Array<Hash>
Returns an array of hashes which represent the optional objects for this resource
The hashes must follow the following structure:
{ name: ‘field_name’, type: ‘ChatbotHelper::Telegram::FieldType’ }
where type
represents the type of the resulting object which must be a subclass of ChatbotHelper::Telegram::BaseResource
60 61 62 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 60 def optional_objects [] end |
.required_arrays ⇒ Array<Hash>
Returns an array of hashes which represent the required arrays for this resource
The hashes must follow the following structure:
{ name: ‘field_name’, type: ‘ChatbotHelper::Telegram::FieldType’ }
where type
represents the type of the resulting objects in the array, which must be a subclass of ChatbotHelper::Telegram::BaseResource
Arrays are arrays of complex objects. If you have arrays of primitive data types see required_fields and optional_fields.
79 80 81 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 79 def required_arrays [] end |
.required_fields ⇒ Array<String>
Returns an array of words which represent the required fields for this resource
Fields are primitive data types like String
, Integer
, Boolean
and Float
or arrays of primitive data types. If you have more complex types like Object
or arrays of Objects, see required_objects, optional_objects, required_arrays and optional_arrays.
16 17 18 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 16 def required_fields %w[] end |
.required_objects ⇒ Array<Hash>
Returns an array of hashes which represent the required objects for this resource
The hashes must follow the following structure:
{ name: ‘field_name’, type: ‘ChatbotHelper::Telegram::FieldType’ }
where type
represents the type of the resulting object which must be a subclass of ChatbotHelper::Telegram::BaseResource
45 46 47 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 45 def required_objects [] end |
.valid_resource!(resource) ⇒ Boolean
Checks a given Hash and returns true iff it is a valid bot Resource as defined in the Telegram bot documentation. Raises an exception otherwise.
See core.telegram.org/bots/api for more information about the resource structures.
144 145 146 147 148 149 150 151 152 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 144 def valid_resource!(resource) if valid_resource?(resource) true else m = 'The given resource is not valid or does not follow its '\ 'specification from the Telegram bot API' raise ChatbotHelper::Exceptions::InvalidResource.new(m), m end end |
.valid_resource?(resource) ⇒ Boolean
Checks a given Hash and returns true iff it is a valid bot Resource as defined in the Telegram bot documentation. Returns false otherwise.
See core.telegram.org/bots/api for more information about the resource structures.
rubocop:disable Metrics/MethodLength, Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 112 def valid_resource?(resource) return false if resource.nil? || !resource.respond_to?('[]') required_fields.each do |f| return false if resource[f].nil? end required_objects.each do |o| return false unless o[:type].valid_resource?(resource[o[:name]]) end required_arrays.each do |a| arr = resource[a[:name]] return false unless arr.respond_to?('each') arr.each do |el| return false unless a[:type].valid_resource?(el) end end # All tests passed. Return true... true end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
184 185 186 187 188 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 184 def ==(other) other.is_a?(ChatbotHelper::Telegram::BaseResource) ? @hash == other.hash : @hash == other end |
#to_s ⇒ Object Also known as: to_json
178 179 180 |
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 178 def to_s @toolbox.generate_json(@hash) end |