Class: RelayGrid::Client

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

Overview

The entry point for every API call. Holds one Faraday connection and hands out the resource objects.

client = RelayGrid::Client.new                      # global configuration
client = RelayGrid::Client.new(api_key: "sk_other") # per-client override

Instances are frozen once built, so a single client is safe to share across threads.

Constant Summary collapse

IDEMPOTENT_METHODS =

Requests that may be replayed without changing server state. notify is deliberately absent: a blind retry sends the recipient a second real notification. See README ("Retries and idempotency").

%i[get head options].freeze
RETRIABLE_STATUSES =
[429, 500, 502, 503, 504].freeze
RETRIABLE_EXCEPTIONS =
[
  Faraday::ConnectionFailed,
  Faraday::TimeoutError,
  # Required for RETRIABLE_STATUSES to have any effect: faraday-retry signals
  # a retriable status by raising this, and only retries exceptions listed
  # here. Leaving it out silently disables status-based retries.
  Faraday::RetriableResponse,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil, **overrides) ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
40
41
42
43
44
# File 'lib/relaygrid/client.rb', line 35

def initialize(configuration = nil, **overrides)
  base = configuration || RelayGrid.configuration
  @configuration = base.merge(overrides).validate!

  # Built up front so the frozen instance never has to memoize lazily.
  @connection = build_connection
  @resources = {}

  freeze
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



33
34
35
# File 'lib/relaygrid/client.rb', line 33

def configuration
  @configuration
end

#connectionObject (readonly)

Returns the value of attribute connection.



92
93
94
# File 'lib/relaygrid/client.rb', line 92

def connection
  @connection
end

Instance Method Details

#channel_tokensObject



62
63
64
# File 'lib/relaygrid/client.rb', line 62

def channel_tokens
  resource(:channel_tokens) { Resources::ChannelTokens.new(self) }
end

#delete(path) ⇒ Object



88
89
90
# File 'lib/relaygrid/client.rb', line 88

def delete(path)
  request(:delete, path)
end

#deliveriesObject



50
51
52
# File 'lib/relaygrid/client.rb', line 50

def deliveries
  resource(:deliveries) { Resources::Deliveries.new(self) }
end

#get(path, params: {}) ⇒ Object



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

def get(path, params: {})
  request(:get, path, params: params)
end

#message_templatesObject



66
67
68
# File 'lib/relaygrid/client.rb', line 66

def message_templates
  resource(:message_templates) { Resources::MessageTemplates.new(self) }
end

#messagesObject



54
55
56
# File 'lib/relaygrid/client.rb', line 54

def messages
  resource(:messages) { Resources::Messages.new(self) }
end

#notificationsObject



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

def notifications
  resource(:notifications) { Resources::Notifications.new(self) }
end

#notify(**kwargs) ⇒ Object

Send a notification -- the one call the whole gem exists for. See Resources::Notifications#notify.



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

def notify(**kwargs)
  notifications.notify(**kwargs)
end

#patch(path, body: {}) ⇒ Object



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

def patch(path, body: {})
  request(:patch, path, body: body)
end

#post(path, body: {}) ⇒ Object



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

def post(path, body: {})
  request(:post, path, body: body)
end

#usersObject



58
59
60
# File 'lib/relaygrid/client.rb', line 58

def users
  resource(:users) { Resources::Users.new(self) }
end