ToolJet/app/services/airtable_query_service.rb
Deepti Kakade b10d777f63
Fixes Rubocop issues in tests (#359)
* Updated project set up guide for Mac, added node version and Webpack install steps.

* Worked on PR comment i.e Can we change this line to Install Node.js ( version: v14.9.0 )

* Fixed "Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping."

* Resolved rubocop comments i.e "Style/FrozenStringLiteralComment: Missing frozen string literal comment." in test folder

Co-authored-by: Deepti Kakade <deepti@saeloun.com>
Co-authored-by: Deepti Kakade <“deepti@saeloun.com”>
2021-07-01 12:54:35 +05:30

68 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class AirtableQueryService
attr_accessor :query, :source, :options, :source_options, :current_user
def initialize(data_query, data_source, options, source_options, current_user)
@query = data_query
@source = data_source
@options = options
@source_options = source_options
@current_user = current_user
end
def process
operation = options["operation"]
api_key = source_options["api_key"]
error = false
if operation === "list_records"
base_id = options["base_id"]
table_name = options["table_name"]
page_size = options["page_size"]
offset = options["offset"]
result = list_records(api_key, base_id, table_name, page_size, offset)
data = result
error = result.code != 200
end
if operation === "retrieve_record"
base_id = options["base_id"]
table_name = options["table_name"]
record_id = options["record_id"]
result = retrieve_record(api_key, base_id, table_name, record_id)
data = result
error = result.code != 200
end
if error
{ status: "error", code: 500, message: data["message"], data: data }
else
{ status: "success", data: data }
end
end
private
def list_records(api_key, base_id, table_name, page_size, offset)
result = HTTParty.get(URI.encode("https://api.airtable.com/v0/#{base_id}/#{table_name}"),
headers: { "Content-Type":
"application/json", "Authorization": "Bearer #{api_key}" })
result
end
def retrieve_record(api_key, base_id, table_name, record_id)
result = HTTParty.get(URI.encode("https://api.airtable.com/v0/#{base_id}/#{table_name}/#{record_id}"),
headers: { "Content-Type":
"application/json", "Authorization": "Bearer #{api_key}" })
result
end
end