Class: Billingrails::Client

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

Overview

Main client for interacting with the Billingrails API

Constant Summary collapse

DEFAULT_BASE_URL =
'https://api.billingrails.com/v1'
DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client

Initialize a new client

Parameters:

  • api_key (String)

    Your API key

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    Base URL for API requests

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Request timeout in seconds



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/billingrails/client.rb', line 44

def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT)
  @api_key = api_key
  @base_url = base_url.chomp('/')
  @timeout = timeout
  validate_config!

  # Initialize top-level resources
  @accounts = Resources::Accounts.new(self)
  @credit_grants = Resources::CreditGrants.new(self)
  @discounts = Resources::Discounts.new(self)
  @fees = Resources::Fees.new(self)
  @invoices = Resources::Invoices.new(self)
  @payments = Resources::Payments.new(self)
  @payment_pages = Resources::PaymentPages.new(self)

  # Initialize nested namespaces
  @biller = BillerNamespace.new(self)
  @seller = SellerNamespace.new(self)
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def accounts
  @accounts
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



35
36
37
# File 'lib/billingrails/client.rb', line 35

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



35
36
37
# File 'lib/billingrails/client.rb', line 35

def base_url
  @base_url
end

#billerObject (readonly)

Returns the value of attribute biller.



37
38
39
# File 'lib/billingrails/client.rb', line 37

def biller
  @biller
end

#credit_grantsObject (readonly)

Returns the value of attribute credit_grants.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def credit_grants
  @credit_grants
end

#discountsObject (readonly)

Returns the value of attribute discounts.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def discounts
  @discounts
end

#feesObject (readonly)

Returns the value of attribute fees.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def fees
  @fees
end

#invoicesObject (readonly)

Returns the value of attribute invoices.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def invoices
  @invoices
end

#payment_pagesObject (readonly)

Returns the value of attribute payment_pages.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def payment_pages
  @payment_pages
end

#paymentsObject (readonly)

Returns the value of attribute payments.



36
37
38
# File 'lib/billingrails/client.rb', line 36

def payments
  @payments
end

#sellerObject (readonly)

Returns the value of attribute seller.



37
38
39
# File 'lib/billingrails/client.rb', line 37

def seller
  @seller
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



35
36
37
# File 'lib/billingrails/client.rb', line 35

def timeout
  @timeout
end

Instance Method Details

#request(method, path, body: nil, params: nil) ⇒ Hash

Make an HTTP request

Parameters:

  • method (Symbol)

    HTTP method (:get, :post, :put, :patch, :delete)

  • path (String)

    API endpoint path

  • body (Hash, nil) (defaults to: nil)

    Request body

  • params (Hash, nil) (defaults to: nil)

    Query parameters

Returns:

  • (Hash)

    Parsed response body



71
72
73
74
75
76
77
# File 'lib/billingrails/client.rb', line 71

def request(method, path, body: nil, params: nil)
  uri = build_uri(path, params)
  request = build_request(method, uri, body)
  
  response = execute_request(uri, request)
  handle_response(response)
end