factual-api 1.0.3 → 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v1.1
2
+ * debug mode
3
+ * raw read
4
+
1
5
  ## v1.0.3
2
6
  * adding crosswalk(namespace_id, namespace)
3
7
 
data/README.md CHANGED
@@ -402,5 +402,26 @@ You can query Factual for the detailed schema of any specific table in Factual.
402
402
  factual.table("global").schema
403
403
  ````
404
404
 
405
+ # Raw Read Queries
405
406
 
407
+ You can perform any read queries documented in the Factual API using the
408
+ raw read query. Just supply the full path (including the first
409
+ forward-slash) and the request will be made using your OAuth token:
406
410
 
411
+ ````ruby
412
+ # Find rows in the restaurant database whose name begins with "Star" and return both the data and a total count of the matched rows
413
+ factual.read('/t/restaurants-us?filters={"name":{"$bw":"Star"}}&include_count=true')
414
+ ````
415
+
416
+ # Debug Mode
417
+
418
+ To see the query paths generated by the driver you can use it in debug mode by passing true as
419
+ the third argument of the Factual constructor. Here is an example in irb:
420
+
421
+
422
+ ````ruby
423
+ > factual = Factual.new(key, secret, true)
424
+ > factual.table("places").filters("name" => {"$bw" => "starbucks"})
425
+ Request: http://api.v3.factual.com/t/places?filters=%7B%22name%22%3A%7B%22%24bw%22%3A%22starbucks%22%7D%7D
426
+ => [{"address"=>"11290 Donner Pass Rd", "category"=>"Food & Beverage > Cafes, Coffee Houses & Tea Houses", ...
427
+ ````
data/lib/factual.rb CHANGED
@@ -5,8 +5,8 @@ require 'factual/query/resolve'
5
5
  require 'factual/query/crosswalk'
6
6
 
7
7
  class Factual
8
- def initialize(key, secret)
9
- @api = API.new(generate_token(key, secret))
8
+ def initialize(key, secret, debug_mode = false)
9
+ @api = API.new(generate_token(key, secret), debug_mode)
10
10
  end
11
11
 
12
12
  def table(table_id_or_alias)
@@ -25,6 +25,10 @@ class Factual
25
25
  Query::Resolve.new(@api, :values => values)
26
26
  end
27
27
 
28
+ def read(path)
29
+ @api.raw_read(path)
30
+ end
31
+
28
32
  private
29
33
 
30
34
  def generate_token(key, secret)
data/lib/factual/api.rb CHANGED
@@ -7,8 +7,9 @@ class Factual
7
7
  DRIVER_VERSION_TAG = "factual-ruby-driver-1.0"
8
8
  PARAM_ALIASES = { :search => :q, :sort_asc => :sort }
9
9
 
10
- def initialize(access_token)
10
+ def initialize(access_token, debug_mode = false)
11
11
  @access_token = access_token
12
+ @debug_mode = debug_mode
12
13
  end
13
14
 
14
15
  def execute(query, other_params={})
@@ -20,18 +21,27 @@ class Factual
20
21
  handle_request(:schema, query.path + "/schema", query.params)["view"]
21
22
  end
22
23
 
24
+ def raw_read(path)
25
+ payload = JSON.parse(make_request("#{API_V3_HOST}#{path}").body)
26
+ handle_payload(payload)
27
+ end
28
+
23
29
  private
24
30
 
25
31
  def handle_request(action, path, params)
26
- payload = JSON.parse(make_request(path, params).body)
32
+ url = "#{API_V3_HOST}/#{path}?#{query_string(params)}"
33
+ puts "Request: #{url}" if @debug_mode
34
+ payload = JSON.parse(make_request(url).body)
35
+ handle_payload(payload)
36
+ end
37
+
38
+ def handle_payload(payload)
27
39
  raise StandardError.new(payload["message"]) unless payload["status"] == "ok"
28
40
  payload["response"]
29
41
  end
30
42
 
31
- def make_request(path, params)
32
- url = "#{API_V3_HOST}/#{path}?#{query_string(params)}"
43
+ def make_request(url)
33
44
  headers = { "X-Factual-Lib" => DRIVER_VERSION_TAG }
34
-
35
45
  @access_token.get(url, headers)
36
46
  end
37
47
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: factual-api
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.3
5
+ version: "1.1"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Rudiger Lippert
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-03-13 00:00:00 +08:00
14
+ date: 2012-03-30 00:00:00 +08:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency