Module: ClientSuccess::Client

Extended by:
Client
Included in:
Client
Defined in:
lib/client_success/client.rb

Defined Under Namespace

Classes: Error, NotFound

Instance Method Summary collapse

Instance Method Details

#create(attributes:, connection:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/client_success/client.rb', line 57

def create(attributes:, connection:)
  body = Schema::Client::Create[attributes]
    .transform_keys { |k| k.to_s.camelize(:lower) }
    .to_json

  response = connection.post(
    "/v1/clients", body)

  payload = response.body

  # TODO: find a better way to deal with api weirdness here
  payload["customFieldValues"].compact!

  DomainModel::Client.new(
    payload.deep_transform_keys(&:underscore))
end

#delete(client_id:, connection:) ⇒ Object



109
110
111
112
113
# File 'lib/client_success/client.rb', line 109

def delete(client_id:, connection:)
  # TODO: handle response
  connection.delete(
    "/v1/clients/#{client_id}")
end

#get_details(client_id:, connection:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/client_success/client.rb', line 29

def get_details(client_id:, connection:)
  response = connection.get(
    "/v1/clients/#{client_id}")

  payload = response.body

  DomainModel::Client.new(
    payload.deep_transform_keys(&:underscore))
rescue Connection::NotFound
  raise NotFound, "client with id '#{client_id}' not found"
end

#get_details_by_external_id(external_id:, connection:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/client_success/client.rb', line 41

def get_details_by_external_id(external_id:, connection:)
  params = {
    "externalId" => external_id
  }

  response = connection.get(
    "/v1/clients?#{params.compact.to_query}")

  payload = response.body

  DomainModel::Client.new(
    payload.deep_transform_keys(&:underscore))
rescue Connection::NotFound
  raise NotFound, "client with external id '#{external_id}' not found"
end

#list_all(assigned_csm_id: nil, active_only: true, connection:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/client_success/client.rb', line 13

def list_all(assigned_csm_id: nil, active_only: true,
         connection:)
  params = {
    "assignedCsmId" => assigned_csm_id,
    "activeOnly" => active_only
  }

  response = connection.get(
    "/v1/clients?#{params.compact.to_query}")

  response.body.map do |payload|
    DomainModel::Client.new(
      payload.deep_transform_keys(&:underscore))
  end
end

#update(client_id:, attributes:, connection:) ⇒ Object

NOTE: according to the api documentation, this is not a PATCH operation,

i.e. any fields not supplied will be set to null - so make sure you
supply everything :)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/client_success/client.rb', line 77

def update(client_id:, attributes:, connection:)
  body = attributes
    .deep_transform_keys { |k| k.to_s.camelize(:lower) }
    .to_json

  response = connection.put(
    "/v1/clients/#{client_id}", body)

  payload = response.body

  # TODO: find a better way to deal with api weirdness here
  payload["customFieldValues"].compact!

  DomainModel::Client.new(
    payload.deep_transform_keys(&:underscore))
end

#update_custom_field(client_id:, custom_field_name:, value:, connection:) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/client_success/client.rb', line 94

def update_custom_field(client_id:, custom_field_name:, value:, connection:)
  body = {
    custom_field_name => value.to_s
  }

  begin
    connection.patch(
      "/v1/customfield/value/client/#{client_id}", body)
  rescue Connection::ParsingError => error
    # NOTE: request submitted to resolve invalid JSON response from client
    #       success for this endpoint
    raise error unless error.message =~ /Update successful/
  end
end