Sha256: fa9cae7685bb2022be27b4f09c8defac5eee8896035bb401e8da272963518fd4

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

# This module holds all utilities related to the /subaccounts endpoint.
module Sms77::Subaccounts
  module Action
    CREATE = 'create'
    DELETE = 'delete'
    READ = 'read'
    TRANSFER_CREDITS = 'transfer_credits'
    UPDATE = 'update'
  end

  module Validator
    def self.is_action?(str)
      Sms77::Util::in_module_constants?(str, Sms77::Subaccounts::Action)
    end

    def self.validate(params)
      action = params[:action]

      case action
      when Sms77::Subaccounts::Action::CREATE
        raise 'Parameter validation failed' unless Sms77::Subaccounts::Validator::create(params)
      when Sms77::Subaccounts::Action::DELETE
        raise 'Parameter validation failed' unless Sms77::Subaccounts::Validator::delete(params)
      when Sms77::Subaccounts::Action::TRANSFER_CREDITS
        raise 'Parameter validation failed' unless Sms77::Subaccounts::Validator::transfer_credits(params)
      when Sms77::Subaccounts::Action::UPDATE
        raise 'Parameter validation failed' unless Sms77::Subaccounts::Validator::update(params)
      else
        raise "Unknown action #{action}" unless Sms77::Subaccounts::Validator::is_action?(action)
      end
    end

    def self.create(params)
      Sms77::Util::lengthy_string?(params[:email]) &&
        Sms77::Util::lengthy_string?(params[:name])
    end

    def self.delete(params)
      Sms77::Util::is_positive_integer?(params[:id])
    end

    def self.transfer_credits(params)
      Sms77::Util::is_positive_integer?(params[:amount]) &&
        Sms77::Util::is_positive_integer?(params[:id])
    end

    def self.update(params)
      Sms77::Util::is_positive_integer?(params[:amount]) &&
        Sms77::Util::is_positive_integer?(params[:id]) &&
        Sms77::Util::is_positive_integer?(params[:threshold])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sms77-0.5.0 lib/sms77/subaccounts.rb