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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/investolink.rb', line 76
def self.request(method, url, api_key, api_token, params=nil, ={})
api_key ||= @@api_key
api_token ||= @@api_token
raise AuthenticationError.new('No API key provided. (HINT: set your API key using "Investolink.api_key = <API-KEY> and Investolink.api_token = <API-TOKEN>". You can generate API keys from the Crowdnetic web interface. See http://www.crowdnetic.com for details, or email [email protected] if you have any questions.)') unless api_key
=begin
if !verify_ssl_certs
unless @no_verify
$stderr.puts "WARNING: Running without SSL cert verification. Execute 'Investolink.verify_ssl_certs = true' to enable verification."
@no_verify = true
end
ssl_opts = { :verify_ssl => false }
elsif !Util.file_readable(@@ssl_bundle_path)
unless @no_bundle
$stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
@no_bundle = true
end
ssl_opts = { :verify_ssl => false }
else
ssl_opts = {
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
:ssl_ca_file => @@ssl_bundle_path
}
end
=end
ssl_opts = {}
uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
ua = {
:bindings_version => Investolink::VERSION,
:lang => 'ruby',
:lang_version => lang_version,
:platform => RUBY_PLATFORM,
:publisher => 'investolink',
:uname => uname
}
params = Util.objects_to_ids(params)
url = self.api_url(url) + ".json"
case method.to_s.downcase.to_sym
when :get, :head, :delete
if params && params.count
query_string = Util.flatten_params(params).collect{|p| "#{p[0]}=#{p[1]}"}.join('&')
url += "?#{query_string}"
end
payload = nil
else
payload = params
end
begin
= { :x_investolink_client_user_agent => Investolink::JSON.dump(ua) }.merge()
rescue => e
= {
:x_investolink_client_raw_user_agent => ua.inspect,
:error => "#{e} (#{e.class})"
}.merge()
end
= {
:user_agent => "Investolink/v1 RubyBindings/#{Investolink::VERSION}",
:x_api_key => "#{api_key}",
:x_api_token => "#{api_token}"
}.merge()
opts = {
:method => method,
:url => url,
:headers => ,
:open_timeout => 30,
:payload => payload,
:timeout => 80
}.merge(ssl_opts)
begin
response = execute_request(opts)
rescue SocketError => e
self.handle_restclient_error(e)
rescue NoMethodError => e
if e.error_message =~ /\WRequestFailed\W/
e = APIConnectionError.new('Unexpected HTTP response code')
self.handle_restclient_error(e)
else
raise
end
rescue RestClient::ExceptionWithResponse => e
if rcode = e.http_code and rbody = e.http_body
self.handle_api_error(rcode, rbody)
else
self.handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
self.handle_restclient_error(e)
end
rbody = response.body
rcode = response.code
begin
resp = Investolink::JSON.load(rbody)
rescue MultiJson::DecodeError
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
end
resp = Util.symbolize_names(resp)
[resp, api_key, api_token]
end
|