<commit_before>--- Lua library for accessing the Twitter REST API v1.1
--
-- @module  luatwit
-- @license MIT
local assert, error, next, pairs, select, setmetatable, table_concat, tostring, type, unpack =
      assert, error, next, pairs, select, setmetatable, table.concat, tostring, type, unpack
local oauth = require "OAuth"
local json = require "cjson"
local util = require "luatwit.util"
local multipart = require("OAuth.helpers").multipart

local _M = {}

--- API resource data.
-- @see luatwit.resources
_M.resources = require "luatwit.resources"

--- API object definitions.
-- @see luatwit.objects
_M.objects = require "luatwit.objects"

--- JSON null value.
_M.null = json.null

--- Class prototype that implements the API calls.
-- Methods are created on demand from the definitions in the `resources` table.
-- @type api
_M.api = util.new()

local function build_args_str(rules)
    local res = {}
    for name, _ in pairs(rules) do
        res[#res + 1] = name
    end
    return table_concat(res, ", ")
end

local function build_required_str(rules)
    local res = {}
    for name, req in pairs(rules) do
        if req then
            res[#res + 1] = name
        end
    end
    return table_concat(res, ", ")
end

local scalar_types = { string = true, number = true, boolean = true }

-- Checks if the arguments in a table match the rules.
local function check_args(args, rules, res_name)
    if type(args) ~= "table" then
        return res_name .. ": arguments must be passed in a table"
    end
    if not rules then return nil end
    -- check for valid args (names starting with _ are ignored)
    for name, val in pairs(args) do
        if type(name) ~= "string" then
            return res_name .. ": keys must be strings"
        end
        local rule = rules[name]
        if rule == nil and name:sub(1, 1) ~= "_" then
            return res_name .. ": invalid argument '" .. name .. "' not in (" .. build_args_str(rules) .. ")"
        end
        local rule_type = type(rule)
        local allowed_type
        if rule_type == "boolean" then
            allowed_type = scalar_types
        elseif rule_type == "table" and #rule == 2 then
            allowed_type = {}
            allowed_type[rule[2]] = true
        else
            return res_name .. ": invalid rule for field '" .. name .. "'"
        end
        if not allowed_type[type(val)] then
            return res_name .. ": argument '" .. name .. "' must be of type (" .. build_args_str(allowed_type) .. ")"
        end
    end
    -- check if required args are present
    for name, rule in pairs(rules) do
        local required = rule
        if type(rule) == "table" then
            required = rule[1]
        end
        if required and args[name] == nil then
            return res_name .. ": missing required argument '" .. name .. "' in (" .. build_required_str(rules) .. ")"
        end
    end
    return nil
end
<commit_msg>fix handling of custom arguments<commit_after>--- Lua library for accessing the Twitter REST API v1.1
--
-- @module  luatwit
-- @license MIT
local assert, error, next, pairs, select, setmetatable, table_concat, tostring, type, unpack =
      assert, error, next, pairs, select, setmetatable, table.concat, tostring, type, unpack
local oauth = require "OAuth"
local json = require "cjson"
local util = require "luatwit.util"
local multipart = require("OAuth.helpers").multipart

local _M = {}

--- API resource data.
-- @see luatwit.resources
_M.resources = require "luatwit.resources"

--- API object definitions.
-- @see luatwit.objects
_M.objects = require "luatwit.objects"

--- JSON null value.
_M.null = json.null

--- Class prototype that implements the API calls.
-- Methods are created on demand from the definitions in the `resources` table.
-- @type api
_M.api = util.new()

local function build_args_str(rules)
    local res = {}
    for name, _ in pairs(rules) do
        res[#res + 1] = name
    end
    return table_concat(res, ", ")
end

local function build_required_str(rules)
    local res = {}
    for name, req in pairs(rules) do
        if req then
            res[#res + 1] = name
        end
    end
    return table_concat(res, ", ")
end

local scalar_types = { string = true, number = true, boolean = true }

-- Checks if the arguments in a table match the rules.
local function check_args(args, rules, res_name)
    if type(args) ~= "table" then
        return res_name .. ": arguments must be passed in a table"
    end
    if not rules then return nil end
    -- check for valid args (names starting with _ are ignored)
    for name, val in pairs(args) do
        if type(name) ~= "string" then
            return res_name .. ": keys must be strings"
        end
        if name:sub(1, 1) ~= "_" then
            local rule = rules[name]
            if rule == nil then
                return res_name .. ": invalid argument '" .. name .. "' not in (" .. build_args_str(rules) .. ")"
            end
            local rule_type = type(rule)
            local allowed_types
            if rule_type == "boolean" then
                allowed_types = scalar_types
            elseif rule_type == "table" and #rule == 2 then
                allowed_types = {}
                allowed_types[rule[2]] = true
            else
                return res_name .. ": invalid rule for field '" .. name .. "'"
            end
            if not allowed_types[type(val)] then
                return res_name .. ": argument '" .. name .. "' must be of type (" .. build_args_str(allowed_types) .. ")"
            end
        end
    end
    -- check if required args are present
    for name, rule in pairs(rules) do
        local required = rule
        if type(rule) == "table" then
            required = rule[1]
        end
        if required and args[name] == nil then
            return res_name .. ": missing required argument '" .. name .. "' in (" .. build_required_str(rules) .. ")"
        end
    end
    return nil
end

{'fraction_non_alphanumeric': 0.1097907949790795, 'fraction_numerical': 0.0030125523012552303, 'mean_word_length': 2.1838039424613744, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '58820', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1716, 'n_words': 626}