Class: Talkbird::Entity::Channel
- Inherits:
-
Object
- Object
- Talkbird::Entity::Channel
- Defined in:
- lib/talkbird/entity/channel.rb
Overview
SendBird channel
Constant Summary collapse
- DEFAULTS =
{ distinct: true, is_ephemeral: true }.freeze
Class Method Summary collapse
-
.build(payload) ⇒ Channel, Boolean
Build a channel based on the current payload.
- .create(from, to, opts = {}) ⇒ Object
- .find(from, to) ⇒ Object
- .find_or_create(from, to) ⇒ Object
Instance Method Summary collapse
- #id ⇒ Object
-
#initialize(data = {}) ⇒ Channel
constructor
A new instance of Channel.
- #members ⇒ Object
- #name ⇒ Object
- #to_s ⇒ Object
- #update(message) ⇒ Object
Constructor Details
#initialize(data = {}) ⇒ Channel
Returns a new instance of Channel.
77 78 79 |
# File 'lib/talkbird/entity/channel.rb', line 77 def initialize(data = {}) @data = data end |
Class Method Details
.build(payload) ⇒ Channel, Boolean
Build a channel based on the current payload.
48 49 50 |
# File 'lib/talkbird/entity/channel.rb', line 48 def build(payload) !payload.empty? && Channel.new(payload) end |
.create(from, to, opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/talkbird/entity/channel.rb', line 52 def create(from, to, opts = {}) body = DEFAULTS.merge(opts) body[:channel_url] = opts.fetch(:id) { SecureRandom.uuid } body[:user_ids] = [from, to] result = Client.request( :post, 'group_channels', body: body ) if result.is_a?(Result::Success) Channel.new(result.body) else false end end |
.find(from, to) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/talkbird/entity/channel.rb', line 15 def find(from, to) # The only way to find a conversation with another person is to search # through all the conversations with some rather strict parameters # and pick the best match. # # In this case, the order is based on the latest last message as it # makes more sense to have the result = Client.request( :get, "users/#{from}/my_group_channels", params: { members_exactly_in: to, order: 'latest_last_message', distinct_mode: 'distinct', public_mode: 'private', show_member: true, limit: 1 } ) channels = result.body[:channels] || [] # Since this is the result of a search, the response can be an empty # array, in which case the result should also be false. if result.is_a?(Result::Success) && !channels.empty? Channel.build(channels.first) else false end end |
Instance Method Details
#id ⇒ Object
81 82 83 |
# File 'lib/talkbird/entity/channel.rb', line 81 def id @data[:channel_url] end |
#members ⇒ Object
89 90 91 |
# File 'lib/talkbird/entity/channel.rb', line 89 def members @data[:members].map { |hsh| Entity::User.new(hsh) } end |
#name ⇒ Object
85 86 87 |
# File 'lib/talkbird/entity/channel.rb', line 85 def name @data[:name] end |
#to_s ⇒ Object
101 102 103 |
# File 'lib/talkbird/entity/channel.rb', line 101 def to_s "#<Talkbird::Entity::Channel id=#{id} name=#{name} members=#{members}>" end |