Class: Sendly::ConversationsResource
- Inherits:
-
Object
- Object
- Sendly::ConversationsResource
- Defined in:
- lib/sendly/conversations_resource.rb
Instance Method Summary collapse
- #add_labels(id, label_ids:) ⇒ Object
- #close(id) ⇒ Object
- #each(status: nil, batch_size: 100, &block) ⇒ Object
- #get(id, include_messages: false, message_limit: nil, message_offset: nil) ⇒ Object
- #get_context(id, max_messages: nil) ⇒ Object
-
#initialize(client) ⇒ ConversationsResource
constructor
A new instance of ConversationsResource.
- #list(limit: 20, offset: 0, status: nil) ⇒ Object
- #mark_read(id) ⇒ Object
- #remove_label(id, label_id:) ⇒ Object
- #reopen(id) ⇒ Object
- #reply(id, text:, media_urls: nil, metadata: nil) ⇒ Object
- #update(id, metadata: nil, tags: nil) ⇒ Object
Constructor Details
#initialize(client) ⇒ ConversationsResource
Returns a new instance of ConversationsResource.
5 6 7 |
# File 'lib/sendly/conversations_resource.rb', line 5 def initialize(client) @client = client end |
Instance Method Details
#add_labels(id, label_ids:) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/sendly/conversations_resource.rb', line 82 def add_labels(id, label_ids:) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? raise ValidationError, "Label IDs are required" if label_ids.nil? || label_ids.empty? encoded_id = URI.encode_www_form_component(id) @client.post("/conversations/#{encoded_id}/labels", { labelIds: label_ids }) end |
#close(id) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/sendly/conversations_resource.rb', line 58 def close(id) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? encoded_id = URI.encode_www_form_component(id) response = @client.post("/conversations/#{encoded_id}/close") Conversation.new(response) end |
#each(status: nil, batch_size: 100, &block) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sendly/conversations_resource.rb', line 110 def each(status: nil, batch_size: 100, &block) return enum_for(:each, status: status, batch_size: batch_size) unless block_given? offset = 0 loop do page = list(limit: batch_size, offset: offset, status: status) page.each(&block) break unless page.has_more offset += batch_size end end |
#get(id, include_messages: false, message_limit: nil, message_offset: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sendly/conversations_resource.rb', line 20 def get(id, include_messages: false, message_limit: nil, message_offset: nil) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? params = {} params[:include_messages] = true if params[:message_limit] = if params[:message_offset] = if encoded_id = URI.encode_www_form_component(id) response = @client.get("/conversations/#{encoded_id}", params.compact) ConversationWithMessages.new(response) end |
#get_context(id, max_messages: nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sendly/conversations_resource.rb', line 99 def get_context(id, max_messages: nil) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? params = {} params[:max_messages] = if encoded_id = URI.encode_www_form_component(id) response = @client.get("/conversations/#{encoded_id}/context", params.compact) ConversationContext.new(response) end |
#list(limit: 20, offset: 0, status: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/sendly/conversations_resource.rb', line 9 def list(limit: 20, offset: 0, status: nil) params = { limit: [limit, 100].min, offset: offset } params[:status] = status if status response = @client.get("/conversations", params.compact) ConversationList.new(response) end |
#mark_read(id) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/sendly/conversations_resource.rb', line 74 def mark_read(id) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? encoded_id = URI.encode_www_form_component(id) response = @client.post("/conversations/#{encoded_id}/mark-read") Conversation.new(response) end |
#remove_label(id, label_id:) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/sendly/conversations_resource.rb', line 90 def remove_label(id, label_id:) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? raise ValidationError, "Label ID is required" if label_id.nil? || label_id.empty? encoded_id = URI.encode_www_form_component(id) encoded_label_id = URI.encode_www_form_component(label_id) @client.delete("/conversations/#{encoded_id}/labels/#{encoded_label_id}") end |
#reopen(id) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/sendly/conversations_resource.rb', line 66 def reopen(id) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? encoded_id = URI.encode_www_form_component(id) response = @client.post("/conversations/#{encoded_id}/reopen") Conversation.new(response) end |
#reply(id, text:, media_urls: nil, metadata: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sendly/conversations_resource.rb', line 33 def reply(id, text:, media_urls: nil, metadata: nil) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? raise ValidationError, "Message text is required" if text.nil? || text.empty? body = { text: text } body[:mediaUrls] = media_urls if media_urls body[:metadata] = if encoded_id = URI.encode_www_form_component(id) response = @client.post("/conversations/#{encoded_id}/messages", body) Message.new(response) end |
#update(id, metadata: nil, tags: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sendly/conversations_resource.rb', line 46 def update(id, metadata: nil, tags: nil) raise ValidationError, "Conversation ID is required" if id.nil? || id.empty? body = {} body[:metadata] = unless .nil? body[:tags] = unless .nil? encoded_id = URI.encode_www_form_component(id) response = @client.patch("/conversations/#{encoded_id}", body) Conversation.new(response) end |