optimis_client 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/optimis_client/base.rb +37 -22
- data/lib/optimis_client/version.rb +1 -1
- metadata +2 -2
data/lib/optimis_client/base.rb
CHANGED
@@ -2,45 +2,45 @@ module OptimisClient
|
|
2
2
|
|
3
3
|
class ResponseError < StandardError
|
4
4
|
attr_accessor :status, :message, :errors
|
5
|
-
|
5
|
+
|
6
6
|
def initialize(http_status, message=nil, errors = {})
|
7
7
|
self.status, self.message, self.errors = http_status, message, errors
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
class Base
|
12
|
-
|
12
|
+
|
13
13
|
DEFAULT_TIMEOUT = 10*1000 # 10s
|
14
14
|
|
15
15
|
@@hydra = Typhoeus::Hydra.new
|
16
|
-
|
16
|
+
|
17
17
|
class << self
|
18
18
|
attr_accessor :host, :secure, :api_key, :timeout
|
19
|
-
|
19
|
+
|
20
20
|
def hydra
|
21
21
|
@@hydra
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def stubbed?
|
25
25
|
!!@stubbed
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def stubbed=(value)
|
29
29
|
@stubbed = value
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def secure=(value, disable_ssl_peer_verification = true)
|
33
33
|
if value && !(Typhoeus::Easy.new.curl_version =~ /OpenSSL/)
|
34
|
-
raise "Your libcurl SSL support not enabled."
|
34
|
+
raise "Your libcurl SSL support not enabled."
|
35
35
|
end
|
36
36
|
@secure = value
|
37
37
|
@disable_ssl_peer_verification = disable_ssl_peer_verification
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def http_protocol
|
41
41
|
(self.secure)? "https://" : "http://"
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
def hydra_run_all
|
45
45
|
self.hydra.run unless self.stubbed?
|
46
46
|
end
|
@@ -48,18 +48,33 @@ module OptimisClient
|
|
48
48
|
protected
|
49
49
|
|
50
50
|
def new_request(url, options={})
|
51
|
-
options = { :disable_ssl_peer_verification => @disable_ssl_peer_verification,
|
51
|
+
options = { :disable_ssl_peer_verification => @disable_ssl_peer_verification,
|
52
52
|
:timeout => (@timeout || DEFAULT_TIMEOUT) }.merge(options)
|
53
|
-
|
54
|
-
options[:params] ||= {}
|
53
|
+
|
54
|
+
options[:params] ||= {}
|
55
55
|
options[:params].merge!( :api_key => self.api_key ) unless options[:params][:api_key]
|
56
|
-
|
56
|
+
if options[:params]
|
57
|
+
options[:params] = fix_array_param_keys(options[:params])
|
58
|
+
end
|
59
|
+
|
57
60
|
options[:headers] ||= {}
|
58
61
|
options[:headers].merge!( "Authorization" => self.api_key ) unless options[:headers][:api_key]
|
59
|
-
|
62
|
+
|
60
63
|
Typhoeus::Request.new(url, options)
|
61
64
|
end
|
62
|
-
|
65
|
+
|
66
|
+
def fix_array_param_keys(params)
|
67
|
+
fixed_params = {}
|
68
|
+
params.each do |key, value|
|
69
|
+
if Array === value
|
70
|
+
fixed_params.store("#{key}[]", value)
|
71
|
+
else
|
72
|
+
fixed_params.store(key, value)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
fixed_params
|
76
|
+
end
|
77
|
+
|
63
78
|
def parse_json(response)
|
64
79
|
begin
|
65
80
|
Yajl::Parser.parse(response.body)
|
@@ -67,8 +82,8 @@ module OptimisClient
|
|
67
82
|
raise ResponseError.new( 502, "Parsing service JSON error: #{response.body}")
|
68
83
|
end
|
69
84
|
end
|
70
|
-
|
71
|
-
def hydra_run(request)
|
85
|
+
|
86
|
+
def hydra_run(request)
|
72
87
|
self.hydra.queue(request)
|
73
88
|
self.hydra.run
|
74
89
|
response = request.response
|
@@ -81,8 +96,8 @@ module OptimisClient
|
|
81
96
|
return response
|
82
97
|
end
|
83
98
|
end
|
84
|
-
|
99
|
+
|
85
100
|
end
|
86
101
|
end
|
87
|
-
|
88
|
-
end
|
102
|
+
|
103
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optimis_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: typhoeus
|