module Gameball::Utils

Public Instance Methods

extractAttributesToHash(body) click to toggle source
# File lib/gameball/utils/helper.rb, line 21
def extractAttributesToHash(body) # Helper method that extracts the attributes from the body and pass it to hashing function
  playerUniqueId = body[:playerUniqueId]
  amount = body[:amount]
  transactionTime = body[:transactionTime]
  begin
    body[:transactionTime] = transactionTime.iso8601
  rescue NoMethodError => exception
    raise Gameball::GameballError.new("Invalid Date Formate, Please use Date and Time objects")
  end

  body["hash"] = Gameball::Utils::hashBody(playerUniqueId: playerUniqueId, amount: (amount || ""), transactionTime: (transactionTime || ""))
  body
end
hashBody(playerUniqueId:, transactionTime: "", amount: "") click to toggle source
# File lib/gameball/utils/helper.rb, line 5
def hashBody(playerUniqueId:, transactionTime: "", amount: "") # Helper method that hashes the attributes that are sent using SHA1 algorithm
  # Check if transaction Key is provided else raise Error
  if !Gameball.transaction_key
    raise Gameball::GameballError.new("Please provide transaction_key, try Gameball::transaction_key='your_key'") # Raise exception
  else
    if transactionTime == ""
      formatted_time = ""
    else
      formatted_time = transactionTime.strftime("%y%m%d%H%M%S")
    end
  end

  str = playerUniqueId + ":" + formatted_time + ":" + amount.to_s + ":" + Gameball.transaction_key
  return Digest::SHA1.hexdigest (str)
end
request(verb, path, body = {}) click to toggle source
# File lib/gameball/utils/request.rb, line 5
def request(verb, path, body = {})
  # check for api_version and key and throw error if not included
  if !Gameball.api_key
    raise Gameball::GameballError.new("Please provide the api_key before making a request, try Gameball::api_key='your_key'")
  end
  uri = URI(Gameball.api_base + "/api" + "/" + Gameball.api_version + path)

  https = Net::HTTP.new(uri.host, uri.port)
  https.max_retries = Gameball.max_retries
  https.read_timeout = Gameball.read_timeout
  https.keep_alive_timeout = Gameball.keep_alive_timeout
  https.use_ssl = true

  case verb.downcase
  when "post"
    req = Net::HTTP::Post.new(uri.path, initheader = { "Content-Type" => "application/json" })
  when "get"
    req = Net::HTTP::Get.new(uri.path, initheader = { "Content-Type" => "application/json" })
  when "put"
    req = Net::HTTP::Put.new(uri.path, initheader = { "Content-Type" => "application/json" })
  when "delete"
    req = Net::HTTP::Delete.new(uri.path, initheader = { "Content-Type" => "application/json" })
  else
    raise Gameball::GameballException.new("Please provide a valid HTTP Verb") # will later throw an exception
  end
  if body != {}
    req.body = body.to_json
  end
  req["APIKey"] = Gameball.api_key
  res = https.request(req)
  return res
end
request_async(verb, path, body = {}) click to toggle source
# File lib/gameball/utils/request.rb, line 38
def request_async(verb, path, body = {})
  #check for api_version and key and throw exceptions
  if !Gameball.api_key
    raise Gameball::GameballError.new("Please provide the api_key before making a request, try Gameball::api_key='your_key'")
  end
  uri = URI(Gameball.api_base + "/api" + "/" + Gameball.api_version + path)

  https = Net::HTTP.new(uri.host, uri.port)
  https.max_retries = Gameball.max_retries
  https.read_timeout = Gameball.read_timeout
  https.keep_alive_timeout = Gameball.keep_alive_timeout
  https.use_ssl = true

  case verb.downcase
  when "post"
    req = Net::HTTP::Post.new(uri.path, initheader = { "Content-Type" => "application/json" })
  when "get"
    req = Net::HTTP::Get.new(uri.path, initheader = { "Content-Type" => "application/json" })
  when "put"
    req = Net::HTTP::Put.new(uri.path, initheader = { "Content-Type" => "application/json" })
  when "delete"
    req = Net::HTTP::Delete.new(uri.path, initheader = { "Content-Type" => "application/json" })
  else
    puts "Please Provide a valid verb" # will later throw an exception
  end
  if body != {}
    req.body = body.to_json
  end
  req["APIKey"] = Gameball.api_key
  Thread.new do
    res = https.request(req)
    return res
  end
end
validate(hash, required, optional) click to toggle source
# File lib/gameball/utils/validation.rb, line 5
def validate(hash, required, optional) # Method used to validate body parameters by checking optional fields and required fields
  errors = []
  # Loop on required fields and throw error if field is not in ruby hash
  required.each do |val|
    raise Gameball::GameballError.new("Required key: #{val.to_sym.inspect}") unless hash.has_key?(val.to_sym)
  end
  allowed = optional + required
  # Loop on each field in ruby hash and check if it is allowed
  hash.each_key do |val|
    unless allowed.include?(val.to_s)
      raise Gameball::GameballError.new("Unknown key: #{val.to_sym.inspect}. Valid keys are: #{allowed.map(&:inspect).join(", ")}")
    end
  end
end