Sha256: e2c19feac3312b440f1f7c7dd9240ff74570af41793e2109d5bcc6b82f640081

Contents?: true

Size: 812 Bytes

Versions: 1

Compression:

Stored size: 812 Bytes

Contents

module Fortnox
  module API
    module Repository
      module Savers

        def save( entity )
          return true if entity.saved?

          hash = entity_to_hash( entity )

          return save_new( hash ) if entity.new?
          update_existing( hash )
        end

      private

        def save_new( hash )
          post( @options.uri, { body: hash.to_json } )
        end

        def update_existing( hash )
          put(
              entity_url( hash ),
              { body: hash.to_json }
            )
        end

        def entity_url( hash )
          id = cut_id_from_hash( hash )
          "#{ @options.uri }#{ id }"
        end

        def cut_id_from_hash( hash )
          hash[ @options.json_entity_wrapper ].delete( @options.unique_id )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fortnox-api-0.1.0 lib/fortnox/api/repositories/base/savers.rb