Class: Apologist::CtAs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/Apologist/ct_as/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/Apologist/ct_as/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#log_cta_click(request_options: {}, **params) ⇒ Apologist::Types::SuccessResponse

Records that a user clicked on a specific CTA

Examples:

client.ct_as.log_cta_click(
  id: "id",
  prompt_id: "prompt_id"
)

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/Apologist/ct_as/client.rb', line 70

def log_cta_click(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::CtAs::Types::CtaClickRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "ctas/#{URI.encode_uri_component(params[:id].to_s)}/click",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::Types::SuccessResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#match_ctas(request_options: {}, **params) ⇒ Apologist::CtAs::Types::MatchCtasResponse

Finds matching CTAs based on conversation context, user, session, device, or messages

Examples:

client.ct_as.match_ctas(request: {
  key: "value"
})

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/Apologist/ct_as/client.rb', line 29

def match_ctas(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "ctas/match",
    body: Apologist::Types::CtaMatchRequest.new(params).to_h,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::CtAs::Types::MatchCtasResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end