Module: LHS::Item::Save

Extended by:
ActiveSupport::Concern
Included in:
LHS::Item
Defined in:
lib/lhs/concerns/item/save.rb

Instance Method Summary collapse

Instance Method Details

#save(options = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/lhs/concerns/item/save.rb', line 9

def save(options = nil)
  save!(options)
rescue LHC::Error => e
  self.errors = LHS::Errors.new(e.response)
  false
end

#save!(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lhs/concerns/item/save.rb', line 16

def save!(options = {})
  options = options.present? ? options.dup : {}
  data = _data._raw.dup
  if href.present?
    url = href
  else
    endpoint = endpoint_for_persistance(data, options)
    url = url_for_persistance(endpoint, data, options)
    endpoint.remove_interpolated_params!(data)
    endpoint.remove_interpolated_params!(options.fetch(:params, {}))
    options.merge!(endpoint.options.merge(options)) if endpoint.options
  end

  options = options.merge(method: :post, url: url, body: data.to_json)
  options[:headers] ||= {}
  options[:headers].merge!('Content-Type' => 'application/json')

  data = record_for_persistance.request(options)
  _data.merge_raw!(data)
  true
end