lib/datasift.rb in datasift-3.3.0 vs lib/datasift.rb in datasift-3.4.0
- old
+ new
@@ -19,10 +19,11 @@
require dir + '/pylon'
require dir + '/account'
require dir + '/account_identity'
require dir + '/account_identity_token'
require dir + '/account_identity_limit'
+require dir + '/odp'
#
require 'rbconfig'
module DataSift
#
@@ -49,11 +50,11 @@
# enable_ssl: true, open_timeout: 30, timeout: 30 }
def initialize(config)
raise InvalidConfigError.new('Config cannot be nil') if config.nil?
if !config.key?(:username) || !config.key?(:api_key)
raise InvalidConfigError.new('A valid username and API key are required. ' +
- 'You can check your API credentials at https://datasift.com/settings')
+ 'You can check your API credentials at https://app.datasift.com/settings')
end
@config = config
@historics = DataSift::Historics.new(config)
@push = DataSift::Push.new(config)
@@ -64,15 +65,16 @@
@pylon = DataSift::Pylon.new(config)
@account = DataSift::Account.new(config)
@account_identity = DataSift::AccountIdentity.new(config)
@account_identity_token = DataSift::AccountIdentityToken.new(config)
@account_identity_limit = DataSift::AccountIdentityLimit.new(config)
+ @odp = DataSift::Odp.new(config)
end
attr_reader :historics, :push, :managed_source, :managed_source_resource,
:managed_source_auth, :historics_preview, :pylon, :account,
- :account_identity, :account_identity_token, :account_identity_limit
+ :account_identity, :account_identity_token, :account_identity_limit, :odp
# Checks if the syntax of the given CSDL is valid
#
# @param boolResponse [Boolean] If true a boolean is returned indicating
# whether the CSDL is valid, otherwise the full response object is returned
@@ -165,25 +167,26 @@
case method.to_s.downcase.to_sym
when :get, :head, :delete
url += "#{URI.parse(url).query ? '&' : '?'}#{encode params}"
payload = nil
else
- payload = MultiJson.dump(params)
+ payload = params.is_a?(String) ? params : MultiJson.dump(params)
headers.update({ :content_type => 'application/json' })
end
options.update(
:headers => headers,
:method => method,
:open_timeout => open_timeout,
:timeout => timeout,
:payload => payload,
:url => url,
- :ssl_version => OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:ssl_version],
+ :ssl_version => config[:ssl_version],
:verify_ssl => OpenSSL::SSL::VERIFY_PEER
)
+ response = nil
begin
response = RestClient::Request.execute options
if !response.nil? && response.length > 0
if new_line_separated
res_arr = response.split("\n")
@@ -221,11 +224,23 @@
begin
code = e.http_code
body = e.http_body
if code && body
error = MultiJson.load(body)
- handle_api_error(e.http_code, (error['error'] ? error['error'] : '') + " for URL #{url}")
+ response_on_error = {
+ :data => nil,
+ :datasift => {
+ :x_ratelimit_limit => e.response.headers[:x_ratelimit_limit],
+ :x_ratelimit_remaining => e.response.headers[:x_ratelimit_remaining],
+ :x_ratelimit_cost => e.response.headers[:x_ratelimit_cost]
+ },
+ :http => {
+ :status => e.response.code,
+ :headers => e.response.headers
+ }
+ }
+ handle_api_error(e.http_code, (error['error'] ? error['error'] : '') + " for URL #{url}", response_on_error)
else
process_client_error(e)
end
rescue MultiJson::DecodeError
process_client_error(e)
@@ -236,12 +251,16 @@
end
private
def self.build_url(path, config)
- 'http' + (config[:enable_ssl] ? 's' : '') + '://' + config[:api_host] +
- '/' + config[:api_version] + '/' + path
+ url = 'http' + (config[:enable_ssl] ? 's' : '') + '://' + config[:api_host]
+ if !config[:api_version].nil?
+ url += '/' + config[:api_version] + '/' + path
+ else
+ url += '/' + path
+ end
end
# Returns true if username or api key are not set
def self.is_invalid?(config)
!config.key?(:username) || !config.key?(:api_key)
@@ -255,26 +274,30 @@
def self.encode(params)
params.collect { |param, value| [param, CGI.escape(value.to_s)].join('=') }.join('&')
end
- def self.handle_api_error(code, body)
+ def self.handle_api_error(code, body, response)
case code
when 400
- raise BadRequestError.new(code, body)
+ raise BadRequestError.new(code, body, response)
when 401
- raise AuthError.new(code, body)
+ raise AuthError.new(code, body, response)
when 404
- raise ApiResourceNotFoundError.new(code, body)
+ raise ApiResourceNotFoundError.new(code, body, response)
when 409
- raise ConflictError.new(code, body)
+ raise ConflictError.new(code, body, response)
when 410
- raise GoneError.new(code, body)
+ raise GoneError.new(code, body, response)
+ when 413
+ raise PayloadTooLargeError.new(code, body, response)
+ when 422
+ raise UnprocessableEntityError.new(code, body, response)
when 429
- raise TooManyRequestsError.new(code, body)
+ raise TooManyRequestsError.new(code, body, response)
else
- raise DataSiftError.new(code, body)
+ raise DataSiftError.new(code, body, response)
end
end
def self.process_client_error(e)
case e
@@ -308,11 +331,11 @@
end
unless on_close.nil?
raise BadParametersError.new('on_close - 2 parameter required') unless on_close.arity == 2
end
begin
- stream = WebsocketTD::Websocket.new('websocket.datasift.com', '/multi', "username=#{config[:username]}&api_key=#{config[:api_key]}")
+ stream = WebsocketTD::Websocket.new(config[:stream_host], '/multi', "username=#{config[:username]}&api_key=#{config[:api_key]}")
connection = LiveStream.new(config, stream)
KNOWN_SOCKETS[connection] = Time.new.to_i
stream.on_ping = lambda { |data|
KNOWN_SOCKETS[connection] = Time.new.to_i
}
@@ -360,15 +383,15 @@
if config[:retry_timeout] > config[:max_retry_time]
if use_closed && !on_close.nil?
on_close.call(connection, message)
else
- on_error.call(connection, ReconnectTimeoutError.new("Connecting to DataSift has failed, re-connection was attempted but
- multiple consecutive failures where encountered. As a result no further
- re-connection will be automatically attempted. Manually invoke connect() after
- investigating the cause of the failure, be sure to observe DataSift's
- re-connect policies available at http://dev.datasift.com/docs/streaming-api/reconnecting
- - Error { #{message}}"))
+ on_error.call(connection, ReconnectTimeoutError.new("Connecting to DataSift has " \
+ "failed, re-connection was attempted but multiple consecutive failures where " \
+ "encountered. As a result no further re-connection will be automatically " \
+ "attempted. Manually invoke connect() after investigating the cause of the " \
+ "failure, be sure to observe DataSift's re-connect policies available at " \
+ "http://dev.datasift.com/docs/streaming-api/reconnecting - Error {#{message}}"))
end
else
sleep config[:retry_timeout]
new_stream(config, on_delete, on_error, on_open, on_close)
end