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