Class: Printavo::Resources::Contacts

Inherits:
Base
  • Object
show all
Defined in:
lib/printavo/resources/contacts.rb

Constant Summary collapse

ALL_QUERY =
File.read(File.join(__dir__, '../graphql/contacts/all.graphql')).freeze
FIND_QUERY =
File.read(File.join(__dir__, '../graphql/contacts/find.graphql')).freeze
CREATE_MUTATION =
File.read(File.join(__dir__, '../graphql/contacts/create.graphql')).freeze
DELETE_MUTATION =
File.read(File.join(__dir__, '../graphql/contacts/delete.graphql')).freeze
UPDATE_MUTATION =
File.read(File.join(__dir__, '../graphql/contacts/update.graphql')).freeze
MAX_PAGE_SIZE =
100
PAGE_FILTERS =
%i[primary_only query sort_descending sort_on].freeze

Instance Method Summary collapse

Methods inherited from Base

#all_pages, #each_page, #initialize

Constructor Details

This class inherits a constructor from Printavo::Resources::Base

Instance Method Details

#all(first: 25, after: nil, **filters) ⇒ Object



16
17
18
# File 'lib/printavo/resources/contacts.rb', line 16

def all(first: 25, after: nil, **filters)
  page(first: first, after: after, **filters).records
end

#create(**input) ⇒ Printavo::Contact

Creates a new contact. Requires at minimum email:.

Examples:

client.contacts.create(first_name: "Jane", last_name: "Smith",
                       email: "jane@example.com", phone: "555-867-5309")

Returns:



52
53
54
55
# File 'lib/printavo/resources/contacts.rb', line 52

def create(**input)
  data = @graphql.mutate(CREATE_MUTATION, variables: { input: camelize_keys(input) })
  Printavo::Contact.new(data['contactCreate'])
end

#delete(id) ⇒ nil

Permanently deletes a contact by ID.

Examples:

client.contacts.delete("123")

Parameters:

  • id (String, Integer)

Returns:

  • (nil)


77
78
79
80
# File 'lib/printavo/resources/contacts.rb', line 77

def delete(id)
  @graphql.mutate(DELETE_MUTATION, variables: { id: id.to_s })
  nil
end

#find(id) ⇒ Printavo::Contact

Finds a contact by ID.

Examples:

client.contacts.find("123")

Parameters:

  • id (String, Integer)

Returns:



40
41
42
43
# File 'lib/printavo/resources/contacts.rb', line 40

def find(id)
  data = @graphql.query(FIND_QUERY, variables: { id: id.to_s })
  Printavo::Contact.new(data['contact'])
end

#page(first: 25, after: nil, **filters) ⇒ Printavo::Page

Fetches one bounded contact page while preserving sanitized partial GraphQL errors. This method never follows the next cursor.

Returns:



24
25
26
27
28
29
30
31
# File 'lib/printavo/resources/contacts.rb', line 24

def page(first: 25, after: nil, **filters)
  validate_page!(first: first, filters: filters)
  envelope = @graphql.query_envelope(
    ALL_QUERY,
    variables: contact_variables(first: first, after: after, filters: filters)
  )
  build_page(envelope)
end

#update(id, **input) ⇒ Printavo::Contact

Updates an existing contact by ID.

Examples:

client.contacts.update("123", phone: "555-999-0000")

Parameters:

  • id (String, Integer)

Returns:



64
65
66
67
68
# File 'lib/printavo/resources/contacts.rb', line 64

def update(id, **input)
  data = @graphql.mutate(UPDATE_MUTATION,
                         variables: { id: id.to_s, input: camelize_keys(input) })
  Printavo::Contact.new(data['contactUpdate'])
end