Module: Onebot::Client::ApiHelper

Included in:
Onebot::Client
Defined in:
lib/onebot/client/api_helper.rb

Constant Summary collapse

METHODS_LIST_FILE =
File.expand_path("../api.json", __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#func_listObject

Returns the value of attribute func_list.



6
7
8
# File 'lib/onebot/client/api_helper.rb', line 6

def func_list
  @func_list
end

Class Method Details

.define_helpers(list) ⇒ Object

Defines method with underscored name to post to specific endpoint:

define_method :send_private_msg


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/onebot/client/api_helper.rb', line 64

def define_helpers(list)
  list.each do |method_name, method_info|
    # puts "define #{method_name}"
    params = parse_param(method_info["params"])
    if method_name[0] == "."
      class_eval %(
        private def #{method_name[1..]} (params = { #{params} })
          request("#{method_name}", params)
        end
      ), __FILE__, __LINE__ - 4
    else
      class_eval %(
        def #{method_name} (params = { #{params} })
          request("#{method_name}", params)
        end
      ), __FILE__, __LINE__ - 4
    end
  end
end

.get_default(str) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/onebot/client/api_helper.rb', line 14

def get_default(str)
  str.sub!(/^`(.+)`$/, '\1')
  return str.to_i if str =~ /^[+-]?\d+$/
  return true if str == "true"
  return false if str == "false"

  m = str.match(/^\s*(\d+)\s*\*\s*(\d+)\s*/)
  return m[1].to_i * m[2].to_i if m

  str
end

.methods_list(file = METHODS_LIST_FILE) ⇒ Object



10
11
12
# File 'lib/onebot/client/api_helper.rb', line 10

def methods_list(file = METHODS_LIST_FILE)
  @func_list = JSON.parse(File.read(file))
end

.parse_param(input) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/onebot/client/api_helper.rb', line 26

def parse_param(input)
  return nil unless input
  output = ""

  params = input.map do |param, extra|
    default = case extra["default"]
              when "çİş" then ""
              when "-" then nil
              when nil then nil
              else get_default extra["default"]
    end

    if default.nil?
      next param
    end

    {"#{param}": default}
  end

  params.each do |param|
    output << if param.instance_of?(Hash)
      if param.values[0].instance_of?(String)
        "#{param.keys[0]}: '#{param.values[0]}'"
      else
        "#{param.keys[0]}: #{param.values[0]}"
      end
    else
      "#{param}:nil"
    end
    output << ","
  end
  output.delete_suffix!(",")
  output.to_s
end