Class: ChatbotHelper::Telegram::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/chatbot_helper/telegram/base_resource.rb

Overview

A base resource class for Telegram bot API resources.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • hash (Hash) (defaults to: nil)

    A hash which represents this resource as described in the telegram bot api. Defaults to nil.

  • string (String) (defaults to: nil)

    A json String which represents this resource as described in the telegram bot api. Defaults to nil.



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

#hashObject (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_arraysArray<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.

Returns:

  • (Array<Hash>)

    The optional arrays for this resource



98
99
100
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 98

def optional_arrays
  []
end

.optional_fieldsArray<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.

Returns:

  • (Array<String>)

    The optional fields for this resource



30
31
32
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 30

def optional_fields
  %w[]
end

.optional_objectsArray<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

Returns:

  • (Array<Hash>)

    The optional objects for this resource



60
61
62
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 60

def optional_objects
  []
end

.required_arraysArray<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.

Returns:

  • (Array<Hash>)

    The required arrays for this resource



79
80
81
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 79

def required_arrays
  []
end

.required_fieldsArray<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.

Returns:

  • (Array<String>)

    The required fields for this resource



16
17
18
# File 'lib/chatbot_helper/telegram/base_resource.rb', line 16

def required_fields
  %w[]
end

.required_objectsArray<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

Returns:

  • (Array<Hash>)

    The required objects for this resource



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.

Parameters:

  • resource (Hash)

    The resource representing the Telegram bot API resource as a hash with stringified keys

Returns:

  • (Boolean)

    True iff the given resource represents this resource as described in the Telegram bot API.



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

Parameters:

  • resource (Hash)

    The resource representing the Telegram bot API resource as a hash with stringified keys

Returns:

  • (Boolean)

    True iff the given resource represents this resource as described in the Telegram bot API.



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_sObject 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