Class: GoodData::ScheduledMail

Inherits:
MdObject show all
Includes:
Mixin::Lockable
Defined in:
lib/gooddata/models/metadata/scheduled_mail.rb

Constant Summary collapse

DEFAULT_OPTS =
{
  # Meta options
  :title => 'Scheduled report example',
  :summary => 'Daily at 12:00pm PT',
  :tags => '',
  :deprecated => 0,

  # Content When options
  :recurrency => '0:0:0:1*12:0:0',
  :startDate => '2012-06-05',
  :timeZone => 'America/Los_Angeles',

  # Content Email options
  :to => [],
  :bcc => [],
  :subject => 'Scheduled Report',
  :body => "Hey, I'm sending you new Reports and Dashboards!",

  # Attachments
  :attachments => []
}

Constants inherited from MdObject

MdObject::IDENTIFIERS_CFG, MdObject::MD_OBJ_CTG

Constants included from Mixin::MdIdToUri

Mixin::MdIdToUri::IDENTIFIERS_CFG

Constants included from Mixin::MdObjectIndexer

Mixin::MdObjectIndexer::MD_OBJ_CTG

Constants included from Mixin::MdObjectQuery

Mixin::MdObjectQuery::ERROR_MESSAGE_NO_PROJECT

Instance Attribute Summary

Attributes inherited from Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Lockable

#lock, #lock!, #lock_with_dependencies!, #locked?, #unlock, #unlock!, #unlock_with_dependencies!, #unlocked?

Methods inherited from MdObject

#==, #add_tag, #browser_uri, #delete, #deprecated, #deprecated=, find_replaceable_values, #get_flag?, #initialize, #listed?, #production, #production=, #project, #reload!, #remove_tag, replace, #replace, #replace!, replace_bracketed, replace_quoted, #restricted, #restricted=, #save, #save_as, #set_flag, #tag_set, #unlisted, #unlisted=, #validate

Methods included from Mixin::MdIdToUri

#identifier_to_uri

Methods included from Mixin::MdObjectIndexer

#[]

Methods included from Mixin::MdObjectQuery

#all, #dependency, #dependency?, #query, #usedby, #usedby?, #using, #using?

Methods included from Mixin::MdFinders

#find_by_identifier, #find_by_tag, #find_by_title, #find_first_by_identifier, #find_first_by_title

Methods included from Mixin::MdObjId

#uri_obj_id

Methods included from Mixin::MdGrantees

#change_permission, #grant, #grantees, #revoke

Methods included from Mixin::MdRelations

#dependency, #dependency?, #usedby, #usedby?, #using, #using?

Methods included from Mixin::ObjId

#obj_id

Methods included from Mixin::Links

#links

Methods inherited from Rest::Resource

#initialize

Methods inherited from Rest::Object

client, default_client, #initialize, #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

This class inherits a constructor from GoodData::MdObject

Class Method Details

.all(options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Array<GoodData::MdObject> | Array<Hash>

Method intended to get all objects of that type in a specified project

to pull in full objects. This is desirable from the usability POV but unfortunately has negative impact on performance so it is not the default

Parameters:

  • options (Hash) (defaults to: { :client => GoodData.connection, :project => GoodData.project })

    the options hash

Options Hash (options):

  • :full (Boolean)

    if passed true the subclass can decide

Returns:

  • (Array<GoodData::MdObject> | Array<Hash>)

    Return the appropriate metadata objects or their representation



56
57
58
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 56

def all(options = { :client => GoodData.connection, :project => GoodData.project })
  query('scheduledMail', ScheduledMail, options)
end

.convert_attachment(item, opts) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 60

def convert_attachment(item, opts)
  if item.is_a?(GoodData::Dashboard)
    {
      dashboardAttachment: GoodData::DashboardAttachment::DEFAULT_OPTS.merge(opts.merge(:uri => item.uri))
    }
  elsif item.is_a?(GoodData::Report)
    {
      reportAttachment: GoodData::ReportAttachment::DEFAULT_OPTS.merge(opts.merge(:uri => item.uri))
    }
  elsif item.is_a?(GoodData::DashboardAttachment)
    item.json
  elsif item.is_a?(GoodData::ReportAttachment)
    item.json
  elsif item.is_a?(Hash)
    item
  elsif item == 'dashboardAttachment'
    {
      dashboardAttachment: GoodData::DashboardAttachment::DEFAULT_OPTS.merge(opts)
    }
  elsif item == 'reportAttachment'
    {
      reportAttachment: GoodData::ReportAttachment::DEFAULT_OPTS.merge(opts)
    }
  end
end

.create(options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 86

def create(options = { :client => GoodData.connection, :project => GoodData.project })
  client = options[:client]
  fail ArgumentError, 'No :client specified' if client.nil?

  p = options[:project]
  fail ArgumentError, 'No :project specified' if p.nil?

  project = client.projects(p)
  fail ArgumentError, 'Wrong :project specified' if project.nil?

  opts = GoodData::ScheduledMail::DEFAULT_OPTS.merge(GoodData::Helpers.symbolize_keys(options))

  scheduled_mail = {
    :scheduledMail => {
      :meta => {
        :title => opts[:title],
        :summary => opts[:summary],
        :tags => opts[:tags],
        :deprecated => opts[:deprecated]
      },
      :content => {
        :when => {
          :recurrency => opts[:recurrency],
          :startDate => opts[:startDate] || opts[:start_date],
          :timeZone => opts[:timeZone] || opts[:time_zone] || opts[:timezone]
        },
        :to => opts[:to].is_a?(Array) ? opts[:to] : [opts[:to]],
        :bcc => opts[:bcc].is_a?(Array) ? opts[:bcc] : [opts[:bcc]],
        :subject => opts[:subject],
        :body => opts[:body]
      }
    }
  }

  attachments = opts[:attachments].map do |attachment|
    key = attachment.keys.first
    body = attachment[key]

    ScheduledMail.convert_attachment(key, body)
  end

  scheduled_mail[:scheduledMail][:content][:attachments] = attachments

  client.create(ScheduledMail, GoodData::Helpers.stringify_keys(scheduled_mail), :project => project)
end

Instance Method Details

#add_attachment(item, opts) ⇒ Array

Add attachment

Parameters:

  • item (String | Object)

    Schedule to add

  • opts (Hash)

    Optional schedule options

Returns:

  • (Array)

    New list of attachments



138
139
140
141
142
143
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 138

def add_attachment(item, opts)
  attachment = ScheduledMail.convert_attachment(item, opts)
  fail ArgumentError unless attachment

  content['attachments'] << attachment
end

#add_attachment!(item, opts) ⇒ Array

Add attachment and save

Parameters:

  • item (String | Object)

    Schedule to add

  • opts (Hash)

    Optional schedule options

Returns:

  • (Array)

    New list of attachments



150
151
152
153
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 150

def add_attachment!(item, opts)
  add_attachment(item, opts)
  save!
end

#attachmentsArray<GoodData::DashboardAttachment | GoodData::ReportAttachment>

Get attachments as objects

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 158

def attachments
  content['attachments'].map do |attachment|
    key = attachment.keys.first

    if key == 'dashboardAttachment'
      GoodData::DashboardAttachment.new(self, attachment)
    elsif key == 'reportAttachment'
      GoodData::ReportAttachment.new(self, attachment)
    else
      RuntimeError "Unsupported attachment type: #{key}"
    end
  end
end

#bodyString

Get body

Returns:

  • (String)

    Scheduled email body



175
176
177
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 175

def body
  content['body']
end

#body=(new_body) ⇒ String

Set body

Parameters:

  • new_body (String)

    New body to be set

Returns:

  • (String)

    New body



183
184
185
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 183

def body=(new_body)
  content['body'] = new_body
end

#recurrencyString

Get recurrency string

Returns:

  • (String)

    Recurrency (cron) string



190
191
192
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 190

def recurrency
  content['when']['recurrency']
end

#recurrency=(new_recurrency) ⇒ Hash

Set recurrency

Parameters:

  • new_recurrency (String)

    New recurrency to be set

Returns:

  • (Hash)

    New recurrency



198
199
200
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 198

def recurrency=(new_recurrency)
  content['when']['recurrency'] = new_recurrency
end

#start_dateString

Get start date

Returns:

  • (String)

    Start date



205
206
207
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 205

def start_date
  content['when']['startDate']
end

#start_date=(new_start_date) ⇒ String

Set start date

Parameters:

  • new_start_date (String)

    New start date to be set

Returns:

  • (String)

    New start date



213
214
215
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 213

def start_date=(new_start_date)
  content['when']['startDate'] = new_start_date
end

#subjectString

Get subject

Returns:

  • (String)

    Subject of scheduled email



220
221
222
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 220

def subject
  content['subject']
end

#subject=(new_subject) ⇒ String

Set subject

Parameters:

  • new_subject (String)

    New subject to be set

Returns:

  • (String)

    New subject



228
229
230
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 228

def subject=(new_subject)
  content['subject'] = new_subject
end

#timezoneString

Get timezone

Returns:

  • (String)

    Timezone



235
236
237
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 235

def timezone
  content['when']['timeZone']
end

#timezone=(new_timezone) ⇒ String

Set timezone

Parameters:

  • new_timezone (String)

    New timezone string to be set

Returns:

  • (String)

    New timezone



243
244
245
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 243

def timezone=(new_timezone)
  content['when']['timeZone'] = new_timezone
end

#toString

Get recipients

Returns:

  • (String)

    Recipients of email



250
251
252
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 250

def to
  content['to']
end

#to=(new_to) ⇒ Array<String>

Set recipients

Parameters:

  • new_to (String|Array<String>)

    New recipients to be set

Returns:

  • (Array<String>)

    New recipient list



258
259
260
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 258

def to=(new_to)
  content['to'] = new_to.is_a?(Array) ? new_to : [new_to]
end

#whenHash

Get 'when' section

Returns:

  • (Hash)

    'when' section from json



265
266
267
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 265

def when
  content['when']
end

#when=(new_when) ⇒ Hash

Set 'when' section

Parameters:

  • new_when (Hash)

    New 'when' section to be set

Returns:

  • (Hash)

    New 'when' section



273
274
275
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 273

def when=(new_when)
  content['when'] = new_when
end