Class: Factual::API

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

Constant Summary collapse

VERSION =
"1.3.20"
API_V3_HOST =
"api.v3.factual.com"
DRIVER_VERSION_TAG =
"factual-ruby-driver-v" + VERSION
PARAM_ALIASES =
{ :search => :q, :sort_asc => :sort }

Instance Method Summary collapse

Constructor Details

#initialize(access_token, debug_mode = false, host = nil, timeout = nil) ⇒ API

Returns a new instance of API.



8
9
10
11
12
13
14
# File 'lib/factual/api.rb', line 8

def initialize(access_token, debug_mode = false, host = nil, timeout = nil)
  @access_token = access_token
  @debug_mode = debug_mode
  @timeout = timeout
  @host = host || API_V3_HOST
  @headers = {}
end

Instance Method Details

#apply_header(key, value) ⇒ Object



16
17
18
# File 'lib/factual/api.rb', line 16

def apply_header(key, value)
  @headers[key] = value if value
end

#diffs(view_id, params = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/factual/api.rb', line 53

def diffs(view_id, params = {})
  start_date = (params[:start] || params["start"] || 0).to_i * 1000
  end_date = (params[:end] || params["end"] || Time.now).to_i * 1000

  path = "/t/#{view_id}/diffs?start=#{start_date}&end=#{end_date}"
  url = "http://#{@host}#{path}"
  resp = make_request(url)

  resp.body.split("\n").collect do |rowJson|
    row = JSON.parse(rowJson)
  end
end

#full_path(action, path, params) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/factual/api.rb', line 66

def full_path(action, path, params)
  fp = "/#{path}"
  fp += "/#{action}" unless action == :read

  qs = query_string(params)
  fp += "?#{qs}" unless qs.empty?

  fp
end

#get(query, other_params = {}) ⇒ Object



20
21
22
23
# File 'lib/factual/api.rb', line 20

def get(query, other_params = {})
  merged_params = query.params.merge(other_params)
  handle_request(query.action || :read, query.path, merged_params)
end

#post(request) ⇒ Object



25
26
27
# File 'lib/factual/api.rb', line 25

def post(request)
  handle_request(nil, request.path, request.body, :post)
end

#raw_get(path, query) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/factual/api.rb', line 33

def raw_get(path, query)
  path = '/' + path unless path =~ /^\//
  url = "http://#{@host}#{path}"

  qs = query_string(query)
  url += "?#{qs}" unless qs.empty?

  resp = make_request(url)
  payload = JSON.parse(resp.body)
  handle_payload(payload)
end

#raw_post(path, body) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/factual/api.rb', line 45

def raw_post(path, body)
  path = '/' + path unless path =~ /^\//
  url = "http://#{@host}#{path}"
  resp = make_request(url, query_string(body), :post)
  payload = JSON.parse(resp.body)
  handle_payload(payload)
end

#schema(query) ⇒ Object



29
30
31
# File 'lib/factual/api.rb', line 29

def schema(query)
  handle_request(:schema, query.path, query.params)["view"]
end