Class: Broadcast::Client

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

Constant Summary collapse

CHANNEL_OVERRIDE_KEY =
:__broadcast_ruby_channel_override

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**settings) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/broadcast/client.rb', line 13

def initialize(**settings)
  @config = Configuration.new
  settings.each { |k, v| @config.public_send(:"#{k}=", v) }
  @config.validate!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/broadcast/client.rb', line 11

def config
  @config
end

Instance Method Details

#broadcastsObject



60
61
62
# File 'lib/broadcast/client.rb', line 60

def broadcasts
  @broadcasts ||= Resources::Broadcasts.new(self)
end

#email_serversObject



84
85
86
# File 'lib/broadcast/client.rb', line 84

def email_servers
  @email_servers ||= Resources::EmailServers.new(self)
end

#get_email(id) ⇒ Object



46
47
48
# File 'lib/broadcast/client.rb', line 46

def get_email(id)
  transactionals.get_transactional(id)
end

#opt_in_formsObject



80
81
82
# File 'lib/broadcast/client.rb', line 80

def opt_in_forms
  @opt_in_forms ||= Resources::OptInForms.new(self)
end

#request(method, path, body_or_params = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
92
93
94
95
96
# File 'lib/broadcast/client.rb', line 89

def request(method, path, body_or_params = nil)
  payload = inject_channel_scope(body_or_params)
  uri = build_uri(path, method, payload)

  retry_with_backoff { execute(method, uri, payload) }
rescue Net::OpenTimeout, Net::ReadTimeout => e
  raise Broadcast::TimeoutError, "Request timeout: #{e.message}"
end

#segmentsObject



64
65
66
# File 'lib/broadcast/client.rb', line 64

def segments
  @segments ||= Resources::Segments.new(self)
end

#send_email(to:, subject: nil, body: nil, reply_to: nil) ⇒ Object

Thin convenience wrapper around ‘transactionals.create`. Use `client.transactionals.create` directly for template_id, double_opt_in, preheader, and other advanced options.



42
43
44
# File 'lib/broadcast/client.rb', line 42

def send_email(to:, subject: nil, body: nil, reply_to: nil)
  transactionals.create(to: to, subject: subject, body: body, reply_to: reply_to)
end

#sequencesObject



56
57
58
# File 'lib/broadcast/client.rb', line 56

def sequences
  @sequences ||= Resources::Sequences.new(self)
end

#subscribersObject

— Resource sub-clients —



52
53
54
# File 'lib/broadcast/client.rb', line 52

def subscribers
  @subscribers ||= Resources::Subscribers.new(self)
end

#templatesObject



68
69
70
# File 'lib/broadcast/client.rb', line 68

def templates
  @templates ||= Resources::Templates.new(self)
end

#transactionalsObject



76
77
78
# File 'lib/broadcast/client.rb', line 76

def transactionals
  @transactionals ||= Resources::Transactionals.new(self)
end

#webhook_endpointsObject



72
73
74
# File 'lib/broadcast/client.rb', line 72

def webhook_endpoints
  @webhook_endpoints ||= Resources::WebhookEndpoints.new(self)
end

#with_channel(broadcast_channel_id) ⇒ Object

Run a block with a temporary broadcast_channel_id override that will be auto-included on every request inside the block. Useful for admin/system tokens that need to scope each call to a specific channel.

client.with_channel(123) do
  client.email_servers.list
end


28
29
30
31
32
33
34
35
# File 'lib/broadcast/client.rb', line 28

def with_channel(broadcast_channel_id)
  key = channel_override_key
  previous = Thread.current[key]
  Thread.current[key] = broadcast_channel_id
  yield self
ensure
  Thread.current[key] = previous
end