Class: GoodData::Subscription
- Inherits:
-
Rest::Resource
- Object
- Rest::Object
- Rest::Resource
- GoodData::Subscription
- Defined in:
- lib/gooddata/models/subscription.rb
Constant Summary collapse
- SUBSCRIPTION_PATH =
'/gdc/projects/%s/users/%s/subscriptions'
- EMPTY_OBJECT =
{ 'subscription' => { 'triggers' => [], 'condition' => { 'condition' => { 'expression' => 'true' } }, 'message' => { 'template' => { 'expression' => '' } }, 'subject' => { 'template' => { 'expression' => '' } }, 'channels' => [], 'meta' => { 'title' => '' } } }
Instance Attribute Summary collapse
-
#channels ⇒ Object
Returns the value of attribute channels.
-
#message ⇒ Object
Returns the value of attribute message.
-
#process ⇒ Object
Returns the value of attribute process.
-
#project_events ⇒ Object
Returns the value of attribute project_events.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#timer_event ⇒ Object
Returns the value of attribute timer_event.
-
#title ⇒ Object
Returns the value of attribute title.
Attributes inherited from Rest::Object
Class Method Summary collapse
- .[](id = :all, opts = { client: GoodData.connection }) ⇒ Object
- .all(opts = { client: GoodData.connection }) ⇒ Object
- .create(opts = { client: GoodData.connection }) ⇒ Object
- .create_object(data = {}) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #delete ⇒ Object
-
#initialize(json) ⇒ Subscription
constructor
Initializes object instance from raw wire JSON.
- #obj_id ⇒ Object (also: #subscription_id)
- #save ⇒ Object
- #uri ⇒ Object
Methods inherited from Rest::Object
client, default_client, #saved?
Methods included from Mixin::DataPropertyReader
Methods included from Mixin::DataPropertyWriter
Methods included from Mixin::MetaPropertyReader
Methods included from Mixin::MetaPropertyWriter
Methods included from Mixin::MetaGetter
Methods included from Mixin::DataGetter
Methods included from Mixin::RootKeyGetter
Methods included from Mixin::ContentGetter
Constructor Details
#initialize(json) ⇒ Subscription
Initializes object instance from raw wire JSON
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/gooddata/models/subscription.rb', line 115 def initialize(json) super @json = json @title = data['meta']['title'] @message = data['message']['template']['expression'] @subject = data['subject']['template']['expression'] @process = data['condition']['condition']['expression'][/'(.*)'/, 1] @channels = data['channels'] @project_events = data['triggers'].find { |h| h.keys.first == 'projectEventTrigger' }['projectEventTrigger']['types'] timer_events = data['triggers'].select { |h| h.keys.first == 'timerEvent' } timer_events && @timer_events = timer_events.map { |_, v| v } end |
Instance Attribute Details
#channels ⇒ Object
Returns the value of attribute channels.
43 44 45 |
# File 'lib/gooddata/models/subscription.rb', line 43 def channels @channels end |
#message ⇒ Object
Returns the value of attribute message.
43 44 45 |
# File 'lib/gooddata/models/subscription.rb', line 43 def @message end |
#process ⇒ Object
Returns the value of attribute process.
43 44 45 |
# File 'lib/gooddata/models/subscription.rb', line 43 def process @process end |
#project_events ⇒ Object
Returns the value of attribute project_events.
43 44 45 |
# File 'lib/gooddata/models/subscription.rb', line 43 def project_events @project_events end |
#subject ⇒ Object
Returns the value of attribute subject.
43 44 45 |
# File 'lib/gooddata/models/subscription.rb', line 43 def subject @subject end |
#timer_event ⇒ Object
Returns the value of attribute timer_event.
43 44 45 |
# File 'lib/gooddata/models/subscription.rb', line 43 def timer_event @timer_event end |
Class Method Details
.[](id = :all, opts = { client: GoodData.connection }) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gooddata/models/subscription.rb', line 46 def [](id = :all, opts = { client: GoodData.connection }) c = GoodData.get_client(opts) pid = (opts[:project].respond_to?(:pid) && opts[:project].pid) || opts[:project] uri = SUBSCRIPTION_PATH % [pid, c.user.account_setting_id] if id == :all data = c.get uri data['subscriptions']['items'].map { |subscription_data| c.create(Subscription, subscription_data, project: pid) } else c.create(Subscription, c.get("#{uri}/#{id}"), project: pid) end end |
.all(opts = { client: GoodData.connection }) ⇒ Object
58 59 60 |
# File 'lib/gooddata/models/subscription.rb', line 58 def all(opts = { client: GoodData.connection }) Subscription[:all, opts] end |
.create(opts = { client: GoodData.connection }) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gooddata/models/subscription.rb', line 62 def create(opts = { client: GoodData.connection }) c = GoodData.get_client(opts) [:project, :channels, :process, :project_events].each { |key| fail "No #{key.inspect} specified" unless opts[key] } pid = (opts[:project].respond_to?(:pid) && opts[:project].pid) || opts[:project] project_events = (opts[:project_events].respond_to?(:each) && opts[:project_events]) || [opts[:project_events]] process_id = (opts[:process].respond_to?(:process_id) && opts[:process].process_id) || opts[:process] condition = "params.PROCESS_ID=='#{process_id}'" channels = (opts[:channels].respond_to?(:each) && opts[:channels]) || [opts[:channels]] channel_uris = channels.map { |channel| channel.respond_to?(:uri) && channel.uri || channel } = { message: 'Email body', subject: 'Email subject', title: c.user.email }.merge(opts) subscription = create_object(.merge(channels: channel_uris, project_events: project_events, condition: condition, project: pid)) subscription.save subscription end |
.create_object(data = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gooddata/models/subscription.rb', line 82 def create_object(data = {}) c = GoodData.get_client(data) new_data = GoodData::Helpers.deep_dup(EMPTY_OBJECT).tap do |d| d['subscription']['condition']['condition']['expression'] = data[:condition] d['subscription']['message']['template']['expression'] = data[:message] d['subscription']['subject']['template']['expression'] = data[:subject] d['subscription']['meta']['title'] = data[:title] d['subscription']['channels'] = data[:channels] triggers = [] triggers << { 'projectEventTrigger' => { 'types' => data[:project_events] } } if data[:timer_event] triggers << { 'timerEvent' => { 'cronExpression' => data[:timer_event] } } end d['subscription']['triggers'] = triggers end c.create(Subscription, new_data, data) end |
Instance Method Details
#==(other) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/gooddata/models/subscription.rb', line 176 def ==(other) return false unless [:project, :title, :process, :channels, :message, :subject, :project_events, :timer_event].all? { |m| other.respond_to?(m) } @project == other.project && @title == other.title && @process == other.process && @channels == other.channels && @message == other. && @subject == other.subject && @project_events == other.project_events && @timer_event == other.timer_event end |
#delete ⇒ Object
172 173 174 |
# File 'lib/gooddata/models/subscription.rb', line 172 def delete client.delete uri end |
#obj_id ⇒ Object Also known as: subscription_id
166 167 168 |
# File 'lib/gooddata/models/subscription.rb', line 166 def obj_id uri.split('/').last end |
#save ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/gooddata/models/subscription.rb', line 130 def save response = if uri data_to_send = GoodData::Helpers.deep_dup(raw_data).tap do |d| d['subscription']['condition']['condition']['expression'] = "params.PROCESS_ID=='#{(process.respond_to?(:process_id) && process.process_id) || process}'" d['subscription']['message']['template']['expression'] = d['subscription']['subject']['template']['expression'] = subject d['subscription']['meta']['title'] = title d['subscription']['channels'] = ((channels.respond_to?(:each) && channels) || [channels]).map { |channel| channel.respond_to?(:uri) && channel.uri || channel } triggers = [] triggers << { 'projectEventTrigger' => { 'types' => (project_events.respond_to?(:each) && project_events) || [project_events] } } if timer_event triggers << { 'timerEvent' => { 'cronExpression' => timer_event } } end d['subscription']['triggers'] = triggers end client.put(uri, data_to_send) else client.post(SUBSCRIPTION_PATH % [project, client.user.account_setting_id], raw_data) end @json = client.get response['subscription']['meta']['uri'] self end |
#uri ⇒ Object
162 163 164 |
# File 'lib/gooddata/models/subscription.rb', line 162 def uri data['meta']['uri'] if data && data['meta'] && data['meta']['uri'] end |