Sha256: 367f68d1fb3632dc4f1c75f2e8721810b3bfa6aa34efca19c368192bf399d9d1

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'pina/models/address'
require 'pina/models/contact_bank_account'
require 'pina/models/contact'
require 'pina/collections/contact'

module Pina
  class Contact
    class << self
      def new(params = nil)
        Pina::Models::Contact.new(params)
      end

      def find_by(hash)
        response = where(hash)

        return response.items.first if response.is_a? Pina::Collections::Contact

        response
      end

      def where(hash, _page = nil)
        response = Pina::RestAdapter.get(:contacts, hash)

        return Pina::Collections::Contact.new(attributes(response)) if
          response.ok?

        response
      end

      def find(id)
        response = Pina::RestAdapter.get(:contacts, id)

        return Pina::Models::Contact.new(attributes(response)) if response.ok?

        response
      end

      def all(page = nil)
        response = Pina::RestAdapter.get(:contacts, page)

        return Pina::Collections::Contact.new(attributes(response)) if
          response.ok?

        response
      end

      def create(contact)
        response = Pina::RestAdapter.post(:contacts, contact)

        return Pina::Models::Contact.new(attributes(response)) if response.ok?

        response
      end

      def update(id, contact)
        response = Pina::RestAdapter.patch(:contacts, id, contact)

        return Pina::Models::Contact.new(attributes(response)) if response.ok?

        response
      end

      private

      def attributes(response)
        response.to_hash.merge(response: response)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pina-0.14.4 lib/pina/contact.rb
pina-0.14.3 lib/pina/contact.rb
pina-0.14.2 lib/pina/contact.rb