Class: SendgridThreads::Client

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

Constant Summary collapse

VERSION =
"v1".freeze
URL =
'https://input.threads.io'.freeze
HEADERS =
{'Content-Type' => 'application/json', 'Accept' => 'application/json'}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



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

def initialize( params = {} )
  @key    = params.fetch(:key,    SendgridThreads.config.key)
  @secret = params.fetch(:secret, SendgridThreads.config.secret)
  @url    = params.fetch(:url,    SendgridThreads.config.url || URL)
  @raise_exceptions = params.fetch(:raise_exceptions,
                            SendgridThreads.config.raise_exceptions || true)
  @adapter    = params.fetch(:adapter, adapter)
  @conn       = params.fetch(:conn, conn)
  @user_agent = params.fetch(:user_agent, "sendgrid-threads/#{SendgridThreads::VERSION};ruby")
  yield self if block_given?
end

Instance Attribute Details

#adapterObject



43
44
45
# File 'lib/sendgrid_threads/client.rb', line 43

def adapter
  @adapter ||= Faraday.default_adapter
end

#connObject



35
36
37
38
39
40
41
# File 'lib/sendgrid_threads/client.rb', line 35

def conn
  @conn ||= Faraday.new(url: @url) do |conn|
    conn.request :url_encoded
    conn.request :basic_auth, @key, @secret
    conn.adapter adapter
  end
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/sendgrid_threads/client.rb', line 9

def key
  @key
end

#raise_exceptionsObject (readonly)

Returns the value of attribute raise_exceptions.



9
10
11
# File 'lib/sendgrid_threads/client.rb', line 9

def raise_exceptions
  @raise_exceptions
end

#secretObject (readonly)

Returns the value of attribute secret.



9
10
11
# File 'lib/sendgrid_threads/client.rb', line 9

def secret
  @secret
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/sendgrid_threads/client.rb', line 9

def url
  @url
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



9
10
11
# File 'lib/sendgrid_threads/client.rb', line 9

def user_agent
  @user_agent
end

Instance Method Details

#post(endpoint, body = nil) ⇒ Object



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

def post(endpoint, body = nil)
  endpoint_with_version = "/#{VERSION}/#{endpoint}"
  response = conn.post(endpoint_with_version) do |req|
    req.headers = HEADERS.merge('User-Agent' => @user_agent)
    req.body = JSON(body) if body
  end
  fail SendgridThreads::Exception, response.body if raise_exceptions? && response.status != 200

  response
end

#raise_exceptions?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sendgrid_threads/client.rb', line 47

def raise_exceptions?
  @raise_exceptions
end