Class: Airrecord::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/airrecord/client.rb

Constant Summary collapse

AIRTABLE_RPS_LIMIT =

Per Airtable’s documentation you will get throttled for 30 seconds if you issue more than 5 requests per second. Airrecord is a good citizen.

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



13
14
15
# File 'lib/airrecord/client.rb', line 13

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/airrecord/client.rb', line 6

def api_key
  @api_key
end

#connectionObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/airrecord/client.rb', line 17

def connection
  @connection ||= Faraday.new(
    url: "https://api.airtable.com",
    headers: {
      "Authorization" => "Bearer #{api_key}",
      "User-Agent"    => "Airrecord/#{Airrecord::VERSION}",
    },
  ) do |conn|
    if Airrecord.throttle?
      conn.request :airrecord_rate_limiter, requests_per_second: AIRTABLE_RPS_LIMIT
    end
    conn.adapter :net_http_persistent
  end
end

Instance Method Details

#escape(*args) ⇒ Object



32
33
34
# File 'lib/airrecord/client.rb', line 32

def escape(*args) 
  ERB::Util.url_encode(*args)
end

#handle_error(status, error) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/airrecord/client.rb', line 42

def handle_error(status, error)
  if error.is_a?(Hash) && error['error']
    raise Error, "HTTP #{status}: #{error['error']['type']}: #{error['error']['message']}"
  else
    raise Error, "HTTP #{status}: Communication error: #{error}"
  end
end

#parse(body) ⇒ Object



36
37
38
39
40
# File 'lib/airrecord/client.rb', line 36

def parse(body)
  JSON.parse(body)
rescue JSON::ParserError
  nil
end