Class: Factual::API

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

Constant Summary collapse

VERSION =
"1.3.2"
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.



12
13
14
15
16
17
# File 'lib/factual/api.rb', line 12

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
end

Instance Method Details

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



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/factual/api.rb', line 50

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



63
64
65
66
67
# File 'lib/factual/api.rb', line 63

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

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



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

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



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

def post(request)
  response = make_request("http://" + @host + request.path, request.body, :post)
  payload = JSON.parse(response.body)
  handle_payload(payload)
end

#raw_get(path, query) ⇒ Object



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

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

#raw_post(path, body) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/factual/api.rb', line 42

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



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

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