Class: AirtableSync::Api
- Inherits:
-
Object
- Object
- AirtableSync::Api
- Defined in:
- app/services/airtable_sync/api.rb
Instance Attribute Summary collapse
-
#base_id ⇒ Object
readonly
Returns the value of attribute base_id.
Class Method Summary collapse
Instance Method Summary collapse
- #bases ⇒ Object
- #create_record(record, table_name) ⇒ Object
- #delete_record(table_name, airtable_id) ⇒ Object
- #get_all_items(table_name:) ⇒ Object
- #get_item(table_name, airtable_id) ⇒ Object
-
#initialize(base_id = nil) ⇒ Api
constructor
A new instance of Api.
- #table(table_name) ⇒ Object
- #update_record(record, table_name) ⇒ Object
Constructor Details
#initialize(base_id = nil) ⇒ Api
Returns a new instance of Api.
7 8 9 |
# File 'app/services/airtable_sync/api.rb', line 7 def initialize(base_id = nil) @base_id = base_id end |
Instance Attribute Details
#base_id ⇒ Object (readonly)
Returns the value of attribute base_id.
5 6 7 |
# File 'app/services/airtable_sync/api.rb', line 5 def base_id @base_id end |
Class Method Details
.bases ⇒ Object
12 |
# File 'app/services/airtable_sync/api.rb', line 12 def self.bases = new.bases |
Instance Method Details
#bases ⇒ Object
11 |
# File 'app/services/airtable_sync/api.rb', line 11 def bases = JSON[Airrecord::Client.new(ENV.fetch('AIRTABLE_PERSONAL_ACCESS_TOKEN')).connection.get('/v0/meta/bases').body]['bases'] |
#create_record(record, table_name) ⇒ Object
18 19 20 21 |
# File 'app/services/airtable_sync/api.rb', line 18 def create_record(record, table_name) airtable_record = table(table_name).create(record.as_airtable_json) # rubocop:disable Rails/SaveBang record.update_column(:airtable_id, airtable_record.id) # rubocop:disable Rails/SkipsModelValidations end |
#delete_record(table_name, airtable_id) ⇒ Object
29 |
# File 'app/services/airtable_sync/api.rb', line 29 def delete_record(table_name, airtable_id) = table(table_name).find(airtable_id).destroy |
#get_all_items(table_name:) ⇒ Object
14 |
# File 'app/services/airtable_sync/api.rb', line 14 def get_all_items(table_name:) = table(table_name).all |
#get_item(table_name, airtable_id) ⇒ Object
16 |
# File 'app/services/airtable_sync/api.rb', line 16 def get_item(table_name, airtable_id) = table(table_name).find(airtable_id) |
#table(table_name) ⇒ Object
31 |
# File 'app/services/airtable_sync/api.rb', line 31 def table(table_name) = Airrecord.table(AirtableSync.configuration.api_key, base_id, table_name) |
#update_record(record, table_name) ⇒ Object
23 24 25 26 27 |
# File 'app/services/airtable_sync/api.rb', line 23 def update_record(record, table_name) airtable_record = table(table_name).find(record.airtable_id) record.as_airtable_json.each { |key, value| airtable_record[key.to_s] = value } airtable_record.save # rubocop:disable Rails/SaveBang end |