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: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

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, #initialize, #listed?, #project, #reload!, #remove_tag, replace, #replace, #replace!, replace_bracketed, replace_quoted, #save, #save_as, #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

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 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

Returns:

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

    Return the appropriate metadata objects or their representation



53
54
55
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 53

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

.convert_attachment(item, opts) ⇒ Object



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

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



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 83

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



135
136
137
138
139
140
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 135

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



147
148
149
150
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 147

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

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

Get attachments as objects

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 155

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



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

def body
  content['body']
end

#body=(new_body) ⇒ String

Set body

Parameters:

  • new_body (String)

    New body to be set

Returns:



180
181
182
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 180

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

#recurrencyString

Get recurrency string

Returns:

  • (String)

    Recurrency (cron) string



187
188
189
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 187

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



195
196
197
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 195

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

#start_dateString

Get start date

Returns:



202
203
204
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 202

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:



210
211
212
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 210

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

#subjectString

Get subject

Returns:

  • (String)

    Subject of scheduled email



217
218
219
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 217

def subject
  content['subject']
end

#subject=(new_subject) ⇒ String

Set subject

Parameters:

  • new_subject (String)

    New subject to be set

Returns:



225
226
227
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 225

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

#timezoneString

Get timezone

Returns:



232
233
234
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 232

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

#timezone=(new_timezone) ⇒ String

Set timezone

Parameters:

  • new_timezone (String)

    New timezone string to be set

Returns:



240
241
242
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 240

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

#toString

Get recipients

Returns:

  • (String)

    Recipients of email



247
248
249
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 247

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



255
256
257
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 255

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



262
263
264
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 262

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



270
271
272
# File 'lib/gooddata/models/metadata/scheduled_mail.rb', line 270

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