Sha256: a0c8c9abf7106f68c21c1e71726f75b836507f729549fcb9349abf71d54bf81a

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 KB

Contents

require 'uri'
require_relative 'query_string'
require_relative 'faraday_rate_limiter'

module Airrecord
  class Client
    attr_reader :api_key
    attr_writer :connection

    # 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.
    AIRTABLE_RPS_LIMIT = 5

    def initialize(api_key)
      @api_key = api_key
    end

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

    def escape(*args)
      QueryString.escape(*args)
    end

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
airrecord-1.0.6 lib/airrecord/client.rb
airrecord-1.0.5 lib/airrecord/client.rb
airrecord-1.0.4 lib/airrecord/client.rb
airrecord-1.0.3 lib/airrecord/client.rb
airrecord-1.0.2 lib/airrecord/client.rb
airrecord-1.0.1 lib/airrecord/client.rb
airrecord-1.0.0 lib/airrecord/client.rb