Class: Trolley::RecipientGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/trolley/gateways/RecipientGateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RecipientGateway

Returns a new instance of RecipientGateway.



5
6
7
# File 'lib/trolley/gateways/RecipientGateway.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#create(body) ⇒ Object



14
15
16
17
# File 'lib/trolley/gateways/RecipientGateway.rb', line 14

def create(body)
  response = @client.post('/v1/recipients/', body)
  recipient_builder(response)
end

#delete(recipient_id) ⇒ Object

Parameters:

  • recipient_id (String)

    or [Array] The id (or array of ids) of the recipient to delete



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trolley/gateways/RecipientGateway.rb', line 25

def delete(recipient_id)
  path = '/v1/recipients/'
  body = ''

  if recipient_id.is_a?(String)
    path += recipient_id
  elsif recipient_id.is_a?(Array)
    body = { ids: recipient_id }
  else
    raise 'Invalid recipient_id type'
  end

  @client.delete(path, body)
  true
end

#find(recipient_id) ⇒ Object



9
10
11
12
# File 'lib/trolley/gateways/RecipientGateway.rb', line 9

def find(recipient_id)
  response = @client.get("/v1/recipients/#{recipient_id}")
  recipient_builder(response)
end

#find_logs(recipient_id) ⇒ Object

Note:

This method retrieves a list of activity logs for a recipient



42
43
44
45
# File 'lib/trolley/gateways/RecipientGateway.rb', line 42

def find_logs(recipient_id)
  response = @client.get("/v1/recipients/#{recipient_id}/logs")
  JSON.parse(response, object_class: OpenStruct)
end

#find_payments(recipient_id, page = 1, page_size = 10) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/trolley/gateways/RecipientGateway.rb', line 47

def find_payments(recipient_id, page = 1, page_size = 10)
  query_string = URI.encode_www_form(
    page: page.to_s,
    pageSize: page_size.to_s
  )
  response = @client.get("/v1/recipients/#{recipient_id}/payments?#{query_string}")
  payments_list_builder(response)
end

#payments_list_builder(response) ⇒ Object



78
79
80
# File 'lib/trolley/gateways/RecipientGateway.rb', line 78

def payments_list_builder(response)
  Utils::PaginatedArray.from_response(response, Payment)
end

#recipient_builder(response) ⇒ Object

rubocop:enable Metrics/ParameterLists



70
71
72
# File 'lib/trolley/gateways/RecipientGateway.rb', line 70

def recipient_builder(response)
  Utils::ResponseMapper.build(response, Recipient)
end

#recipient_list_builder(response) ⇒ Object



74
75
76
# File 'lib/trolley/gateways/RecipientGateway.rb', line 74

def recipient_list_builder(response)
  Utils::PaginatedArray.from_response(response, Recipient)
end

#search(page = 1, page_size = 10, prefix_search = '', filters = {}) ⇒ Object

TODO: if we can afford a breaking change ideally these should be kwargs rubocop:disable Metrics/ParameterLists



58
59
60
61
62
63
64
65
66
67
# File 'lib/trolley/gateways/RecipientGateway.rb', line 58

def search(page = 1, page_size = 10, prefix_search = '', filters = {})
  query_string = URI.encode_www_form(
    page: page.to_s,
    pageSize: page_size.to_s,
    search: prefix_search,
    **filters
  )
  response = @client.get("/v1/recipients?#{query_string}")
  recipient_list_builder(response)
end

#update(recipient_id, body) ⇒ Object



19
20
21
22
# File 'lib/trolley/gateways/RecipientGateway.rb', line 19

def update(recipient_id, body)
  @client.patch("/v1/recipients/#{recipient_id}", body)
  true
end