Module: Crm

Defined in:
lib/crm.rb,
lib/crm/core.rb,
lib/crm/type.rb,
lib/crm/event.rb,
lib/crm/errors.rb,
lib/crm/account.rb,
lib/crm/contact.rb,
lib/crm/mailing.rb,
lib/crm/activity.rb,
lib/crm/collection.rb,
lib/crm/core/mixins.rb,
lib/crm/template_set.rb,
lib/crm/core/rest_api.rb,
lib/crm/event_contact.rb,
lib/crm/mailing_delivery.rb,
lib/crm/mailing_recipient.rb,
lib/crm/core/configuration.rb,
lib/crm/core/basic_resource.rb,
lib/crm/core/log_subscriber.rb,
lib/crm/core/item_enumerator.rb,
lib/crm/core/mixins/findable.rb,
lib/crm/core/attachment_store.rb,
lib/crm/core/mixins/modifiable.rb,
lib/crm/core/mixins/searchable.rb,
lib/crm/core/connection_manager.rb,
lib/crm/core/mixins/inspectable.rb,
lib/crm/core/search_configurator.rb,
lib/crm/core/mixins/change_loggable.rb,
lib/crm/core/mixins/attribute_provider.rb,
lib/crm/core/mixins/merge_and_deletable.rb

Defined Under Namespace

Modules: Core, Errors Classes: Account, Activity, Collection, Contact, Event, EventContact, Mailing, MailingDelivery, MailingRecipient, TemplateSet, Type

Class Method Summary collapse

Class Method Details

.autoload_module(mod, mod_source) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/crm.rb', line 25

def self.autoload_module(mod, mod_source)
  mod_dir = mod_source.gsub(/\.rb$/, '')
  Dir.glob("#{mod_dir}/*.rb").each do |file|
    name = File.basename(file, ".rb")
    mod.autoload name.camelcase, file
  end
end

.configure {|config| ... } ⇒ void

This method returns an undefined value.

Configures the JustRelate WebCRM SDK. The config keys tenant, login and api_key must be provided.

Examples:

Crm.configure do |config|
  config.tenant  = 'my_tenant'
  config.   = 'my_login'
  config.api_key = 'my_api_key'
end

Yield Parameters:



18
19
20
21
22
23
# File 'lib/crm.rb', line 18

def self.configure
  config = ::Crm::Core::Configuration.new
  yield config
  config.validate!
  Core::RestApi.instance = Core::RestApi.new(config.endpoint_uri, config., config.api_key)
end

.find(*ids) ⇒ Crm::Core::BasicResource, Crm::Core::ItemEnumerator

Fetches multiple items by ids. The base type of the items can be mixed (e.g. a Contact or Account).

Examples:

Crm.find('e70a7123f499c5e0e9972ab4dbfb8fe3')
# => Crm::Contact

Crm.find('e70a7123f499c5e0e9972ab4dbfb8fe3', '2185dd25c2f4fa41fbef422c1b9cfc38')
# => Crm::Core::ItemEnumerator

Crm.find(['e70a7123f499c5e0e9972ab4dbfb8fe3', '2185dd25c2f4fa41fbef422c1b9cfc38'])
# => Crm::Core::ItemEnumerator

Parameters:

  • ids (String, Array<String>)

    A single ID or a list of IDs.

Returns:

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/crm.rb', line 51

def self.find(*ids)
  flattened_ids = ids.flatten
  if flattened_ids.compact.blank?
    raise Crm::Errors::ResourceNotFound.new(
        "Items could not be found.", flattened_ids)
  end
  enumerator = Core::ItemEnumerator.new(flattened_ids)

  if ids.size == 1 && !ids.first.kind_of?(Array)
    enumerator.first
  else
    enumerator
  end
end

.search(filters: nil, query: nil, limit: :none, offset: 0, sort_by: nil, sort_order: nil) ⇒ Crm::Core::ItemEnumerator

Performs a search. Retrieves only IDs and passes them to an ItemEnumerator. The ItemEnumerator then fetches the items on demand.

The search considers the following base types:

Examples:

Crm.search([{field: 'last_name', condition: 'equals', value: 'Johnson'}])
# => Crm::Core::ItemEnumerator with all contacts with last name Johnson.

Crm.search(
  [
    {field: 'last_name', condition: 'equals', value: 'Johnson'},
    {field: 'locality', condition: 'equals', value: 'Boston'}
  ],
  limit: 20,
  offset: 10,
  sort_by: 'created_at',
  sort_order: 'desc'
)
# => Crm::Core::ItemEnumerator with max 20 contacts with last name Johnson from Boston.

Parameters:

  • filters (Array<Hash{String => String}>) (defaults to: nil)

    Array of filters, each filter is a hash with three properties: field, condition, value. Filters are AND expressions.

  • query (String) (defaults to: nil)

    The search term of a full-text search for words starting with the term (case-insensitive prefix search). Affects score.

  • limit (Fixnum) (defaults to: :none)

    The number of results to return at most. Minimum: 0. Use :none to specify no limit. Default: :none.

  • offset (Fixnum) (defaults to: 0)

    The number of results to skip. Minimum: 0. Default: 0.

  • sort_by (String) (defaults to: nil)

    The name of the attribute by which the result is to be sorted:

    • base_type

    • created_at (server default)

    • dtstart_at

    • first_name

    • last_name

    • score

    • title

    • updated_at

  • sort_order (String) (defaults to: nil)

    One of asc (ascending) or desc (descending). For sort_by score, the only valid sort order is desc (can be omitted).

Returns:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/crm.rb', line 116

def self.search(filters: nil, query: nil, limit: :none, offset: 0, sort_by: nil, sort_order: nil)
  server_limit = 100
  limit = Float::INFINITY if limit.nil? || limit == :none
  offset ||= 0

  ids = []
  total = nil
  initial_offset = offset

  loop do
    params = {
      'filters' => filters,
      'query' => query,
      'limit' => [limit, server_limit].min,
      'offset' => offset,
      'sort_by' => sort_by,
      'sort_order' => sort_order,
    }.reject { |k, v| v.nil? }
    search_results = Core::RestApi.instance.post('search', params)
    ids.concat(search_results['results'].map { |r| r['id'] })
    total = search_results['total']
    break if ids.size >= [limit, (total - initial_offset)].min
    limit -= server_limit
    offset += server_limit
  end

  Core::ItemEnumerator.new(ids, total: total)
end