Class: Factual::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/factual.rb

Overview

:nodoc:

Constant Summary collapse

CONNECT_TIMEOUT =
30
DEFAULT_LIMIT =
20

Instance Method Summary collapse

Constructor Details

#initialize(api_key, version, domain, debug = false) ⇒ Adapter

Returns a new instance of Adapter.



371
372
373
374
375
# File 'lib/factual.rb', line 371

def initialize(api_key, version, domain, debug=false)
  @domain = domain
  @base   = "/api/v#{version}/#{api_key}"
  @debug  = debug
end

Instance Method Details

#api_call(url) ⇒ Object

Raises:



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/factual.rb', line 377

def api_call(url)
  api_url = @base + url
  puts "[Factual API Call] http://#{@domain}#{api_url}" if @debug

  json = "{}"
  begin
    Net::HTTP.start(@domain, 80) do |http|
      response = http.get(api_url)
      json     = response.body
    end
  rescue Exception => e
    raise ApiError.new(e.to_s + " when getting " + api_url)
  end

  obj  = JSON.parse(json)
  resp = Factual::Response.new(obj)
  raise ApiError.new(resp["error"]) if resp["status"] == "error"
  return resp
end

#get_token(unique_id) ⇒ Object



441
442
443
444
445
446
# File 'lib/factual.rb', line 441

def get_token(unique_id)
  url  = "/sessions/get_token?uniqueId=#{unique_id}"
  resp = api_call(url)

  return resp["string"]
end

#input(table_key, params) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/factual.rb', line 448

def input(table_key, params)
  token = params.delete(:token)
  query_string = params.to_a.collect do |k,v| 
    v_string = (v.is_a?(Hash) || v.is_a?(Array)) ? v.to_json : v.to_s
    CGI.escape(k.to_s) + '=' + CGI.escape(v_string) 
  end.join('&')

  url  = "/tables/#{table_key}/input.js?" + query_string
  url += "&token=" + token if token
  resp = api_call(url)

  return resp['response']
end

#read_row(table_key, subject_key) ⇒ Object



404
405
406
407
408
409
410
411
# File 'lib/factual.rb', line 404

def read_row(table_key, subject_key)
  url  = "/tables/#{table_key}/read.jsaml?subject_key=#{subject_key}"
  resp = api_call(url)

  row_data = resp["response", "data", 0] || []
  row_data.unshift # remove the subject_key
  return row_data
end

#read_table(table_key, options = {}) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/factual.rb', line 413

def read_table(table_key, options={})
  filters   = options[:filters]
  sorts     = options[:sorts]
  searches  = options[:searches]
  page_size = options[:page_size]
  page      = options[:page]

  limit = page_size.to_i 
  limit = DEFAULT_LIMIT unless limit > 0
  offset = (page.to_i - 1) * limit
  offset = 0 unless offset > 0

  filters  = (filters || {}).merge( "$search" => searches) if searches

  filters_query = "&filters=" + CGI.escape(filters.to_json) if filters

  if sorts
    sorts = sorts[0] if sorts.length == 1
    sorts_query = "&sort=" + sorts.to_json
  end

  url  = "/tables/#{table_key}/read.jsaml?limit=#{limit}&offset=#{offset}" 
  url += filters_query.to_s + sorts_query.to_s
  resp = api_call(url)

  return resp["response"]
end

#schema(table_key) ⇒ Object



397
398
399
400
401
402
# File 'lib/factual.rb', line 397

def schema(table_key)
  url  = "/tables/#{table_key}/schema.json"
  resp = api_call(url)

  return resp["schema"]
end