callr 1.1.1 → 1.2.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 +4 -4
- data/lib/callr.rb +163 -148
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a348a5af36634ec06084fe4e3f5cd1dc6f245adb
|
4
|
+
data.tar.gz: 227abcce7f42089b33ea4750d7c92ae32be022ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9d063ef18d0845c85819d83638a3bff59e3831ea7450166ac98082a2342cdfa28e92a3de9671ca47ae2849cfb5844894303141bb8cd60d518424eb09d4fc761
|
7
|
+
data.tar.gz: 10b64324cf43a63179551a8599a41640ecf9b51391a3970e445882fc885e30e4f8689ec00360e87314a6093eef182232aef07a0c8758d2fdab895444905017b6
|
data/lib/callr.rb
CHANGED
@@ -6,153 +6,168 @@ require 'net/https'
|
|
6
6
|
require 'uri'
|
7
7
|
require 'json'
|
8
8
|
|
9
|
-
SDK_VERSION = "1.
|
9
|
+
SDK_VERSION = "1.2.0"
|
10
10
|
|
11
11
|
module CALLR
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
end
|
12
|
+
class Api
|
13
|
+
@login = nil
|
14
|
+
@password = nil
|
15
|
+
@proxy = nil
|
16
|
+
@login_as = nil
|
17
|
+
@headers = {
|
18
|
+
"Expect" => "",
|
19
|
+
"Content-Type" => "application/json-rpc; charset=utf-8"
|
20
|
+
}
|
21
|
+
|
22
|
+
API_URL = "https://api.callr.com/json-rpc/v1.1/"
|
23
|
+
|
24
|
+
###
|
25
|
+
# Initialization
|
26
|
+
# @param string login
|
27
|
+
# @param string password
|
28
|
+
###
|
29
|
+
def initialize(login, password, options = nil)
|
30
|
+
@login = login
|
31
|
+
@password = password
|
32
|
+
set_options(options)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
public
|
37
|
+
def set_options(options)
|
38
|
+
if options.is_a?(Hash)
|
39
|
+
options = symbolize_keys(options)
|
40
|
+
set_proxy(options[:proxy]) if options.has_key?(:proxy)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_login_as(type, target)
|
45
|
+
case type
|
46
|
+
when 'user'
|
47
|
+
type = 'User.login'
|
48
|
+
when 'account'
|
49
|
+
type = 'Account.hash'
|
50
|
+
else
|
51
|
+
raise CallrLocalException.new("INVALID_LOGIN_AS_TYPE", 2)
|
52
|
+
end
|
53
|
+
|
54
|
+
@login_as = "#{type} #{target}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_proxy(proxy)
|
58
|
+
if proxy.is_a?(String)
|
59
|
+
@proxy = URI.parse(proxy)
|
60
|
+
else
|
61
|
+
raise CallrLocalException.new("PROXY_NOT_STRING", 1)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
###
|
66
|
+
# Send a request to CALLR webservice
|
67
|
+
###
|
68
|
+
def call(method, *params)
|
69
|
+
send(method, params)
|
70
|
+
end
|
71
|
+
|
72
|
+
###
|
73
|
+
# Send a request to CALLR webservice
|
74
|
+
###
|
75
|
+
def send(method, params = [], id = nil)
|
76
|
+
check_auth()
|
77
|
+
|
78
|
+
json = {
|
79
|
+
:id => id.nil? || id.is_a?(Integer) == false ? rand(999 - 100) + 100 : id,
|
80
|
+
:jsonrpc => "2.0",
|
81
|
+
:method => method,
|
82
|
+
:params => params.is_a?(Array) ? params : []
|
83
|
+
}.to_json
|
84
|
+
|
85
|
+
uri = URI.parse(API_URL)
|
86
|
+
http = http_or_http_proxy(uri)
|
87
|
+
|
88
|
+
req = Net::HTTP::Post.new(uri.request_uri, @headers)
|
89
|
+
req.basic_auth(@login, @password)
|
90
|
+
req.add_field('User-Agent', "sdk=RUBY; sdk-version=#{SDK_VERSION}; lang-version=#{RUBY_VERSION}; platform=#{RUBY_PLATFORM}")
|
91
|
+
req.add_field('CALLR-Login-As', @login_as) unless @login_as.to_s.empty?
|
92
|
+
|
93
|
+
begin
|
94
|
+
res = http.request(req, json)
|
95
|
+
if res.code.to_i != 200
|
96
|
+
raise CallrException.new("HTTP_CODE_ERROR", -1, {:http_code => res.code.to_i, :http_message => res.message})
|
97
|
+
end
|
98
|
+
return parse_response(res)
|
99
|
+
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ETIMEDOUT, Errno::ECONNREFUSED,
|
100
|
+
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
|
101
|
+
raise CallrException.new("HTTP_EXCEPTION", -2, {:exception => e})
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
private
|
107
|
+
def symbolize_keys(hash)
|
108
|
+
res = {}
|
109
|
+
hash.map do |k,v|
|
110
|
+
res[k.to_sym] = v.is_a?(Hash) ? symbolize_keys(v) : v
|
111
|
+
end
|
112
|
+
return res
|
113
|
+
end
|
114
|
+
|
115
|
+
def check_auth
|
116
|
+
if @login.nil? || @password.nil? || @login.length == 0 || @password.length == 0
|
117
|
+
raise CallrLocalException.new("CREDENTIALS_NOT_SET", 1)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def http_or_http_proxy(uri)
|
122
|
+
if not @proxy.nil?
|
123
|
+
http = Net::HTTP::Proxy(
|
124
|
+
@proxy.host,
|
125
|
+
@proxy.port,
|
126
|
+
@proxy.user,
|
127
|
+
@proxy.password
|
128
|
+
).new(uri.host, uri.port)
|
129
|
+
else
|
130
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
131
|
+
end
|
132
|
+
|
133
|
+
http.use_ssl = uri.scheme == "https"
|
134
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
135
|
+
http.open_timeout = 10
|
136
|
+
http.read_timeout = 10
|
137
|
+
|
138
|
+
return http
|
139
|
+
end
|
140
|
+
|
141
|
+
###
|
142
|
+
# Response analysis
|
143
|
+
###
|
144
|
+
def parse_response(res)
|
145
|
+
begin
|
146
|
+
data = JSON.parse(res.body)
|
147
|
+
if data.nil? == false && data.has_key?("result") && data["result"].nil? == false
|
148
|
+
return data["result"]
|
149
|
+
elsif data.nil? == false && data.has_key?("error") && data["error"].nil? == false
|
150
|
+
raise CallrException.new(data["error"]["message"], data["error"]["code"], nil)
|
151
|
+
else
|
152
|
+
raise CallrException.new("INVALID_RESPONSE", -3, {:response => res.body})
|
153
|
+
end
|
154
|
+
rescue JSON::ParserError
|
155
|
+
raise CallrException.new("INVALID_RESPONSE", -3, {:response => res.body})
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
class CallrException < Exception
|
161
|
+
attr_reader :msg
|
162
|
+
attr_reader :code
|
163
|
+
attr_reader :data
|
164
|
+
|
165
|
+
def initialize(msg, code = 0, data = nil)
|
166
|
+
@msg = msg
|
167
|
+
@code = code
|
168
|
+
@data = data
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
class CallrLocalException < CallrException; end
|
173
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael JACQUIN
|
8
8
|
- Vincent MERCIER
|
9
|
+
- Baptiste CLAVIE
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2017-
|
13
|
+
date: 2017-05-03 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: json
|