ruby-factual 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -15,7 +15,7 @@ A block of code is worth a thousand words.
15
15
  > puts table.name, table.creator
16
16
  >
17
17
  > # read rows after filtering and sorting
18
- > table.filter(:two_letter_abbrev => "CA").sort(:state => -1).each_row do |state_info|
18
+ > table.filter(:two_letter_abbrev => "CA").sort(:state => -1).page(1, :size => 5).each_row do |state_info|
19
19
  >
20
20
  > # read facts
21
21
  > # fact attributes: value, subject_key, field_ref, field (hash)
data/lib/factual.rb CHANGED
@@ -47,6 +47,7 @@ module Factual
47
47
  # the table data before calling a find_one or each_row.
48
48
  class Table
49
49
  attr_accessor :name, :key, :description, :rating, :source, :creator, :total_row_count, :created_at, :updated_at, :fields, :geo_enabled, :downloadable
50
+ attr_accessor :page_size, :page
50
51
  attr_reader :adapter # :nodoc:
51
52
 
52
53
  def initialize(table_key, adapter) # :nodoc:
@@ -54,6 +55,8 @@ module Factual
54
55
  @adapter = adapter
55
56
  @schema = adapter.schema(@table_key)
56
57
  @key = table_key
58
+ @page_size = Adapter::DEFAULT_LIMIT
59
+ @page = 1
57
60
 
58
61
  [:name, :description, :rating, :source, :creator, :total_row_count, :created_at, :updated_at, :fields, :geo_enabled, :downloadable].each do |attr|
59
62
  k = camelize(attr)
@@ -66,6 +69,23 @@ module Factual
66
69
  end
67
70
  end
68
71
 
72
+ # Define the paging, it can be chained before +find_one+ or +each_row+, +each_row+ makes more sense though.
73
+ # The default page size is 20. And there is a {API limitation policy}[http://wiki.developer.factual.com/ApiLimit] need to be followed.
74
+ #
75
+ # The params are:
76
+ # * page_num: the page number
77
+ # * page_size_hash (optional): { :size => <page_size> }
78
+ #
79
+ # Samples:
80
+ # table.page(2).each_row { |row| ... } # for each row of the 2nd page
81
+ # table.page(2, :size => 10).each_row { |row| ... } # for each row of the 2nd page, when page size is 10
82
+ def page(page_num, page_size_hash=nil)
83
+ @page_size = page_size_hash[:size] || @page_size if page_size_hash
84
+ @page = page_num.to_i if page_num.to_i > 0
85
+
86
+ return self
87
+ end
88
+
69
89
  # Define table filters, it can be chained before +find_one+ or +each_row+.
70
90
  #
71
91
  # The params can be:
@@ -122,7 +142,7 @@ module Factual
122
142
  # puts row.inspect
123
143
  # end
124
144
  def each_row
125
- resp = @adapter.read_table(@table_key, @filters, @sorts)
145
+ resp = @adapter.read_table(@table_key, @filters, @sorts, @page_size, @page)
126
146
 
127
147
  @total_rows = resp["total_rows"]
128
148
  rows = resp["data"]
@@ -133,8 +153,18 @@ module Factual
133
153
  end
134
154
  end
135
155
 
136
- # TODO
137
- def add_row(values)
156
+ # Samples:
157
+ # api.get_table('g9R1u2').add_row('NE') # add row with single subject
158
+ # api.get_table('g9R1u2').add_row('NE', :state => 'Nebraska') # add single subject with fact values
159
+ # api.get_table('EZ21ij').add_row(:last_name => 'Newguy') # if the table use UUID as their subject, the subject fields are not required
160
+ #
161
+ # Returns:
162
+ # a Factual::Row object
163
+ def add_row(*params)
164
+ fact_values = params.last.is_a?(Hash) ? params.pop : {}
165
+ subject_values = params
166
+ puts subject_values.inspect
167
+ puts fact_values.inspect
138
168
  end
139
169
 
140
170
  private
@@ -240,6 +270,7 @@ module Factual
240
270
 
241
271
  class Adapter # :nodoc:
242
272
  CONNECT_TIMEOUT = 30
273
+ DEFAULT_LIMIT = 20
243
274
 
244
275
  def initialize(api_key, version, domain, debug=false)
245
276
  @domain = domain
@@ -273,7 +304,13 @@ module Factual
273
304
  return resp["schema"]
274
305
  end
275
306
 
276
- def read_table(table_key, filters=nil, sorts=nil, limit=999)
307
+ def read_table(table_key, filters=nil, sorts=nil, page_size=nil, page=nil)
308
+ limit = page_size.to_i
309
+ limit = DEFAULT_LIMIT unless limit > 0
310
+ offset = (page.to_i - 1) * limit
311
+ offset = 0 unless offset > 0
312
+
313
+ # TODO clean it
277
314
  filters_query = "&filters=" + filters.to_json if filters
278
315
 
279
316
  if sorts
@@ -281,7 +318,7 @@ module Factual
281
318
  sorts_query = "&sort=" + sorts.to_json
282
319
  end
283
320
 
284
- url = "/tables/#{table_key}/read.jsaml?limit=#{limit}" + filters_query.to_s + sorts_query.to_s
321
+ url = "/tables/#{table_key}/read.jsaml?limit=#{limit}&offset=#{offset}" + filters_query.to_s + sorts_query.to_s
285
322
  resp = api_call(url)
286
323
 
287
324
  return resp["response"]
data/test/unit/adapter.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'test/unit/helper'
2
2
  require 'lib/factual'
3
3
 
4
- class AdapterTest < Factual::TestCase
4
+ class AdapterTest < Factual::TestCase # :nodoc:
5
5
  def setup
6
6
  @adapter = Factual::Adapter.new(API_KEY, API_VERSION, API_DOMAIN, DEBUG_MODE)
7
7
  end
data/test/unit/table.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'lib/factual'
2
2
  require 'test/unit/helper'
3
3
 
4
- class TableTest < Factual::TestCase
4
+ class TableTest < Factual::TestCase # :nodoc:
5
5
  def setup
6
6
  api = Factual::Api.new(:api_key => API_KEY, :debug => DEBUG_MODE)
7
7
 
@@ -35,11 +35,32 @@ class TableTest < Factual::TestCase
35
35
  row = @table.sort(:state => 1).find_one
36
36
  assert_equal row["state"].value, "California"
37
37
 
38
- assert_raise Factual::ApiError do
39
- # secondary sort will be supported in next release
40
- row = @table.sort({:state => 1}, {:test_field1 => 1}).find_one
41
- row["state"].value
38
+ # secondary sort
39
+ row = @table.sort({:test_field1 => 1}, {:state => -1}).find_one
40
+ assert_equal row["state"].value, "Washington"
41
+ end
42
+
43
+ def test_each_row
44
+ states = []
45
+ @table.each_row do |row|
46
+ states << row['state'].value
42
47
  end
48
+
49
+ assert_equal states.length, @table.total_row_count
50
+ end
51
+
52
+ def test_paging
53
+ states = []
54
+ @table.page(2, :size => 2).each_row do |row|
55
+ states << row['state'].value
56
+ end
57
+
58
+ assert_equal states.length, 2
59
+ assert_not_equal states[0], "California"
60
+ end
61
+
62
+ def test_adding_row
63
+ row = @table.add_row('NE', :state => 'Nebraska')
43
64
  end
44
65
 
45
66
  def test_row
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-factual
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Forrest Cao