Class: FaithTeams::API::V2::Resource::Batch

Inherits:
Base show all
Defined in:
lib/faithteams/api/v2/resource/batch.rb

Overview

Batch resource

Instance Method Summary collapse

Methods inherited from Base

#delete, #update

Methods inherited from GatewayBase

#initialize, #valid_connection?

Constructor Details

This class inherits a constructor from FaithTeams::API::V2::GatewayBase

Instance Method Details

#create(entity:) ⇒ Entity::Batch

The response sends back the created batch in the data object.

Parameters:

Returns:

Raises:



46
47
48
49
50
51
52
53
# File 'lib/faithteams/api/v2/resource/batch.rb', line 46

def create(entity:)
  data = connection.post(path: "/batches", body: entity.to_h)["data"]

  Entity::Batch.new(attributes: data)
rescue Error::Request => e
  raise Error::Request.new(response: e.response, message: "Request Unsuccessful: The batch #{entity.inspect} could not be created. FaithTeams API failure message: #{e.message}}") if e.response.status.success?
  raise
end

#find(id:) ⇒ Entity::Batch?

Returns containing all batch details.

Parameters:

  • id (Integer)

Returns:

Raises:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/faithteams/api/v2/resource/batch.rb', line 21

def find(id:)
  raise FaithTeams::API::V2::Error::NoSearchParameterProvided if id.nil?

  data = connection.get(path: "/batches/#{id}")["data"]
  return nil unless data

  Entity::Batch.new(attributes: data)
rescue Error::Request => e
  return nil if e.response.code == 404
  raise
end

#find_by_batch(batch:) ⇒ Entity::Batch?

Note:

The batches endpoint does not actually filter by title so we do the filtering here.

Searches for a batch by dtm and title

Parameters:

Returns:



37
38
39
40
# File 'lib/faithteams/api/v2/resource/batch.rb', line 37

def find_by_batch(batch:)
  found = search(sort: "dtm", sortDirection: "desc", dtm: batch.created_datetime)
  found.find { |b| !b.deleted? && b.title == batch.title }
end

#search(**args) ⇒ Array<Entity::Batch>

Parameters:

  • args (Hash)

    Keyword args

Returns:



11
12
13
14
15
16
# File 'lib/faithteams/api/v2/resource/batch.rb', line 11

def search(**args)
  found = connection.get(path: "/batches", params: args)["data"]
  return [] unless found.present?

  found.map { |b| Entity::Batch.new(attributes: b) }
end