ruby-factual 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +1 -1
  2. data/lib/factual.rb +32 -8
  3. metadata +3 -3
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  > table = api.get_table("g9R1u2")
9
9
  > puts table.name
10
10
  >
11
- > table.read(:two_letter_abbrev => "CA").each do |state_info|
11
+ > table.filter(:two_letter_abbrev => "CA").sort(:state => -1)each_row do |state_info|
12
12
  > fact = state_info["state"]
13
13
  > puts fact.value
14
14
  > if fact.input("Kalifornia", :source => "source", :comment => "comment")
data/lib/factual.rb CHANGED
@@ -7,7 +7,8 @@ module Factual
7
7
  def initialize(opts)
8
8
  @api_key = opts[:api_key]
9
9
  @version = opts[:version]
10
- @adapter = Adapter.new(@api_key, @version)
10
+ @domain = opts[:domain] || 'www.factual.com'
11
+ @adapter = Adapter.new(@api_key, @version, @domain)
11
12
  end
12
13
 
13
14
  def get_table(table_key)
@@ -33,21 +34,44 @@ module Factual
33
34
  end
34
35
  end
35
36
 
36
- def read(filters=nil)
37
- filters_query = "filters=" + filters.to_json if filters
38
- resp = @adapter.api_call("/tables/#{@table_key}/read.jsaml?limit=999&" + filters_query.to_s)
37
+ def filter(filters)
38
+ @filters = filters
39
+ return self
40
+ end
41
+
42
+ def sort(sorts)
43
+ @sorts = sorts
44
+ return self
45
+ end
46
+
47
+ def each_row
48
+ filters_query = "&filters=" + @filters.to_json if @filters
49
+ if @sorts
50
+ sorts_by = "sort_by=" + @sorts.keys.collect{|k| get_field_id(k).to_s}.join(",")
51
+ sorts_dir = "sort_dir=" + @sorts.values.collect{|v| (v==1) ? 'asc' : 'desc' }.join(",")
52
+ sorts_query = "&" + sorts_by + "&" + sorts_dir
53
+ end
54
+
55
+ resp = @adapter.api_call("/tables/#{@table_key}/read.jsaml?limit=999" + filters_query.to_s + sorts_query.to_s)
39
56
 
40
57
  @total_rows = resp["response"]["total_rows"]
41
58
  rows = resp["response"]["data"]
42
59
 
43
60
  # TODO iterator
44
- rows.collect do |row_data|
45
- Row.new(@adapter, @table_key, @fields, row_data)
61
+ rows.each do |row_data|
62
+ row = Row.new(@adapter, @table_key, @fields, row_data)
63
+ yield(row) if block_given?
46
64
  end
47
65
  end
48
66
 
49
67
  private
50
68
 
69
+ def get_field_id(field_ref)
70
+ @fields.each do |f|
71
+ return f['id'] if f['field_ref'] == field_ref.to_s
72
+ end
73
+ end
74
+
51
75
  def camelize(str)
52
76
  s = str.to_s.split("_").collect{ |w| w.capitalize }.join
53
77
  s[0].chr.downcase + s[1..-1]
@@ -128,8 +152,8 @@ module Factual
128
152
  class Adapter
129
153
  CONNECT_TIMEOUT = 30
130
154
 
131
- def initialize(api_key, version)
132
- @base = "http://api.factual.com/v#{version}/#{api_key}"
155
+ def initialize(api_key, version, domain)
156
+ @base = "http://#{domain}/api/v#{version}/#{api_key}"
133
157
  end
134
158
 
135
159
  def api_call(url)
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Forrest Cao