Class: BlueStateDigital::Event

Inherits:
ApiDataModel show all
Defined in:
lib/blue_state_digital/event.rb

Defined Under Namespace

Classes: EventSaveValidationException

Constant Summary collapse

FIELDS =
[:event_id_obfuscated, :event_type_id, :creator_cons_id, :name, :description, :venue_name, :venue_country, :venue_zip, :venue_city, :venue_state_cd, :start_date, :end_date]

Constants inherited from ApiDataModel

ApiDataModel::FIELD

Instance Attribute Summary

Attributes inherited from ApiDataModel

#connection

Instance Method Summary collapse

Methods inherited from ApiDataModel

#initialize, #to_hash

Constructor Details

This class inherits a constructor from BlueStateDigital::ApiDataModel

Instance Method Details

#saveObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blue_state_digital/event.rb', line 16

def save
  if self.event_id_obfuscated.blank?
    response_json_text = connection.perform_request '/event/create_event', {accept: 'application/json', event_api_version: '2', values: self.to_json}, "POST"
  else
    response_json_text = connection.perform_request '/event/update_event', {accept: 'application/json', event_api_version: '2', values: self.to_json}, "POST"
  end

  response = JSON.parse(response_json_text)
  if response['validation_errors']
    raise EventSaveValidationException, response['validation_errors']
  else
    self.event_id_obfuscated = response['event_id_obfuscated']
  end

  self
end

#to_jsonObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/blue_state_digital/event.rb', line 33

def to_json
  event_attrs = self.event_id_obfuscated.blank? ? { } : { event_id_obfuscated: self.event_id_obfuscated }
  (FIELDS - [:event_id_obfuscated, :start_date, :end_date]).each do |field|
    event_attrs[field] = self.send(field)
  end

  duration_in_minutes = ((end_date - start_date) / 60).to_i
  day_attrs = { start_datetime_system: start_date.strftime('%Y-%m-%d %H:%M:%S %z'), duration: duration_in_minutes }
  event_attrs[:days] = [ day_attrs ]

  event_attrs.to_json
end