module TextMagic::API::Response

Public Instance Methods

account(hash) click to toggle source
# File lib/textmagic/response.rb, line 11
def account(hash)
  response = OpenStruct.new(hash)
  response.balance = response.balance.to_f
  response.balance = response.balance.to_i if (response.balance % 1).zero?
  response
end
check_number(hash, single) click to toggle source
# File lib/textmagic/response.rb, line 66
def check_number(hash, single)
  response = {}
  hash.each do |phone, check_hash|
    response[phone] = OpenStruct.new(check_hash)
  end
  single ? response.values.first : response
end
message_status(hash, single) click to toggle source
# File lib/textmagic/response.rb, line 32
def message_status(hash, single)
  response = {}
  hash.each do |message_id, message_hash|
    status = message_hash["status"].dup
    metaclass = class << status; self; end
    metaclass.send :attr_accessor, :text, :credits_cost, :reply_number, :status, :created_time, :completed_time
    status.text = message_hash["text"]
    status.credits_cost = message_hash["credits_cost"]
    status.reply_number = message_hash["reply_number"]
    status.status = message_hash["status"]
    status.created_time = Time.at(message_hash["created_time"].to_i) if message_hash["created_time"]
    status.completed_time = Time.at(message_hash["completed_time"].to_i) if message_hash["completed_time"]
    response[message_id] = status
  end
  single ? response.values.first : response
end
receive(hash) click to toggle source
# File lib/textmagic/response.rb, line 49
def receive(hash)
  response = hash["messages"].collect do |message_hash|
    message = "#{message_hash["from"]}: #{message_hash["text"]}"
    metaclass = class << message; self; end
    metaclass.send :attr_accessor, :timestamp, :message_id, :text, :from
    message.text = message_hash["text"]
    message.from = message_hash["from"]
    message.message_id = message_hash["message_id"]
    message.timestamp = Time.at(message_hash["timestamp"].to_i)
    message
  end
  metaclass = class << response; self; end
  metaclass.send :attr_accessor, :unread
  response.unread = hash["unread"]
  response
end
send(hash, single) click to toggle source
# File lib/textmagic/response.rb, line 18
def send(hash, single)
  response = if single
    hash["message_id"].keys.first.dup
  else
    hash["message_id"].invert
  end
  metaclass = class << response; self; end
  metaclass.send :attr_accessor, :sent_text, :parts_count, :message_id
  response.sent_text = hash["sent_text"]
  response.parts_count = hash["parts_count"]
  response.message_id = hash["message_id"]
  response
end