Class: Syspass::Method
- Inherits:
-
Object
show all
- Defined in:
- lib/syspass.rb
Constant Summary
collapse
- @@json_rcp_version =
'2.0'
- @@json_rcp_request_id =
1
{ 'Content-Type' => 'application/json-rpc' }
Instance Method Summary
collapse
Constructor Details
#initialize(prefix, connection) ⇒ Method
Returns a new instance of Method.
12
13
14
15
|
# File 'lib/syspass.rb', line 12
def initialize(prefix, connection)
@prefix = prefix
@connection = connection
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
17
18
19
20
21
22
23
|
# 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
|
Instance Method Details
#query(method, options, params) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# 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) ⇒ Object
51
52
53
54
55
|
# 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
|