class SynapsePayRest::Subscriptions

Wrapper class for /subscriptions endpoints

@todo Implement idempotency keys

Constants

VALID_QUERY_PARAMS

Valid optional args for get @todo Refactor to HTTPClient

Attributes

client[RW]

@!attribute [rw] client

@return [SynapsePayRest::HTTPClient]

Public Class Methods

new(client) click to toggle source

@param client [SynapsePayRest::HTTPClient]

# File lib/synapse_pay_rest/api/subscriptions.rb, line 16
def initialize(client)
  @client = client
end

Public Instance Methods

create(payload:) click to toggle source

Sends a POST request to /subscriptions endpoint to create a new subscription. Returns the response.

@param url [String] @param scope [Array] @see docs.synapsepay.com/docs/create-subscription payload structure

@raise [SynapsePayRest::Error] may return subclasses of error based on HTTP response from API

@return [Hash] API response

# File lib/synapse_pay_rest/api/subscriptions.rb, line 31
def create(payload:)
  path = subscription_path
  client.post(path, payload)
end
get(subscription_id: nil, **options) click to toggle source

Sends a GET request to /subscriptions endpoint. Queries a specific subscription_id if subs_id supplied, else queries all subscriptions. Returns the response.

@param subscription_id [String,void] (optional) id of a subscription to look up @param page [String,Integer] (optional) response will default to 1 @param per_page [String,Integer] (optional) response will default to 20

@raise [SynapsePayRest::Error] may return subclasses of error based on HTTP response from API

@return [Hash] API response

@todo Probably should use CGI or RestClient's param builder instead of rolling our own, probably error-prone and untested version github.com/rest-client/rest-client#usage-raw-url

# File lib/synapse_pay_rest/api/subscriptions.rb, line 51
def get(subscription_id: nil, **options)
  path = subscription_path(subscription_id: subscription_id)

  params = VALID_QUERY_PARAMS.map do |p|
    options[p] ? "#{p}=#{options[p]}" : nil
  end.compact

  path += '?' + params.join('&') if params.any?
  client.get(path)
end
update(subscription_id:, payload:) click to toggle source

Sends a PATCH request to /subscriptions endpoint, updating the current subscription and returns the response.

@param payload [Hash] @see docs.synapsepay.com/docs/update-subscription payload structure for

updating subscription

@raise [SynapsePayRest::Error] may return subclasses of error based on HTTP response from API

@return [Hash] API response

# File lib/synapse_pay_rest/api/subscriptions.rb, line 73
def update(subscription_id:, payload:)
  path = subscription_path(subscription_id: subscription_id)
  client.patch(path, payload)
end

Private Instance Methods

subscription_path(subscription_id: nil) click to toggle source
# File lib/synapse_pay_rest/api/subscriptions.rb, line 80
def subscription_path(subscription_id: nil)
  path = "/subscriptions"
  path += "/#{subscription_id}" if subscription_id
  path
end