Class: SendgridThreads::Client
- Inherits:
-
Object
- Object
- SendgridThreads::Client
- 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
- #adapter ⇒ Object
- #conn ⇒ Object
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#raise_exceptions ⇒ Object
readonly
Returns the value of attribute raise_exceptions.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#initialize(params = {}) {|_self| ... } ⇒ Client
constructor
A new instance of Client.
- #post(endpoint, body = nil) ⇒ Object
- #raise_exceptions? ⇒ Boolean
Constructor Details
#initialize(params = {}) {|_self| ... } ⇒ Client
Returns a new instance of Client.
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
#adapter ⇒ Object
43 44 45 |
# File 'lib/sendgrid_threads/client.rb', line 43 def adapter @adapter ||= Faraday.default_adapter end |
#conn ⇒ Object
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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/sendgrid_threads/client.rb', line 9 def key @key end |
#raise_exceptions ⇒ Object (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 |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
9 10 11 |
# File 'lib/sendgrid_threads/client.rb', line 9 def secret @secret end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/sendgrid_threads/client.rb', line 9 def url @url end |
#user_agent ⇒ Object (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
47 48 49 |
# File 'lib/sendgrid_threads/client.rb', line 47 def raise_exceptions? @raise_exceptions end |