class Syspass::Method

Public Class Methods

new(prefix, connection) click to toggle source
# File lib/syspass.rb, line 12
def initialize(prefix, connection)
    @prefix = prefix
    @connection = connection
end

Public Instance Methods

method_missing(method_name, *args) click to toggle source
# File lib/syspass.rb, line 17
def method_missing(method_name, *args)
    validate_args(args)
    method  = "#{@prefix}/#{method_name}"
    params  = args[0] || {}

    query(method, @connection.options, params)
end
query(method, options, params) click to toggle source
# File lib/syspass.rb, line 25
def query(method, options, params)
    jsonquery = {
        :jsonrpc    => @@json_rcp_version,
        :method     => method,
        :params     => params.merge!(options),
        :id         => @@json_rcp_request_id,
    }
    sslmode = @connection.url.scheme == 'https' 
    http = Net::HTTP.new(@connection.url.host, @connection.url.port)

    if sslmode
        http.use_ssl = sslmode
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    request = Net::HTTP::Post.new(@connection.url.path, @@headers)
    request.body = jsonquery.to_json
    response = http.request(request)
    json_response = JSON.parse(response.body)

    if json_response['error']
        raise "error handling: #{json_response['error']}"
    end
    json_response['result']
end
validate_args(args) click to toggle source
# File lib/syspass.rb, line 51
def validate_args(args)
    unless args.is_a?(Array)
        raise TypeError, "Wrong argument:  #{args.inspect} (expected an Array)"
    end
end