Class: ChartMogul::SubscriptionEvent

Inherits:
APIResource show all
Includes:
API::Actions::Custom, API::Actions::DestroyWithParams
Defined in:
lib/chartmogul/subscription_event.rb

Constant Summary

Constants inherited from APIResource

APIResource::BACKOFF_FACTOR, APIResource::INTERVAL, APIResource::INTERVAL_RANDOMNESS, APIResource::MAX_INTERVAL, APIResource::RETRY_EXCEPTIONS, APIResource::RETRY_STATUSES, APIResource::THREAD_CONNECTION_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API::Actions::DestroyWithParams

#destroy_with_params!, included

Methods included from API::Actions::Custom

#custom!, #custom_without_assign!, included

Methods inherited from APIResource

build_connection, build_query_path, connection, #custom_with_query_params!, custom_with_query_params!, extract_query_params, #extract_query_params, handle_other_error, handle_request_error, handling_errors, immutable_keys, json_patch, json_post, json_put, #path_with_query_params, query_params, set_immutable_keys, set_resource_name, set_resource_path, set_resource_root_key, writeable_query_param

Methods inherited from Object

#allowed_for_write?, #assign_all_attributes, #assign_writeable_attributes, attributes, define_private_writer, define_reader, define_writer, #initialize, #instance_attributes, new_from_json, readonly_attr, #serialize_for_write, #serialized_value_for_attr, writeable_attr, writeable_attributes

Constructor Details

This class inherits a constructor from ChartMogul::Object

Class Method Details

.all(options = {}) ⇒ Object



33
34
35
# File 'lib/chartmogul/subscription_event.rb', line 33

def self.all(options = {})
  SubscriptionEvents.all(options)
end

.create!(attributes) ⇒ Object

This endpoint requires we send the attributes as: { subscription_event: { key: value }} So we do not include the API::Actions::Create module here and rather use a variation of the method there to accommodate this difference in behaviour.



45
46
47
# File 'lib/chartmogul/subscription_event.rb', line 45

def self.create!(attributes)
  custom_with_query_params!(:post, { subscription_event: attributes }, :subscription_event)
end

.destroy!(params = {}) ⇒ Object

Class method: accepts both flat and envelope-wrapped params for backwards compatibility

flat params.           : SubscriptionEvent.destroy!(id: 123)
envelope-wrapped params: SubscriptionEvent.destroy!(subscription_event: { id: 123 })


64
65
66
67
68
69
70
# File 'lib/chartmogul/subscription_event.rb', line 64

def self.destroy!(params = {})
  body = params.key?(:subscription_event) ? params : { subscription_event: params }
  handling_errors do
    connection.delete(resource_path.path, body)
  end
  true
end

.destroy_by_external_id!(data_source_uuid:, external_id:, handle_as_user_edit: nil) ⇒ Object

Delete a subscription event by data_source_uuid and external_id



98
99
100
101
102
103
104
# File 'lib/chartmogul/subscription_event.rb', line 98

def self.destroy_by_external_id!(data_source_uuid:, external_id:, handle_as_user_edit: nil)
  path = build_query_path(resource_path.path, handle_as_user_edit: handle_as_user_edit)
  handling_errors do
    connection.delete(path, subscription_event: { data_source_uuid: data_source_uuid, external_id: external_id })
  end
  true
end

.toggle_disabled_by_external_id!(data_source_uuid:, external_id:, disabled:, handle_as_user_edit: nil) ⇒ Object

Toggle disabled state of a subscription event by data_source_uuid and external_id. Uses envelope-wrapped body ({ subscription_event: … }) as required by this API endpoint.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chartmogul/subscription_event.rb', line 108

def self.toggle_disabled_by_external_id!(data_source_uuid:, external_id:, disabled:, handle_as_user_edit: nil)
  path = build_query_path("#{resource_path.path}/disabled_state", handle_as_user_edit: handle_as_user_edit)
  resp = handling_errors do
    json_patch(path, {
                 subscription_event: { data_source_uuid: data_source_uuid, external_id: external_id },
                 disabled: disabled
               })
  end
  json = ChartMogul::Utils::JSONParser.parse(resp.body, immutable_keys:)
  new_from_json(json[:subscription_event] || json)
end

.update_by_external_id!(data_source_uuid:, external_id:, handle_as_user_edit: nil, **attributes) ⇒ Object

Update a subscription event by data_source_uuid and external_id. Uses envelope-wrapped body ({ subscription_event: … }) as required by this API endpoint.



88
89
90
91
92
93
94
95
# File 'lib/chartmogul/subscription_event.rb', line 88

def self.update_by_external_id!(data_source_uuid:, external_id:, handle_as_user_edit: nil, **attributes)
  path = build_query_path(resource_path.path, handle_as_user_edit: handle_as_user_edit)
  resp = handling_errors do
    json_patch(path, { subscription_event: attributes.merge(data_source_uuid: data_source_uuid, external_id: external_id) })
  end
  json = ChartMogul::Utils::JSONParser.parse(resp.body, immutable_keys:)
  new_from_json(json[:subscription_event] || json)
end

Instance Method Details

#create!Object



37
38
39
# File 'lib/chartmogul/subscription_event.rb', line 37

def create!
  custom_with_query_params!(:post, { subscription_event: instance_attributes }, :subscription_event)
end

#destroy!Object

Instance method: destroys this subscription event



55
56
57
58
59
# File 'lib/chartmogul/subscription_event.rb', line 55

def destroy!
  handling_errors do
    connection.delete(resource_path.path, subscription_event: { id: instance_attributes[:id] })
  end
end

#toggle_disabled!(disabled:, handle_as_user_edit: nil) ⇒ Object

Toggle the disabled state of a subscription event. Note: SubscriptionEvent uses envelope-wrapped requests/responses ({ subscription_event: … }) unlike other resources, because the API requires this format for this endpoint.



75
76
77
78
79
80
81
82
83
84
# File 'lib/chartmogul/subscription_event.rb', line 75

def toggle_disabled!(disabled:, handle_as_user_edit: nil)
  path = self.class.build_query_path(
    "#{resource_path.path}/#{instance_attributes[:id]}/disabled_state",
    handle_as_user_edit: handle_as_user_edit
  )
  resp = handling_errors { self.class.json_patch(path, { disabled: disabled }) }
  json = ChartMogul::Utils::JSONParser.parse(resp.body, immutable_keys: self.class.immutable_keys)
  assign_all_attributes(json[:subscription_event] || json)
  self
end

#update!(attrs) ⇒ Object



49
50
51
52
# File 'lib/chartmogul/subscription_event.rb', line 49

def update!(attrs)
  custom_with_query_params!(:patch, { subscription_event: attrs.merge(id: instance_attributes[:id]) },
                            :subscription_event)
end