Module: IContact::Model

Extended by:
ActiveModel::Naming, ActiveSupport::Concern
Included in:
Account, ClientFolder, Contact, List, Subscription
Defined in:
lib/i_contact/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject



105
106
107
108
109
# File 'lib/i_contact/model.rb', line 105

def destroy
  if persisted?
    connection.delete(path("#{self.class.resource_name.demodulize.pluralize}/#{self.attributes[self.class.key_attr.to_s]}"))
  end
end

#normalize_attrs(attrs) ⇒ Object



91
92
93
94
95
96
# File 'lib/i_contact/model.rb', line 91

def normalize_attrs(attrs)
  attrs.inject({}) do |hsh, pair|
    hsh[pair[0].underscore] = pair[1]
    hsh
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/i_contact/model.rb', line 124

def persisted?
  self.class.key_attr && self.attributes[self.class.key_attr.to_s].present?
end

#saveObject

end ClassMethods



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/i_contact/model.rb', line 70

def save
  if valid?
    if !persisted?
      raw_resp = connection.post(path(self.class.resource_name.demodulize.pluralize),
        serialized_attributes.to_json)
      response = IContact::Response.new(raw_resp)
      if response.valid? && response.warnings.empty?
        if response.parsed_response.values.first && response.parsed_response.values.first.first
          self.attributes = normalize_attrs(response.parsed_response.values.first.first)
        end
        true
      else
        @errors = response.errors + response.warnings
        false
      end
    else

    end
  end
end

#save!Object



98
99
100
101
102
103
# File 'lib/i_contact/model.rb', line 98

def save!
  unless save
    raise IContact::InvalidResource,  @errors.join(', ')
  end
  self
end

#serialized_attributesObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/i_contact/model.rb', line 111

def serialized_attributes
  result = attributes.inject({}) do |hsh, keypair|
    hsh[keypair[0].camelcase(:lower)] = keypair[1] unless keypair[1].nil?
    hsh
  end

  if !persisted?
    [result]
  else
    result
  end
end