Class: GoodData::Subscription

Inherits:
Rest::Resource show all
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

Attributes inherited from Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rest::Object

client, default_client, #saved?

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::DataGetter

#data

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

#initialize(json) ⇒ Subscription

Initializes object instance from raw wire JSON

Parameters:

  • json

    Json used for initialization



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

#channelsObject

Returns the value of attribute channels.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def channels
  @channels
end

#messageObject

Returns the value of attribute message.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def message
  @message
end

#processObject

Returns the value of attribute process.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def process
  @process
end

#project_eventsObject

Returns the value of attribute project_events.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def project_events
  @project_events
end

#subjectObject

Returns the value of attribute subject.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def subject
  @subject
end

#timer_eventObject

Returns the value of attribute timer_event.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def timer_event
  @timer_event
end

#titleObject

Returns the value of attribute title.



43
44
45
# File 'lib/gooddata/models/subscription.rb', line 43

def title
  @title
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.]
  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 }

  options = { message: 'Email body', subject: 'Email subject', title: c.user.email }.merge(opts)
  subscription = create_object(options.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.message &&
    @subject == other.subject &&
    @project_events == other.project_events &&
    @timer_event == other.timer_event
end

#deleteObject



172
173
174
# File 'lib/gooddata/models/subscription.rb', line 172

def delete
  client.delete uri
end

#obj_idObject Also known as: subscription_id



166
167
168
# File 'lib/gooddata/models/subscription.rb', line 166

def obj_id
  uri.split('/').last
end

#saveObject



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'] = message
                 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.], raw_data)
             end
  @json = client.get response['subscription']['meta']['uri']
  self
end

#uriObject



162
163
164
# File 'lib/gooddata/models/subscription.rb', line 162

def uri
  data['meta']['uri'] if data && data['meta'] && data['meta']['uri']
end