Module: CleanModel::Remote::InstanceMethods

Defined in:
lib/clean_model/remote.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/clean_model/remote.rb', line 65

def destroy
  return true if new_record?
  begin
    response = delete
    unless response.is_a?(Net::HTTPSuccess)
      errors[:base] = response.content_type == 'application/json' ? response.body : "#{response.code} - Unexpected error"
    end
  rescue WebClient::Error => ex
    errors[:base] = ex.message
  end
  errors.empty?
end

#httpObject



37
38
39
# File 'lib/clean_model/remote.rb', line 37

def http
  self.class.http
end

#http_get(path, data = {}, &block) ⇒ Object



41
42
43
# File 'lib/clean_model/remote.rb', line 41

def http_get(path, data={}, &block)
  self.class.http_get(path, data, &block)
end

#saveObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/clean_model/remote.rb', line 50

def save
  return false unless valid?
  begin
    response = new_record? ? create : update
    if response.is_a?(Net::HTTPSuccess)
      save_success(response)
    else
      save_fail(response)
    end
  rescue WebClient::Error => ex
    errors[:base] = ex.message
  end
  errors.empty?
end

#wrapped_attributes(options = {}) ⇒ Object



45
46
47
48
# File 'lib/clean_model/remote.rb', line 45

def wrapped_attributes(options={})
  exceptions = options[:except] ? [options[:except]].flatten.map(&:to_sym) : []
  attributes.reject { |k, v| v.nil? || exceptions.include?(k) }.inject({}) { |h, (k, v)| h["#{options[:wrapper] || self.class.to_s.demodulize.underscore}[#{k}]"] = v; h }
end