airtable 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/airtable.gemspec +1 -0
- data/lib/airtable/record.rb +8 -4
- data/lib/airtable/table.rb +8 -3
- data/lib/airtable/version.rb +1 -1
- data/test/airtable_test.rb +22 -1
- data/test/test_helper.rb +4 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67239aa74286bc438bd259bca768866f807d65c6
|
4
|
+
data.tar.gz: d62daaec49b37d8e881f199989cb8522fc3526f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6169dd1faf0dae55dc2792b7ba42115a8d73c8497f230c8d00825feb64ffeebb67b455cafb0c5d064ceb9a11bbc0aba2c140fca45a8de5b6aaf2db592a3102f
|
7
|
+
data.tar.gz: 99c83f64005e54e67fd48a11633ce860897a3fed6fdb757a26641e6ae02b123218387655e3c238dcfd3e41ffb3fa086c6c76fb86eb66cc91ffcc9ae8b6a27a2e
|
data/README.md
CHANGED
@@ -91,7 +91,7 @@ Records can be inserted using the `create` method on a table:
|
|
91
91
|
|
92
92
|
```ruby
|
93
93
|
@record = Airtable::Record.new(:name => "Sarah Jaine", :email => "sarah@jaine.com")
|
94
|
-
@table.create(record)
|
94
|
+
@table.create(@record)
|
95
95
|
# => #<Airtable::Record :name=>"Sarah Jaine", :email=>"sarah@jaine.com", :id=>"rec03sKOVIzU65eV4">
|
96
96
|
```
|
97
97
|
|
data/airtable.gemspec
CHANGED
data/lib/airtable/record.rb
CHANGED
@@ -2,9 +2,7 @@ module Airtable
|
|
2
2
|
|
3
3
|
class Record
|
4
4
|
def initialize(attrs={})
|
5
|
-
|
6
|
-
@attrs = HashWithIndifferentAccess.new(Hash[attrs.map { |k, v| [ to_key(k), v ] }])
|
7
|
-
@attrs.map { |k, v| define_accessor(k) }
|
5
|
+
override_attributes!(attrs)
|
8
6
|
end
|
9
7
|
|
10
8
|
def id; @attrs["id"]; end
|
@@ -28,10 +26,16 @@ module Airtable
|
|
28
26
|
# Hash of attributes with underscored column names
|
29
27
|
def attributes; @attrs; end
|
30
28
|
|
29
|
+
# Removes old and add new attributes for the record
|
30
|
+
def override_attributes!(attrs={})
|
31
|
+
@columns_map = attrs.keys
|
32
|
+
@attrs = HashWithIndifferentAccess.new(Hash[attrs.map { |k, v| [ to_key(k), v ] }])
|
33
|
+
@attrs.map { |k, v| define_accessor(k) }
|
34
|
+
end
|
35
|
+
|
31
36
|
# Hash with keys based on airtable original column names
|
32
37
|
def fields; Hash[@columns_map.map { |k| [ k, @attrs[to_key(k)] ] }]; end
|
33
38
|
|
34
|
-
|
35
39
|
def method_missing(name, *args, &blk)
|
36
40
|
# Accessor for attributes
|
37
41
|
if args.empty? && blk.nil? && @attrs.has_key?(name)
|
data/lib/airtable/table.rb
CHANGED
@@ -30,7 +30,7 @@ module Airtable
|
|
30
30
|
# Returns record based given row id
|
31
31
|
def find(id)
|
32
32
|
result = self.class.get(worksheet_url + "/" + id).parsed_response
|
33
|
-
Record.new(
|
33
|
+
Record.new(result_attributes(result)) if result.present? && result["id"]
|
34
34
|
end
|
35
35
|
|
36
36
|
# Creates a record by posting to airtable
|
@@ -39,7 +39,7 @@ module Airtable
|
|
39
39
|
:body => { "fields" => record.fields }.to_json,
|
40
40
|
:headers => { "Content-type" => "application/json" }).parsed_response
|
41
41
|
if result.present? && result["id"].present?
|
42
|
-
record.
|
42
|
+
record.override_attributes!(result_attributes(result))
|
43
43
|
record
|
44
44
|
else # failed
|
45
45
|
false
|
@@ -48,10 +48,11 @@ module Airtable
|
|
48
48
|
|
49
49
|
# Replaces record in airtable based on id
|
50
50
|
def update(record)
|
51
|
-
result = self.class.put(worksheet_url + "/" + id,
|
51
|
+
result = self.class.put(worksheet_url + "/" + record.id,
|
52
52
|
:body => { "fields" => record.fields }.to_json,
|
53
53
|
:headers => { "Content-type" => "application/json" }).parsed_response
|
54
54
|
if result.present? && result["id"].present?
|
55
|
+
record.override_attributes!(result_attributes(result))
|
55
56
|
record
|
56
57
|
else # failed
|
57
58
|
false
|
@@ -65,6 +66,10 @@ module Airtable
|
|
65
66
|
|
66
67
|
protected
|
67
68
|
|
69
|
+
def result_attributes(res)
|
70
|
+
res["fields"].merge("id" => res["id"]) if res.present? && res["id"]
|
71
|
+
end
|
72
|
+
|
68
73
|
def worksheet_url
|
69
74
|
"/#{app_token}/#{URI.encode(worksheet_name)}"
|
70
75
|
end
|
data/lib/airtable/version.rb
CHANGED
data/test/airtable_test.rb
CHANGED
@@ -16,10 +16,31 @@ describe Airtable do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should fetch record set" do
|
19
|
-
stub_airtable_response("https://api.airtable.com/v0/#{@app_key}/#{@sheet_name}", { "records" => [], "offset" => "abcde" })
|
19
|
+
stub_airtable_response!("https://api.airtable.com/v0/#{@app_key}/#{@sheet_name}", { "records" => [], "offset" => "abcde" })
|
20
20
|
@table = Airtable::Client.new(@client_key).table(@app_key, @sheet_name)
|
21
21
|
@records = @table.records
|
22
22
|
assert_equal "abcde", @records.offset
|
23
23
|
end
|
24
|
+
|
25
|
+
it "should allow creating records" do
|
26
|
+
stub_airtable_response!("https://api.airtable.com/v0/#{@app_key}/#{@sheet_name}",
|
27
|
+
{ "fields" => { "name" => "Sarah Jaine", "email" => "sarah@jaine.com", "foo" => "bar" }, "id" => "12345" }, :post)
|
28
|
+
table = Airtable::Client.new(@client_key).table(@app_key, @sheet_name)
|
29
|
+
record = Airtable::Record.new(:name => "Sarah Jaine", :email => "sarah@jaine.com")
|
30
|
+
table.create(record)
|
31
|
+
assert_equal "12345", record["id"]
|
32
|
+
assert_equal "bar", record["foo"]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should allow updating records" do
|
36
|
+
record_id = "12345"
|
37
|
+
stub_airtable_response!("https://api.airtable.com/v0/#{@app_key}/#{@sheet_name}/#{record_id}",
|
38
|
+
{ "fields" => { "name" => "Sarah Jaine", "email" => "sarah@jaine.com", "foo" => "bar" }, "id" => record_id }, :put)
|
39
|
+
table = Airtable::Client.new(@client_key).table(@app_key, @sheet_name)
|
40
|
+
record = Airtable::Record.new(:name => "Sarah Jaine", :email => "sarah@jaine.com", :id => record_id)
|
41
|
+
table.update(record)
|
42
|
+
assert_equal "12345", record["id"]
|
43
|
+
assert_equal "bar", record["foo"]
|
44
|
+
end
|
24
45
|
end # describe Airtable
|
25
46
|
end # Airtable
|
data/test/test_helper.rb
CHANGED
@@ -2,9 +2,11 @@ require 'airtable'
|
|
2
2
|
require 'fakeweb'
|
3
3
|
require 'minitest/pride'
|
4
4
|
require 'minitest/autorun'
|
5
|
+
require 'active_support/core_ext/hash'
|
5
6
|
|
7
|
+
# https://github.com/chrisk/fakeweb
|
6
8
|
FakeWeb.allow_net_connect = false
|
7
9
|
|
8
|
-
def stub_airtable_response(url, response)
|
9
|
-
FakeWeb.register_uri(
|
10
|
+
def stub_airtable_response!(url, response, method=:get)
|
11
|
+
FakeWeb.register_uri(method, url, :body => response.to_json, :content_type => "application/json")
|
10
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: airtable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Esquenazi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
83
97
|
description: Easily connect to airtable data using ruby with access to all of the
|
84
98
|
airtable features.
|
85
99
|
email:
|