Module: Moneybird::Traits::Save

Instance Method Summary collapse

Instance Method Details

#create(attributes) ⇒ Object Also known as: update



19
20
21
22
23
# File 'lib/moneybird/traits/save.rb', line 19

def create(attributes)
  build(attributes).tap do |resource|
    save(resource)
  end
end

#resource_path(resource) ⇒ Object



4
5
6
# File 'lib/moneybird/traits/save.rb', line 4

def resource_path(resource)
  [path, resource.path].join('')
end

#save(resource) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/moneybird/traits/save.rb', line 8

def save(resource)
  response =
    if resource.persisted?
      client.patch(resource_path(resource), resource.to_json)
    else
      client.post(resource_path(resource), resource.to_json)
    end
  resource.attributes = response
  resource
end