Class: SynapsePayRest::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_pay_rest/models/subscription/subscription.rb

Overview

TODO:

use mixins to remove duplication between Node and BaseNode.

Represents a subscription record and holds methods for creating subscription instances from API calls. This is built on top of the SynapsePayRest::Subscription class and is intended to make it easier to use the API without knowing payload formats or knowledge of REST.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Subscription

Note:

Do not call directly. Use Subscription.create or other class method to instantiate via API action.

Returns a new instance of Subscription.



129
130
131
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 129

def initialize(**options)
  options.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 9

def id
  @id
end

#is_activeObject (readonly)

Returns the value of attribute is_active.



9
10
11
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 9

def is_active
  @is_active
end

#scopeObject (readonly)

Returns the value of attribute scope.



9
10
11
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 9

def scope
  @scope
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 9

def url
  @url
end

Class Method Details

.all(client:, page: nil, per_page: nil) ⇒ Array<SynapsePayRest::Subscription>

Queries the API for all subscriptions and returns them as Subscription instances.

Parameters:

  • client (SynapsePayRest::Client)
  • page (String, Integer) (defaults to: nil)

    (optional) response will default to 1

  • per_page (String, Integer) (defaults to: nil)

    (optional) response will default to 20

Returns:

Raises:



57
58
59
60
61
62
63
64
65
66
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 57

def all(client:, page: nil, per_page: nil)
  raise ArgumentError, 'client must be a SynapsePayRest::Client' unless client.is_a?(Client)
  [page, per_page].each do |arg|
    if arg && (!arg.is_a?(Integer) || arg < 1)
      raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
    end
  end
  response = client.subscriptions.get(page: page, per_page: per_page)
  multiple_from_response(response['subscriptions'])
end

.create(client:, url:, scope:, **options) ⇒ SynapsePayRest::Subscription

Creates a new subscription in the API and returns a Subscription instance from the response data.

Parameters:

Returns:

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 22

def create(client:, url:, scope:, **options)
  raise ArgumentError, 'client must be a SynapsePayRest::Client' unless client.is_a?(Client)
  raise ArgumentError, 'url must be a String' unless url.is_a? String
  raise ArgumentError, 'scope must be an Array' unless scope.is_a? Array
  
  payload = payload_for_create(url: url, scope: scope, **options)
  response = client.subscriptions.create(payload: payload)
  from_response(response)
end

.find(client:, id:) ⇒ SynapsePayRest::Subscription

Queries the API for a subscription by id and returns a Subscription instances if found.

Parameters:

Returns:

Raises:



40
41
42
43
44
45
46
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 40

def find(client:, id:)
  raise ArgumentError, 'client must be a SynapsePayRest::Client' unless client.is_a?(Client)
  raise ArgumentError, 'id must be a String' unless id.is_a?(String)

  response = client.subscriptions.get(subscription_id: id)
  from_response(response)
end

.from_response(response) ⇒ Object

Note:

Shouldn’t need to call this directly.

Creates a Subscription from a response hash.



88
89
90
91
92
93
94
95
96
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 88

def from_response(response)
  args = {
    id:            response['_id'],
    is_active:     response['is_active'],
    scope:         response['scope'],
    url:           response['url']
  }
  self.new(args)
end

.multiple_from_response(response) ⇒ Object

Calls from_response on each member of a response collection.



99
100
101
102
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 99

def multiple_from_response(response)
  return [] if response.empty?
  response.map { |subscription_data| from_response(subscription_data)}
end

.update(client:, id:, **options) ⇒ SynapsePayRest::Subscription

Updates the given key value pairs.

Parameters:

  • is_active (boolean)
  • url (String)
  • scope (Array<String>)

Returns:

Raises:



77
78
79
80
81
82
83
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 77

def update(client:, id:, **options)
  raise ArgumentError, 'client must be a SynapsePayRest::Client' unless client.is_a?(Client)
  
  payload = payload_for_update(options)
  response = client.subscriptions.update(subscription_id: id, payload: payload)
  from_response(response)
end

Instance Method Details

#==(other) ⇒ Object

Checks if two Subscription instances have same id (different instances of same record).



134
135
136
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 134

def ==(other)
  other.instance_of?(self.class) && !id.nil? && id == other.id
end