ratal 0.1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/ratal.rb +91 -0
- data/lib/ratal/nethttp_provider.rb +24 -0
- data/lib/ratal/request_provider.rb +12 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 1043e9c45d1f92b0d4255cf019347a210b616165
|
|
4
|
+
data.tar.gz: 4408d9af846093e8760e1c2c76f629101867f749
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 19e8bfed7790597bae216048f0eef6a34f7db66ea7cde6f026f984cdbde72803074a15f202d9d10192966e87d65bd6a30dd24e7f936fc654becf31340aadad62
|
|
7
|
+
data.tar.gz: 2c9137df84a62fd4534463bc66f664a0aa808f98ca318d742a54c482a41ce342367cf495e0d1ca39c56bd21ee74ab42741127be322c8815c50f18a937132e3af
|
data/lib/ratal.rb
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require 'ratal/request_provider'
|
|
2
|
+
require 'ratal/nethttp_provider'
|
|
3
|
+
|
|
4
|
+
class Ratal
|
|
5
|
+
USER_AGENT = 'KAMAR/1455 CFNetwork/758.4.3 Darwin/15.5.0'
|
|
6
|
+
CONTENT_TYPE = 'application/x-www-form-urlencoded'
|
|
7
|
+
|
|
8
|
+
@api_base = nil
|
|
9
|
+
@api_target = nil
|
|
10
|
+
@provider = nil
|
|
11
|
+
|
|
12
|
+
def initialize(api_base, provider = NetHTTPProvider.new)
|
|
13
|
+
@api_base = api_base
|
|
14
|
+
@provider = provider
|
|
15
|
+
|
|
16
|
+
api_string = 'api/api.php'
|
|
17
|
+
|
|
18
|
+
@api_target = @api_base
|
|
19
|
+
|
|
20
|
+
# Make sure it starts with http://
|
|
21
|
+
@api_base.prepend 'http://' unless @api_target =~ /^[a-z]+:\/\//
|
|
22
|
+
|
|
23
|
+
# If it already has the api string on it, skip it
|
|
24
|
+
return if @api_target.end_with? api_string
|
|
25
|
+
|
|
26
|
+
# Make sure it ends with '/'
|
|
27
|
+
@api_base.concat '/' unless @api_target.end_with? '/'
|
|
28
|
+
|
|
29
|
+
# Make sure it ends with api/api.php
|
|
30
|
+
@api_base.concat api_string
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def settings
|
|
34
|
+
call_method 'GetSettings', 'ServerSettings', 'vtku'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def connect(username, password)
|
|
38
|
+
call_method 'Logon', 'Logon', 'vtku', Username: username, Password: password
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
attr_reader :api_base
|
|
42
|
+
attr_reader :api_target
|
|
43
|
+
|
|
44
|
+
protected
|
|
45
|
+
def call_method(command, target, key, options = {})
|
|
46
|
+
params = {'Command': command, 'FileName': target, 'Key': key}
|
|
47
|
+
str = call params.merge(options)
|
|
48
|
+
|
|
49
|
+
doc = REXML::Document.new(str)
|
|
50
|
+
|
|
51
|
+
result = process_element(doc.root)
|
|
52
|
+
|
|
53
|
+
result
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def call(data)
|
|
57
|
+
# noinspection RubyStringKeysInHashInspection
|
|
58
|
+
headers = {
|
|
59
|
+
'User-Agent': USER_AGENT,
|
|
60
|
+
'Content-Type': CONTENT_TYPE
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
uri = URI(api_target)
|
|
64
|
+
@provider.request(uri, headers, data)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def process_element(elem)
|
|
68
|
+
result = {}
|
|
69
|
+
|
|
70
|
+
children = elem.elements
|
|
71
|
+
if children.empty?
|
|
72
|
+
return elem.text
|
|
73
|
+
else
|
|
74
|
+
children.each do |e|
|
|
75
|
+
result[symbolize e.name] = process_element(e)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
result
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# http://stackoverflow.com/a/1509939
|
|
83
|
+
def symbolize(str)
|
|
84
|
+
str.gsub(/::/, '/').
|
|
85
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
|
86
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
|
87
|
+
tr('- ', '_').
|
|
88
|
+
downcase.
|
|
89
|
+
to_sym
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class NetHTTPProvider < RequestProvider
|
|
2
|
+
def request(uri, headers, data)
|
|
3
|
+
# Convert the symbol keys to strings for the headers
|
|
4
|
+
headers_mapped = {}
|
|
5
|
+
headers.each { |k, v| headers_mapped[k.to_s] = v }
|
|
6
|
+
|
|
7
|
+
# Convert the symbol keys to strings for the data
|
|
8
|
+
data_mapped = {}
|
|
9
|
+
data.each { |k, v| data_mapped[k.to_s] = v }
|
|
10
|
+
|
|
11
|
+
# Make the connection
|
|
12
|
+
http = Net::HTTP.new(uri.hostname, uri.port)
|
|
13
|
+
|
|
14
|
+
# Build the request
|
|
15
|
+
req = Net::HTTP::Post.new(uri.request_uri, headers_mapped)
|
|
16
|
+
req.set_form_data(data_mapped)
|
|
17
|
+
|
|
18
|
+
# Run the request
|
|
19
|
+
response = http.request(req)
|
|
20
|
+
|
|
21
|
+
# Return the result
|
|
22
|
+
response.body
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class RequestProvider
|
|
2
|
+
METHOD = 'Method not implemented!'
|
|
3
|
+
|
|
4
|
+
# Fire a HTTP request.
|
|
5
|
+
# @param [URI] uri The URI to send the request to
|
|
6
|
+
# @param [Hash] headers The headers to be sent
|
|
7
|
+
# @param [Hash] data The parameters to be used on this request
|
|
8
|
+
# @return [String] The result, as a string, of this request
|
|
9
|
+
def request(uri, headers, data)
|
|
10
|
+
raise METHOD
|
|
11
|
+
end
|
|
12
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ratal
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Campbell Suter
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-03-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A ruby port of Project Katal, an unofficial interface to the undocumented
|
|
14
|
+
Kamar API
|
|
15
|
+
email: znix@znix.xyz
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/ratal.rb
|
|
21
|
+
- lib/ratal/nethttp_provider.rb
|
|
22
|
+
- lib/ratal/request_provider.rb
|
|
23
|
+
homepage: https://gitlab.com/EasywaysNZ/ratal
|
|
24
|
+
licenses:
|
|
25
|
+
- GPL-3.0+
|
|
26
|
+
metadata: {}
|
|
27
|
+
post_install_message:
|
|
28
|
+
rdoc_options: []
|
|
29
|
+
require_paths:
|
|
30
|
+
- lib
|
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '0'
|
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
requirements: []
|
|
42
|
+
rubyforge_project:
|
|
43
|
+
rubygems_version: 2.5.2
|
|
44
|
+
signing_key:
|
|
45
|
+
specification_version: 4
|
|
46
|
+
summary: Ruby port of Project Katal
|
|
47
|
+
test_files: []
|