Class: Talkbird::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/talkbird/client.rb

Overview

Class that handles the basic calls to SendBird

Constant Summary collapse

VERSION =
'v3'
SCHEME =
'https'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/talkbird/client.rb', line 105

def initialize
  @http = HTTP.use(
    instrumentation: {
      instrumenter: ActiveSupport::Notifications.instrumenter,
      namespace: Talkbird::Instrumentation::Event::NAMESPACE
    }
  )

  Instrumentation::Event.register_instrumentation_for_request
  Instrumentation::Event.register_instrumentation_for_response
end

Class Method Details

.application_idObject



24
25
26
27
28
29
30
31
32
# File 'lib/talkbird/client.rb', line 24

def application_id
  app_id = ENV['SENDBIRD_APP_ID'].to_s

  if app_id.empty?
    raise ArgumentError, 'Missing SendBird application ID from ENV'
  else
    app_id
  end
end

.hostObject



34
35
36
# File 'lib/talkbird/client.rb', line 34

def host
  "api-#{application_id}.sendbird.com"
end

.request(method, path, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/talkbird/client.rb', line 46

def request(method, path, opts = {})
  default_headers = {
    'Api-Token' => token,
    'Content-Type' => 'application/json; charset=utf8'
  }

  req = HTTP::Request.new(
    verb: method,
    uri: uri(path, opts[:params]),
    headers: (opts[:headers] || {}).merge(default_headers),
    body: MultiJson.dump(opts[:body])
  )

  response = Client.instance.request(req, opts)
  Talkbird::Result.create(response)
end

.tokenObject



14
15
16
17
18
19
20
21
22
# File 'lib/talkbird/client.rb', line 14

def token
  token = ENV['SENDBIRD_API_TOKEN'].to_s

  if token.empty?
    raise ArgumentError, 'Missing SendBird API token from ENV'
  else
    token
  end
end

.uri(path, params = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/talkbird/client.rb', line 38

def uri(path, params = {})
  if path.is_a?(HTTP::URI)
    build_uri_from_existing(path, params)
  else
    build_uri_from_partial_path(path, params)
  end
end

Instance Method Details

#request(req, opts = {}) ⇒ Object



117
118
119
120
# File 'lib/talkbird/client.rb', line 117

def request(req, opts = {})
  options = HTTP::Options.new(opts)
  @http.perform(req, options)
end