Class: SynapsePayRest::Transactions

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_pay_rest/api/transactions.rb

Overview

TODO:

Implement idempotency keys

Wrapper class for /trans endpoints

Constant Summary collapse

VALID_QUERY_PARAMS =
TODO:

Refactor to HTTPClient

Valid optional args for #get

[:page, :per_page].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Transactions

Returns a new instance of Transactions.

Parameters:



16
17
18
# File 'lib/synapse_pay_rest/api/transactions.rb', line 16

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientSynapsePayRest::HTTPClient



13
14
15
# File 'lib/synapse_pay_rest/api/transactions.rb', line 13

def client
  @client
end

Instance Method Details

#create(user_id:, node_id:, payload:, idempotency_key: nil) ⇒ Hash

Sends a POST request to /trans endpoint to create a new transaction. Returns the response.

HTTP response from API

Parameters:

  • user_id (String)
  • node_id (String)

    id of the from node

  • payload (Hash)

Returns:

  • (Hash)

    API response

Raises:

See Also:



60
61
62
63
# File 'lib/synapse_pay_rest/api/transactions.rb', line 60

def create(user_id:, node_id:, payload:, idempotency_key: nil)
  path = create_transaction_path(user_id: user_id, node_id: node_id)
  client.post(path, payload, idempotency_key: idempotency_key)
end

#delete(user_id:, node_id:, trans_id:) ⇒ Hash

Sends a DELETE request to /trans endpoint to cancel a transaction. Returns the response.

HTTP response from API

Parameters:

  • user_id (String)
  • node_id (String)

    id of the from node

  • trans_id (String)

    id of a transaction to delete

Returns:

  • (Hash)

    API response

Raises:



94
95
96
97
# File 'lib/synapse_pay_rest/api/transactions.rb', line 94

def delete(user_id:, node_id:, trans_id:)
  path = create_transaction_path(user_id: user_id, node_id: node_id, trans_id: trans_id)
  client.delete(path)
end

#get(user_id:, node_id:, trans_id: nil, **options) ⇒ Hash

TODO:

Probably should use CGI or RestClient’s param builder instead of

Sends a GET request to /trans endpoint. Queries a specific transaction_id if trans_id supplied, else queries all transactions. Returns the response.

HTTP response from API

rolling our own, probably error-prone and untested version github.com/rest-client/rest-client#usage-raw-url

Parameters:

  • user_id (String)
  • node_id (String)

    id of the from node

  • trans_id (String, void) (defaults to: nil)

    (optional) id of a transaction to look up

  • page (String, Integer)

    (optional) response will default to 1

  • per_page (String, Integer)

    (optional) response will default to 20

Returns:

  • (Hash)

    API response

Raises:



37
38
39
40
41
42
43
44
45
46
# File 'lib/synapse_pay_rest/api/transactions.rb', line 37

def get(user_id:, node_id:, trans_id: nil, **options)
  path = create_transaction_path(user_id: user_id, node_id: node_id, trans_id: trans_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(user_id:, node_id:, trans_id:, payload:) ⇒ Hash

Sends a PATCH request to /trans endpoint to update a transaction. Returns the response.

HTTP response from API

Parameters:

  • user_id (String)
  • node_id (String)

    id of the from node

  • trans_id (String)

    id of a transaction to update

  • payload (Hash)

Returns:

  • (Hash)

    API response

Raises:

See Also:



78
79
80
81
# File 'lib/synapse_pay_rest/api/transactions.rb', line 78

def update(user_id:, node_id:, trans_id:, payload:)
  path = create_transaction_path(user_id: user_id, node_id: node_id, trans_id: trans_id)
  client.patch(path, payload)
end