Class: SynapsePayRest::Subscription
- Inherits:
-
Object
- Object
- SynapsePayRest::Subscription
- Defined in:
- lib/synapse_pay_rest/models/subscription/subscription.rb
Overview
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
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#is_active ⇒ Object
readonly
Returns the value of attribute is_active.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.all(client:, page: nil, per_page: nil) ⇒ Array<SynapsePayRest::Subscription>
Queries the API for all subscriptions and returns them as Subscription instances.
-
.create(client:, url:, scope:, **options) ⇒ SynapsePayRest::Subscription
Creates a new subscription in the API and returns a Subscription instance from the response data.
-
.find(client:, id:) ⇒ SynapsePayRest::Subscription
Queries the API for a subscription by id and returns a Subscription instances if found.
-
.from_response(response) ⇒ Object
Creates a Subscription from a response hash.
-
.multiple_from_response(response) ⇒ Object
Calls from_response on each member of a response collection.
-
.update(client:, id:, **options) ⇒ SynapsePayRest::Subscription
Updates the given key value pairs.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks if two Subscription instances have same id (different instances of same record).
-
#initialize(**options) ⇒ Subscription
constructor
A new instance of Subscription.
Constructor Details
#initialize(**options) ⇒ Subscription
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(**) .each { |key, value| instance_variable_set("@#{key}", value) } end |
Instance Attribute Details
#id ⇒ Object (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_active ⇒ Object (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 |
#scope ⇒ Object (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 |
#url ⇒ Object (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.
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.
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:, **) 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, **) 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.
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
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.
77 78 79 80 81 82 83 |
# File 'lib/synapse_pay_rest/models/subscription/subscription.rb', line 77 def update(client:, id:, **) raise ArgumentError, 'client must be a SynapsePayRest::Client' unless client.is_a?(Client) payload = payload_for_update() 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 |