Method: GoodData::Subscription.create

Defined in:
lib/gooddata/models/subscription.rb

.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