ruby-factual 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. data/README.md +9 -7
  2. data/lib/factual.rb +31 -12
  3. metadata +3 -3
data/README.md CHANGED
@@ -1,15 +1,17 @@
1
1
  ## Sample Usage
2
- > require 'ruby-factual'
2
+ > require 'rubygems'
3
+ > gem 'ruby-factual'
4
+ > require 'factual'
3
5
  >
4
- > api = Factual::Api.new(:api_key => '<YOUR_FACTUAL_API_KEY>', :version => 2)
6
+ > api = Factual::Api.new(:api_key => "<YOUR_FACTUAL_API_KEY>", :version => 2)
5
7
  >
6
- > table = api.get_table('g9R1u2')
8
+ > table = api.get_table("g9R1u2")
7
9
  > puts table.name
8
10
  >
9
- > table.read(:state => 'hawaii').each do |row|
10
- > fact = row['test_field']
11
+ > table.read(:two_letter_abbrev => "CA").each do |state_info|
12
+ > fact = state_info["state"]
11
13
  > puts fact.value
12
- > if fact.input('the corrected value', :source => 'source', :comment => 'comment')
13
- > puts 'inputted'
14
+ > if fact.input("Kalifornia", :source => "source", :comment => "comment")
15
+ > puts "inputted"
14
16
  > end
15
17
  > end
data/lib/factual.rb CHANGED
@@ -16,14 +16,15 @@ module Factual
16
16
  end
17
17
 
18
18
  class Table
19
- attr_accessor :name, :description, :rating, :source, :creator, :total_row_count, :created_at, :updated_at, :fields
19
+ attr_accessor :name, :description, :rating, :source, :creator, :total_row_count, :created_at, :updated_at, :fields, :geo_enabled, :downloadable
20
20
  def initialize(table_key, adapter)
21
21
  @table_key = table_key
22
22
  @adapter = adapter
23
23
  @schema = adapter.schema(@table_key)
24
24
 
25
- [:name, :description, :rating, :source, :creator, :total_row_count, :created_at, :updated_at, :fields].each do |attr|
26
- self.send("#{attr}=", @schema[attr.to_s])
25
+ [:name, :description, :rating, :source, :creator, :total_row_count, :created_at, :updated_at, :fields, :geo_enabled, :downloadable].each do |attr|
26
+ key = camelize(attr)
27
+ self.send("#{attr}=", @schema[key])
27
28
  end
28
29
 
29
30
  @fields.each do |f|
@@ -44,6 +45,13 @@ module Factual
44
45
  Row.new(@adapter, @table_key, @fields, row_data)
45
46
  end
46
47
  end
48
+
49
+ private
50
+
51
+ def camelize(str)
52
+ s = str.to_s.split("_").collect{ |w| w.capitalize }.join
53
+ s[0].chr.downcase + s[1..-1]
54
+ end
47
55
  end
48
56
 
49
57
  class Row
@@ -64,7 +72,7 @@ module Factual
64
72
  @facts_hash = {}
65
73
  @fields.each_with_index do |f, idx|
66
74
  next if f["isPrimary"]
67
- @facts_hash[f["field_ref"]] = Fact.new(@adapter, @table_key, @subject_key, f['id'], row_data[idx+1])
75
+ @facts_hash[f["field_ref"]] = Fact.new(@adapter, @table_key, @subject_key, f, row_data[idx+1])
68
76
  end
69
77
  end
70
78
 
@@ -79,20 +87,26 @@ module Factual
79
87
  end
80
88
 
81
89
  class Fact
82
- attr_accessor :value, :subject_key, :field_id, :adapter
90
+ attr_accessor :value, :subject_key, :field, :adapter
83
91
 
84
- def initialize(adapter, table_key, subject_key, field_id, value)
92
+ def initialize(adapter, table_key, subject_key, field, value)
85
93
  @value = value
86
94
  @subject_key = subject_key
87
95
  @table_key = table_key
88
- @field_id = field_id
96
+ @field = field
89
97
  @adapter = adapter
90
98
  end
91
99
 
92
- def input(value, opts)
100
+ def field_ref
101
+ @field["field_ref"]
102
+ end
103
+
104
+ def input(value, opts={})
105
+ return false if value.nil?
106
+
93
107
  hash = opts.merge({
94
108
  :subjectKey => @subject_key,
95
- :fieldId => @field_id,
109
+ :fieldId => @field['id'],
96
110
  :value => value
97
111
  })
98
112
  query_string = hash.to_a.collect{ |k,v| URI.escape(k.to_s) + '=' + URI.escape(v.to_s) }.join('&')
@@ -120,10 +134,15 @@ module Factual
120
134
 
121
135
  def api_call(url)
122
136
  api_url = @base + url
123
- curl = Curl::Easy.new(api_url) do |c|
124
- c.connect_timeout = CONNECT_TIMEOUT
137
+
138
+ begin
139
+ curl = Curl::Easy.new(api_url) do |c|
140
+ c.connect_timeout = CONNECT_TIMEOUT
141
+ end
142
+ curl.http_get
143
+ rescue Exception => e
144
+ raise ApiError.new(e.to_s + " when getting " + api_url)
125
145
  end
126
- curl.http_get
127
146
 
128
147
  resp = JSON.parse(curl.body_str)
129
148
  raise ApiError.new(resp["error"]) if resp["status"] == "error"
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Forrest Cao