Class: MethodRuby::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/method_ruby/client.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

8.0
ENVIRONMENTS =

rubocop:disable Style/MutableConstant

{
  production: "https://production.methodfi.com",
  sandbox: "https://sandbox.methodfi.com",
  dev: "https://dev.methodfi.com"
}

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(api_key: ENV["METHOD_API_KEY"], opal_token: nil, environment: nil, base_url: ENV["METHOD_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

token. Defaults to ‘ENV`

Each environment maps to a different base URL:

‘“api.example.com/v2/”`. Defaults to `ENV`

Parameters:

  • api_key (String, nil) (defaults to: ENV["METHOD_API_KEY"])

    Secret key for API authentication. Use your team’s secret key as the Bearer

  • opal_token (String, nil) (defaults to: nil)

    Opal token for Opal session endpoints.

  • environment (:production, :sandbox, :dev, nil) (defaults to: nil)

    Specifies the environment to use for the API.

  • base_url (String, nil) (defaults to: ENV["METHOD_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/method_ruby/client.rb', line 144

def initialize(
  api_key: ENV["METHOD_API_KEY"],
  opal_token: nil,
  environment: nil,
  base_url: ENV["METHOD_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= MethodRuby::Client::ENVIRONMENTS.fetch(environment&.to_sym || :production) do
    message = "environment must be one of #{MethodRuby::Client::ENVIRONMENTS.keys}, got #{environment}"
    raise ArgumentError.new(message)
  end

  if api_key.nil?
    raise ArgumentError.new("api_key is required, and can be set via environ: \"METHOD_API_KEY\"")
  end

  headers = {}
  custom_headers_env = ENV["METHOD_CUSTOM_HEADERS"]
  unless custom_headers_env.nil?
    parsed = {}
    custom_headers_env.split("\n").each do |line|
      colon = line.index(":")
      unless colon.nil?
        parsed[line[0...colon].strip] = line[(colon + 1)..].strip
      end
    end
    headers = parsed.merge(headers)
  end

  @api_key = api_key.to_s
  @opal_token = opal_token&.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: headers
  )

  @entities = MethodRuby::Resources::Entities.new(client: self)
  @accounts = MethodRuby::Resources::Accounts.new(client: self)
  @managed_accounts = MethodRuby::Resources::ManagedAccounts.new(client: self)
  @merchants = MethodRuby::Resources::Merchants.new(client: self)
  @payments = MethodRuby::Resources::Payments.new(client: self)
  @reports = MethodRuby::Resources::Reports.new(client: self)
  @simulate = MethodRuby::Resources::Simulate.new(client: self)
  @teams = MethodRuby::Resources::Teams.new(client: self)
  @webhooks = MethodRuby::Resources::Webhooks.new(client: self)
  @events = MethodRuby::Resources::Events.new(client: self)
  @forwarding_requests = MethodRuby::Resources::ForwardingRequests.new(client: self)
  @secrets = MethodRuby::Resources::Secrets.new(client: self)
  @card_products = MethodRuby::Resources::CardProducts.new(client: self)
  @ping = MethodRuby::Resources::Ping.new(client: self)
end

Instance Attribute Details

#accountsMethodRuby::Resources::Accounts (readonly)

Financial accounts (ACH, liability, clearing, debit card)



43
44
45
# File 'lib/method_ruby/client.rb', line 43

def accounts
  @accounts
end

#api_keyString (readonly)

Secret key for API authentication. Use your team’s secret key as the Bearer token.

Returns:

  • (String)


31
32
33
# File 'lib/method_ruby/client.rb', line 31

def api_key
  @api_key
end

#card_productsMethodRuby::Resources::CardProducts (readonly)

Card product definitions



86
87
88
# File 'lib/method_ruby/client.rb', line 86

def card_products
  @card_products
end

#entitiesMethodRuby::Resources::Entities (readonly)

Individuals, corporations, and receive-only entities



39
40
41
# File 'lib/method_ruby/client.rb', line 39

def entities
  @entities
end

#eventsMethodRuby::Resources::Events (readonly)

Webhook event log



74
75
76
# File 'lib/method_ruby/client.rb', line 74

def events
  @events
end

#forwarding_requestsMethodRuby::Resources::ForwardingRequests (readonly)

Request forwarding with sensitive data injection



78
79
80
# File 'lib/method_ruby/client.rb', line 78

def forwarding_requests
  @forwarding_requests
end

#managed_accountsMethodRuby::Resources::ManagedAccounts (readonly)

Method-managed accounts



47
48
49
# File 'lib/method_ruby/client.rb', line 47

def managed_accounts
  @managed_accounts
end

#merchantsMethodRuby::Resources::Merchants (readonly)

Merchant directory



51
52
53
# File 'lib/method_ruby/client.rb', line 51

def merchants
  @merchants
end

#opal_tokenString? (readonly)

Opal token for Opal session endpoints.

Returns:

  • (String, nil)


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

def opal_token
  @opal_token
end

#paymentsMethodRuby::Resources::Payments (readonly)

ACH and clearing payments



55
56
57
# File 'lib/method_ruby/client.rb', line 55

def payments
  @payments
end

#pingMethodRuby::Resources::Ping (readonly)

Health check endpoint



90
91
92
# File 'lib/method_ruby/client.rb', line 90

def ping
  @ping
end

#reportsMethodRuby::Resources::Reports (readonly)

Downloadable reports



59
60
61
# File 'lib/method_ruby/client.rb', line 59

def reports
  @reports
end

#secretsMethodRuby::Resources::Secrets (readonly)

Secure secret storage



82
83
84
# File 'lib/method_ruby/client.rb', line 82

def secrets
  @secrets
end

#simulateMethodRuby::Resources::Simulate (readonly)



62
63
64
# File 'lib/method_ruby/client.rb', line 62

def simulate
  @simulate
end

#teamsMethodRuby::Resources::Teams (readonly)

Team and API key management



66
67
68
# File 'lib/method_ruby/client.rb', line 66

def teams
  @teams
end

#webhooksMethodRuby::Resources::Webhooks (readonly)

Webhook subscriptions



70
71
72
# File 'lib/method_ruby/client.rb', line 70

def webhooks
  @webhooks
end