class FantasticRobot::Request::Base

Attributes

method[R]

Public Class Methods

new(attributes = {}) click to toggle source

Initializer that assigns the fields received

Calls superclass method
# File lib/fantastic_robot/request/base.rb, line 12
def initialize(attributes = {})
  super(attributes)
end

Public Instance Methods

attributes() click to toggle source

Attribute map for ActiveModel::Serialization.

# File lib/fantastic_robot/request/base.rb, line 17
def attributes
  Hash[instance_variables.map{|attrib| [attrib.to_s[1..attrib.to_s.size], nil]}]
end
send_request() click to toggle source

Send request to Telegram API

# File lib/fantastic_robot/request/base.rb, line 27
def send_request
  raise ArgumentError, "Method name not defined" if (self.method.blank?)

  payload = self.to_h
  payload.delete(:method)

  payload.each do |key, value|
    payload[key] = Faraday::UploadIO.new(payload[key], MIME::Types.type_for(payload[key].path).first.content_type) if (value.is_a?(File))
  end

  FantasticRobot.connection.api_call self.method, payload
end
to_h() click to toggle source

Proxy to get a serialized version of the object.

# File lib/fantastic_robot/request/base.rb, line 22
def to_h
  recursive_serialization(self)
end

Private Instance Methods

recursive_serialization(object) click to toggle source

Method to try to recursively seralize the objects received

# File lib/fantastic_robot/request/base.rb, line 43
def recursive_serialization object
  if (object.is_a?(Array))
    # If it's an array, try to serialize each element
    return object.map{|o| recursive_serialization(o)}
  elsif (object.is_a?(Hash))
    # It's a hash, convert each key to sym and try to serialize each value
    return Hash[object.map{|k,v| [k.to_sym, recursive_serialization(v)]}]
  elsif (object.respond_to?(:serializable_hash))
    # If it can be seralized, serialize and try to recursively convert it's attributes
    return recursive_serialization(object.serializable_hash)
  else
    return object
  end
end