Module: DataSift

Included in:
ApiResource
Defined in:
lib/odp.rb,
lib/push.rb,
lib/pylon.rb,
lib/tasks.rb,
lib/account.rb,
lib/version.rb,
lib/datasift.rb,
lib/historics.rb,
lib/live_stream.rb,
lib/managed_source.rb,
lib/account_identity.rb,
lib/api/api_resource.rb,
lib/historics_preview.rb,
lib/managed_source_auth.rb,
lib/account_identity_limit.rb,
lib/account_identity_token.rb,
lib/managed_source_resource.rb

Defined Under Namespace

Classes: Account, AccountIdentity, AccountIdentityLimit, AccountIdentityToken, ApiResource, Client, Historics, HistoricsPreview, LiveStream, ManagedSource, ManagedSourceAuth, ManagedSourceResource, Odp, Push, Pylon, Task

Constant Summary collapse

VERSION =
'3.10.0'.freeze
IS_WINDOWS =
(RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
KNOWN_SOCKETS =
{}
DETECT_DEAD_SOCKETS =
true
SOCKET_DETECTOR_TIMEOUT =
6.5
GET =
'GET'.freeze
HEAD =
'HEAD'.freeze
DELETE =
'DELETE'.freeze
APPLICATION_JSON =
'application/json'.freeze
X_RATELIMIT_LIMIT =

Rate limits

'x_ratelimit_limit'.freeze
X_RATELIMIT_REMAINING =
'x_ratelimit_remaining'.freeze
X_RATELIMIT_COST =
'x_ratelimit_cost'.freeze
X_TASKS_QUEUED =
'x_tasks_queued'.freeze
X_TASKS_QUEUE_LIMIT =
'x_tasks_queue_limit'.freeze
X_ANALYSIS_TASKS_QUEUE_LIMIT =
'x_analysis_tasks_queue_limit'.freeze
X_ANALYSIS_TASKS_QUEUED =
'x_analysis_tasks_queued'.freeze
X_INSIGHT_TASKS_QUEUE_LIMIT =
'x_insight_tasks_queue_limit'.freeze
X_INSIGHT_TASKS_QUEUED =
'x_insight_tasks_queued'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.request(method, path, config, params = {}, headers = {}, timeout = 30, open_timeout = 30, new_line_separated = false) ⇒ Object

Generates and executes an HTTP request from the params provided

Parameters:

  • method (Symbol)

    The HTTP method to use

  • path (String)

    The DataSift path relevant to the base URL of the API

  • config (Object)

    The config object containing user details

  • params (Hash) (defaults to: {})

    A hash representing the params to use in the request

  • headers (Hash) (defaults to: {})

    Any headers to pass to the API

  • timeout (Integer) (defaults to: 30)

    Set the request timeout

  • open_timeout (Integer) (defaults to: 30)

    Set the request open timeout

  • new_line_separated (Boolean) (defaults to: false)

    Will response be newline separated?



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/datasift.rb', line 171

def self.request(method, path, config, params = {}, headers = {},
  timeout = 30, open_timeout = 30, new_line_separated = false)

  validate config
  url = build_url(path, config)

  headers.update(
    :user_agent    => "DataSift/#{config[:api_version]} Ruby/v#{DataSift::VERSION}",
    :authorization => "#{config[:username]}:#{config[:api_key]}",
    :accept  => '*/*'
  )

  case method.to_s.upcase
  when GET, HEAD, DELETE
    url += "#{URI.parse(url).query ? '&' : '?'}#{encode params}"
    payload = nil
  else
    payload = params.is_a?(String) ? params : MultiJson.dump(params)
    headers.update({ :content_type => APPLICATION_JSON })
  end

  options = {
    :headers      => headers,
    :method       => method,
    :open_timeout => open_timeout,
    :timeout      => timeout,
    :payload      => payload,
    :url          => url,
    :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
        data = []
        response.split("\n").each { |e|
          interaction = MultiJson.load(e, :symbolize_keys => true)
          data.push(interaction)
          if params.key? :on_interaction
            params[:on_interaction].call(interaction)
          end
        }
      else
        data = MultiJson.load(response, :symbolize_keys => true)
      end
    else
      data = {}
    end
    {
      :data => data,
      :datasift => build_headers(response.headers),
      :http => {
        :status  => response.code,
        :headers => response.headers
      }
    }
  rescue MultiJson::DecodeError
    raise DataSiftError.new response
  rescue SocketError => e
    process_client_error(e)
  rescue RestClient::ExceptionWithResponse => e
    begin
      code = e.http_code
      body = e.http_body
      error = nil
      if code && body
        begin
          error = MultiJson.load(body)
        rescue MultiJson::ParseError
          # In cases where we receive 502 responses, Nginx may send HTML rather than JSON
          error = body
        end
        response_on_error = {
          :data => nil,
          :datasift => build_headers(e.response.headers),
          :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)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    process_client_error(e)
  end
end

Instance Method Details

#build_path(service, path, config) ⇒ Object

Only to be used for building URI paths for /pylon API calls. API v1.4+ requires a ‘service’

param to be passed as part of the URI. This checks the API version, and adds the service
if necessary


269
270
271
272
273
274
275
276
277
278
# File 'lib/datasift.rb', line 269

def build_path(service, path, config)
  # We need to add the service param to PYLON API URLs for API v1.4+
  if config[:api_version].split('v')[1].to_f >= 1.4
    split_path = path.split('/')
    path = split_path[0] + '/' + service + '/' + split_path[1]
  end
  puts path

  return path
end