Class: API::Templates::Service

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/api/templates/service.rb

Constant Summary collapse

MESSAGE_TEMPLATES_PATH =
T.let("message_templates", ::String)

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Service

Returns a new instance of Service.



12
13
14
# File 'lib/api/templates/service.rb', line 12

def initialize(config:)
  @config = config
end

Instance Method Details

#create(name:, category:, language:, components:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/api/templates/service.rb', line 66

def create(name:, category:, language:, components:)
  allow_category_change = true

  payload = {
    "name": name,
    "category": category.serialize,
    "allow_category_change": allow_category_change,
    "language": language,
    "components": components.map(&:serialize)
  }

  response = with_error_handling { templates_client.post(body: payload) }
  ::CloudWaba::Models::Templates::Response.parse(response: response)
end

#delete(name:, template_id: nil) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/api/templates/service.rb', line 106

def delete(name:, template_id: nil)
  params = { name: name }
  params[:hsm_id] = template_id unless template_id.nil?

  response = with_error_handling { templates_client.delete(params: params) }
  parsed_response = JSON.parse(response.body.to_s)
  
  parsed_response["success"] || false
end

#find_each(limit: 20, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/api/templates/service.rb', line 35

def find_each(limit: 20, &block)
  return unless block_given?

  fields = "id,name,category,language,status,components"
  params = { fields: fields, limit: limit }

  loop do
    response = with_error_handling { templates_client.get(params: params) }
    parsed_response = JSON.parse(response.body.to_s)

    templates = parsed_response["data"].map{|hash| ::CloudWaba::Models::Templates::Response.parse(template_hash: hash)}
    templates.each { |template| yield(template) }

    paging = parsed_response["paging"]
    after = paging&.dig("cursors", "after")
    next_link = paging&.dig("next")

    break if after.nil? || after.empty? || next_link.nil?

    params[:after] = after
  end
end

#list(limit: 20) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/api/templates/service.rb', line 20

def list(limit: 20)
  fields = "id,name,category,language,status,components"
  response = with_error_handling { templates_client.get(params: { fields: fields, limit: limit }) }

  parsed_response = JSON.parse(response.body.to_s)
  templates = parsed_response["data"].map{|hash| ::CloudWaba::Models::Templates::Response.parse(template_hash: hash)}
  ::CloudWaba::Models::Templates::List.new(templates: templates, paging: parsed_response["paging"])
end

#update(template_id:, category:, components:) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/api/templates/service.rb', line 88

def update(template_id:, category:, components:)
  template_client = template_endpoint(template_id: template_id)
  payload = {
    "category": category.serialize,
    "components": components.map(&:serialize)
  }
  response = with_error_handling { client.post(body: payload) }
  parsed_response = JSON.parse(response.body.to_s)

  parsed_response["success"] || false
end