Class: Airrecord::Table
- Inherits:
-
Object
- Object
- Airrecord::Table
- Defined in:
- lib/airrecord/table.rb
Class Attribute Summary collapse
- .api_key ⇒ Object
-
.base_key ⇒ Object
Returns the value of attribute base_key.
-
.table_name ⇒ Object
Returns the value of attribute table_name.
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#updated_keys ⇒ Object
readonly
Returns the value of attribute updated_keys.
Class Method Summary collapse
- .belongs_to(method_name, options) ⇒ Object (also: has_one)
- .client ⇒ Object
- .create(fields, options = {}) ⇒ Object
- .find(id) ⇒ Object
- .find_many(ids) ⇒ Object
- .has_many(method_name, options) ⇒ Object
- .records(filter: nil, sort: nil, view: nil, offset: nil, paginate: true, fields: nil, max_records: nil, page_size: nil) ⇒ Object (also: all)
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #create(options = {}) ⇒ Object
- #destroy ⇒ Object
- #hash ⇒ Object
-
#initialize(fields, id: nil, created_at: nil) ⇒ Table
constructor
A new instance of Table.
- #new_record? ⇒ Boolean
- #save(options = {}) ⇒ Object
- #serializable_fields ⇒ Object
Constructor Details
#initialize(fields, id: nil, created_at: nil) ⇒ Table
Returns a new instance of Table.
110 111 112 113 114 |
# File 'lib/airrecord/table.rb', line 110 def initialize(fields, id: nil, created_at: nil) @id = id self.created_at = created_at self.fields = fields end |
Class Attribute Details
.api_key ⇒ Object
12 13 14 |
# File 'lib/airrecord/table.rb', line 12 def api_key defined?(@api_key) ? @api_key : Airrecord.api_key end |
.base_key ⇒ Object
Returns the value of attribute base_key.
4 5 6 |
# File 'lib/airrecord/table.rb', line 4 def base_key @base_key end |
.table_name ⇒ Object
Returns the value of attribute table_name.
4 5 6 |
# File 'lib/airrecord/table.rb', line 4 def table_name @table_name end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
108 109 110 |
# File 'lib/airrecord/table.rb', line 108 def created_at @created_at end |
#fields ⇒ Object
Returns the value of attribute fields.
108 109 110 |
# File 'lib/airrecord/table.rb', line 108 def fields @fields end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
108 109 110 |
# File 'lib/airrecord/table.rb', line 108 def id @id end |
#updated_keys ⇒ Object (readonly)
Returns the value of attribute updated_keys.
108 109 110 |
# File 'lib/airrecord/table.rb', line 108 def updated_keys @updated_keys end |
Class Method Details
.belongs_to(method_name, options) ⇒ Object Also known as: has_one
32 33 34 |
# File 'lib/airrecord/table.rb', line 32 def belongs_to(method_name, ) has_many(method_name, .merge(single: true)) end |
.client ⇒ Object
7 8 9 10 |
# File 'lib/airrecord/table.rb', line 7 def client @@clients ||= {} @@clients[api_key] ||= Client.new(api_key) end |
.create(fields, options = {}) ⇒ Object
57 58 59 |
# File 'lib/airrecord/table.rb', line 57 def create(fields, ={}) new(fields).tap { |record| record.save() } end |
.find(id) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/airrecord/table.rb', line 38 def find(id) response = client.connection.get("/v0/#{base_key}/#{client.escape(table_name)}/#{id}") parsed_response = client.parse(response.body) if response.success? self.new(parsed_response["fields"], id: id, created_at: parsed_response["createdTime"]) else client.handle_error(response.status, parsed_response) end end |
.find_many(ids) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/airrecord/table.rb', line 49 def find_many(ids) return [] if ids.empty? or_args = ids.map { |id| "RECORD_ID() = '#{id}'"}.join(',') formula = "OR(#{or_args})" records(filter: formula).sort_by { |record| or_args.index(record.id) } end |
.has_many(method_name, options) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/airrecord/table.rb', line 16 def has_many(method_name, ) define_method(method_name.to_sym) do # Get association ids in reverse order, because Airtable’s UI and API # sort associations in opposite directions. We want to match the UI. ids = (self[.fetch(:column)] || []).reverse table = Kernel.const_get(.fetch(:class)) return table.find_many(ids) unless [:single] (id = ids.first) ? table.find(id) : nil end define_method("#{method_name}=".to_sym) do |value| self[.fetch(:column)] = Array(value).map(&:id).reverse end end |
.records(filter: nil, sort: nil, view: nil, offset: nil, paginate: true, fields: nil, max_records: nil, page_size: nil) ⇒ Object Also known as: all
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/airrecord/table.rb', line 61 def records(filter: nil, sort: nil, view: nil, offset: nil, paginate: true, fields: nil, max_records: nil, page_size: nil) = {} [:filterByFormula] = filter if filter if sort [:sort] = sort.map { |field, direction| { field: field.to_s, direction: direction } } end [:view] = view if view [:offset] = offset if offset [:fields] = fields if fields [:maxRecords] = max_records if max_records [:pageSize] = page_size if page_size path = "/v0/#{base_key}/#{client.escape(table_name)}" response = client.connection.get(path, ) parsed_response = client.parse(response.body) if response.success? records = parsed_response["records"] records = records.map { |record| self.new(record["fields"], id: record["id"], created_at: record["createdTime"]) } if paginate && parsed_response["offset"] records.concat(records( filter: filter, sort: sort, view: view, paginate: paginate, fields: fields, offset: parsed_response["offset"], max_records: max_records, page_size: page_size, )) end records else client.handle_error(response.status, parsed_response) end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
192 193 194 195 |
# File 'lib/airrecord/table.rb', line 192 def ==(other) self.class == other.class && serializable_fields == other.serializable_fields end |
#[](key) ⇒ Object
120 121 122 123 |
# File 'lib/airrecord/table.rb', line 120 def [](key) validate_key(key) fields[key] end |
#[]=(key, value) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/airrecord/table.rb', line 125 def []=(key, value) validate_key(key) return if fields[key] == value # no-op @updated_keys << key fields[key] = value end |
#create(options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/airrecord/table.rb', line 132 def create(={}) raise Error, "Record already exists (record has an id)" unless new_record? body = { fields: serializable_fields, **, }.to_json response = client.connection.post("/v0/#{self.class.base_key}/#{client.escape(self.class.table_name)}", body, { 'Content-Type' => 'application/json' }) parsed_response = client.parse(response.body) if response.success? @id = parsed_response["id"] self.created_at = parsed_response["createdTime"] self.fields = parsed_response["fields"] else client.handle_error(response.status, parsed_response) end end |
#destroy ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/airrecord/table.rb', line 175 def destroy raise Error, "Unable to destroy new record" if new_record? response = client.connection.delete("/v0/#{self.class.base_key}/#{client.escape(self.class.table_name)}/#{self.id}") parsed_response = client.parse(response.body) if response.success? true else client.handle_error(response.status, parsed_response) end end |
#hash ⇒ Object
199 200 201 |
# File 'lib/airrecord/table.rb', line 199 def hash serializable_fields.hash end |
#new_record? ⇒ Boolean
116 117 118 |
# File 'lib/airrecord/table.rb', line 116 def new_record? !id end |
#save(options = {}) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/airrecord/table.rb', line 152 def save(={}) return create() if new_record? return true if @updated_keys.empty? # To avoid trying to update computed fields we *always* use PATCH body = { fields: Hash[@updated_keys.map { |key| [key, fields[key]] }], **, }.to_json response = client.connection.patch("/v0/#{self.class.base_key}/#{client.escape(self.class.table_name)}/#{self.id}", body, { 'Content-Type' => 'application/json' }) parsed_response = client.parse(response.body) if response.success? self.fields = parsed_response["fields"] else client.handle_error(response.status, parsed_response) end end |
#serializable_fields ⇒ Object
188 189 190 |
# File 'lib/airrecord/table.rb', line 188 def serializable_fields fields end |