Class: Keplars::Client
- Inherits:
-
Object
- Object
- Keplars::Client
- Defined in:
- lib/keplars/client.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
'https://api.keplars.com'- DEFAULT_TIMEOUT =
30- DEFAULT_MAX_RETRIES =
3- DEFAULT_RETRY_DELAY =
1
Instance Attribute Summary collapse
-
#audiences ⇒ Object
readonly
Returns the value of attribute audiences.
-
#automations ⇒ Object
readonly
Returns the value of attribute automations.
-
#contacts ⇒ Object
readonly
Returns the value of attribute contacts.
-
#domains ⇒ Object
readonly
Returns the value of attribute domains.
-
#emails ⇒ Object
readonly
Returns the value of attribute emails.
Instance Method Summary collapse
-
#initialize(api_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, retry_delay: DEFAULT_RETRY_DELAY) ⇒ Client
constructor
A new instance of Client.
- #request(method, path, body: nil, retry_count: 0) ⇒ Object
Constructor Details
#initialize(api_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, retry_delay: DEFAULT_RETRY_DELAY) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/keplars/client.rb', line 14 def initialize(api_key: nil, base_url: nil, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, retry_delay: DEFAULT_RETRY_DELAY) @api_key = api_key || ENV['KEPLARS_API_KEY'] raise ArgumentError, 'API key is required. Set KEPLARS_API_KEY or pass api_key parameter' if @api_key.nil? || @api_key.empty? unless validate_api_key(@api_key) raise ArgumentError, 'Invalid API key format. Expected: kms_<id>.live_<secret> or kms_<id>.adm_<secret>' end resolved_base_url = base_url || ENV['KEPLARS_BASE_URL'] || DEFAULT_BASE_URL @base_url = resolved_base_url.sub(/\/$/, '') @timeout = timeout @max_retries = max_retries @retry_delay = retry_delay @emails = Resources::Emails.new(self) @contacts = Resources::Contacts.new(self) @audiences = Resources::Audiences.new(self) @automations = Resources::Automations.new(self) @domains = Resources::Domains.new(self) end |
Instance Attribute Details
#audiences ⇒ Object (readonly)
Returns the value of attribute audiences.
12 13 14 |
# File 'lib/keplars/client.rb', line 12 def audiences @audiences end |
#automations ⇒ Object (readonly)
Returns the value of attribute automations.
12 13 14 |
# File 'lib/keplars/client.rb', line 12 def automations @automations end |
#contacts ⇒ Object (readonly)
Returns the value of attribute contacts.
12 13 14 |
# File 'lib/keplars/client.rb', line 12 def contacts @contacts end |
#domains ⇒ Object (readonly)
Returns the value of attribute domains.
12 13 14 |
# File 'lib/keplars/client.rb', line 12 def domains @domains end |
#emails ⇒ Object (readonly)
Returns the value of attribute emails.
12 13 14 |
# File 'lib/keplars/client.rb', line 12 def emails @emails end |
Instance Method Details
#request(method, path, body: nil, retry_count: 0) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/keplars/client.rb', line 35 def request(method, path, body: nil, retry_count: 0) uri = URI("#{@base_url}#{path}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.read_timeout = @timeout http.open_timeout = @timeout request_class = case method.upcase when 'GET' then Net::HTTP::Get when 'POST' then Net::HTTP::Post when 'PUT' then Net::HTTP::Put when 'PATCH' then Net::HTTP::Patch when 'DELETE' then Net::HTTP::Delete else raise ArgumentError, "Unsupported HTTP method: #{method}" end request = request_class.new(uri.request_uri) request['Authorization'] = "Bearer #{@api_key}" request['Content-Type'] = 'application/json' request['User-Agent'] = "keplars-ruby/#{VERSION}" request.body = body.to_json if body begin response = http.request(request) rate_limit_info = extract_rate_limit_info(response) if response.code.to_i >= 400 handle_error_response(response, retry_count, method, path, body) end result = response.body && !response.body.empty? ? JSON.parse(response.body, symbolize_names: true) : nil { data: result, rate_limit_info: rate_limit_info } rescue StandardError => e if retryable_error?(e) && retry_count < @max_retries delay = calculate_backoff(retry_count) sleep(delay) request(method, path, body: body, retry_count: retry_count + 1) else raise NetworkError.new("Request failed: #{e.}", original_error: e) end end end |