Sha256: 29b9527035c07f087c5cb4686b4bee30efa1deb67e7135f568e774d72cc4544f

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'json'
require_relative '../concerns/modelable'

module Immoscout
  module Models
    module Actions
      # Actions to work with contacts.
      module Contact
        extend ActiveSupport::Concern

        included do
          include Immoscout::Models::Concerns::Modelable

          self.unpack_collection = proc do |hash|
            hash
              .fetch('common.realtorContactDetailsList', {})
              .fetch('realtorContactDetails', nil)
          end

          def save
            response =
              if id
                api.put("user/#{api.user_name}/contact/#{id}", as_json)
              else
                api.post("user/#{api.user_name}/contact", as_json)
              end

            handle_response(response)
            self.id = id_from_response(response) unless id
            self
          end

          def destroy
            response = api.delete("user/#{api.user_name}/contact/#{id}")
            handle_response(response)
            self
          end
        end

        class_methods do
          def find(id)
            response = api.get("user/#{api.user_name}/contact/#{id}")
            handle_response(response)
            from_raw(response.body)
          end

          def find_by(hash)
            external_id = hash.symbolize_keys.fetch(:external_id)
            find("ext-#{external_id}")
          end

          def all
            response = api.get("user/#{api.user_name}/contact")
            handle_response(response)
            objects = unpack_collection.call(response.body)
            objects.map { |object| new(object) }
          end
          delegate :first, to: :all
          delegate :last, to: :all

          def create(hash)
            instance = new(hash)
            instance.save
            instance
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
immoscout-1.6.5 lib/immoscout/models/actions/contact.rb
immoscout-1.6.4 lib/immoscout/models/actions/contact.rb
immoscout-1.6.3 lib/immoscout/models/actions/contact.rb
immoscout-1.6.2 lib/immoscout/models/actions/contact.rb
immoscout-1.6.1 lib/immoscout/models/actions/contact.rb
immoscout-1.6.0 lib/immoscout/models/actions/contact.rb