Class: Sendly::Client
- Inherits:
-
Object
- Object
- Sendly::Client
- Defined in:
- lib/sendly/client.rb
Overview
Main Sendly API client
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
API key.
-
#base_url ⇒ String
readonly
Base URL.
-
#max_retries ⇒ Integer
readonly
Maximum retry attempts.
-
#organization_id ⇒ String?
Organization ID.
-
#timeout ⇒ Integer
readonly
Request timeout in seconds.
Instance Method Summary collapse
-
#account ⇒ Sendly::AccountResource
Access the Account resource.
-
#campaigns ⇒ Sendly::CampaignsResource
Access the Campaigns resource.
-
#contacts ⇒ Sendly::ContactsResource
Access the Contacts resource.
-
#conversations ⇒ Sendly::ConversationsResource
Access the Conversations resource.
-
#delete(path) ⇒ Hash
Make a DELETE request.
-
#drafts ⇒ Sendly::DraftsResource
Access the Drafts resource.
-
#enterprise ⇒ Sendly::EnterpriseResource
Access the Enterprise resource.
-
#get(path, params = {}) ⇒ Hash
Make a GET request.
-
#initialize(api_key:, base_url: nil, timeout: 30, max_retries: 3, organization_id: nil) ⇒ Client
constructor
Create a new Sendly client.
-
#labels ⇒ Sendly::LabelsResource
Access the Labels resource.
-
#media ⇒ Sendly::Media
Access the Media resource.
-
#messages ⇒ Sendly::Messages
Access the Messages resource.
-
#patch(path, body = {}) ⇒ Hash
Make a PATCH request.
-
#post(path, body = {}) ⇒ Hash
Make a POST request.
-
#post_multipart(path, file, content_type: "image/jpeg", filename: "upload.jpg") ⇒ Hash
Make a multipart POST request for file uploads.
-
#put(path, body = {}) ⇒ Hash
Make a PUT request.
-
#rules ⇒ Sendly::RulesResource
Access the Rules resource.
-
#templates ⇒ Sendly::TemplatesResource
Access the Templates resource.
-
#verify ⇒ Sendly::VerifyResource
Access the Verify resource.
-
#webhooks ⇒ Sendly::WebhooksResource
Access the Webhooks resource.
Constructor Details
#initialize(api_key:, base_url: nil, timeout: 30, max_retries: 3, organization_id: nil) ⇒ Client
Create a new Sendly client
37 38 39 40 41 42 43 44 45 |
# File 'lib/sendly/client.rb', line 37 def initialize(api_key:, base_url: nil, timeout: 30, max_retries: 3, organization_id: nil) @api_key = api_key @base_url = (base_url || Sendly.base_url).chomp("/") @timeout = timeout @max_retries = max_retries @organization_id = organization_id || ENV["SENDLY_ORG_ID"] validate_api_key! end |
Instance Attribute Details
#api_key ⇒ String (readonly)
Returns API key.
12 13 14 |
# File 'lib/sendly/client.rb', line 12 def api_key @api_key end |
#base_url ⇒ String (readonly)
Returns Base URL.
15 16 17 |
# File 'lib/sendly/client.rb', line 15 def base_url @base_url end |
#max_retries ⇒ Integer (readonly)
Returns Maximum retry attempts.
21 22 23 |
# File 'lib/sendly/client.rb', line 21 def max_retries @max_retries end |
#organization_id ⇒ String?
Returns Organization ID.
24 25 26 |
# File 'lib/sendly/client.rb', line 24 def organization_id @organization_id end |
#timeout ⇒ Integer (readonly)
Returns Request timeout in seconds.
18 19 20 |
# File 'lib/sendly/client.rb', line 18 def timeout @timeout end |
Instance Method Details
#account ⇒ Sendly::AccountResource
Access the Account resource
64 65 66 |
# File 'lib/sendly/client.rb', line 64 def account @account ||= AccountResource.new(self) end |
#campaigns ⇒ Sendly::CampaignsResource
Access the Campaigns resource
92 93 94 |
# File 'lib/sendly/client.rb', line 92 def campaigns @campaigns ||= CampaignsResource.new(self) end |
#contacts ⇒ Sendly::ContactsResource
Access the Contacts resource
99 100 101 |
# File 'lib/sendly/client.rb', line 99 def contacts @contacts ||= ContactsResource.new(self) end |
#conversations ⇒ Sendly::ConversationsResource
Access the Conversations resource
106 107 108 |
# File 'lib/sendly/client.rb', line 106 def conversations @conversations ||= ConversationsResource.new(self) end |
#delete(path) ⇒ Hash
Make a DELETE request
178 179 180 |
# File 'lib/sendly/client.rb', line 178 def delete(path) request(:delete, path) end |
#drafts ⇒ Sendly::DraftsResource
Access the Drafts resource
120 121 122 |
# File 'lib/sendly/client.rb', line 120 def drafts @drafts ||= DraftsResource.new(self) end |
#enterprise ⇒ Sendly::EnterpriseResource
Access the Enterprise resource
134 135 136 |
# File 'lib/sendly/client.rb', line 134 def enterprise @enterprise ||= EnterpriseResource.new(self) end |
#get(path, params = {}) ⇒ Hash
Make a GET request
143 144 145 |
# File 'lib/sendly/client.rb', line 143 def get(path, params = {}) request(:get, path, params: params) end |
#labels ⇒ Sendly::LabelsResource
Access the Labels resource
113 114 115 |
# File 'lib/sendly/client.rb', line 113 def labels @labels ||= LabelsResource.new(self) end |
#media ⇒ Sendly::Media
Access the Media resource
85 86 87 |
# File 'lib/sendly/client.rb', line 85 def media @media ||= Media.new(self) end |
#messages ⇒ Sendly::Messages
Access the Messages resource
50 51 52 |
# File 'lib/sendly/client.rb', line 50 def @messages ||= Messages.new(self) end |
#patch(path, body = {}) ⇒ Hash
Make a PATCH request
161 162 163 |
# File 'lib/sendly/client.rb', line 161 def patch(path, body = {}) request(:patch, path, body: body) end |
#post(path, body = {}) ⇒ Hash
Make a POST request
152 153 154 |
# File 'lib/sendly/client.rb', line 152 def post(path, body = {}) request(:post, path, body: body) end |
#post_multipart(path, file, content_type: "image/jpeg", filename: "upload.jpg") ⇒ Hash
Make a multipart POST request for file uploads
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/sendly/client.rb', line 189 def post_multipart(path, file, content_type: "image/jpeg", filename: "upload.jpg") uri = build_uri(path, {}) http = build_http(uri) boundary = "SendlyRuby#{SecureRandom.hex(16)}" file_data = file.is_a?(String) ? File.binread(file) : file.read body = [] body << "--#{boundary}\r\n" body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{filename}\"\r\n" body << "Content-Type: #{content_type}\r\n\r\n" body << file_data body << "\r\n--#{boundary}--\r\n" req = Net::HTTP::Post.new(uri) req["Authorization"] = "Bearer #{api_key}" req["Accept"] = "application/json" req["User-Agent"] = "sendly-ruby/#{VERSION}" req["Content-Type"] = "multipart/form-data; boundary=#{boundary}" req["X-Organization-Id"] = @organization_id if @organization_id req.body = body.join attempt = 0 begin response = http.request(req) handle_response(response) rescue Net::OpenTimeout, Net::ReadTimeout raise TimeoutError, "Request timed out after #{timeout} seconds" rescue Errno::ECONNREFUSED, Errno::ECONNRESET, SocketError => e raise NetworkError, "Connection failed: #{e.}" rescue RateLimitError => e attempt += 1 if attempt <= max_retries && e.retry_after sleep(e.retry_after) retry end raise rescue ServerError => e attempt += 1 if attempt <= max_retries sleep(2 ** attempt) retry end raise end end |
#put(path, body = {}) ⇒ Hash
Make a PUT request
170 171 172 |
# File 'lib/sendly/client.rb', line 170 def put(path, body = {}) request(:put, path, body: body) end |
#rules ⇒ Sendly::RulesResource
Access the Rules resource
127 128 129 |
# File 'lib/sendly/client.rb', line 127 def rules @rules ||= RulesResource.new(self) end |
#templates ⇒ Sendly::TemplatesResource
Access the Templates resource
78 79 80 |
# File 'lib/sendly/client.rb', line 78 def templates @templates ||= TemplatesResource.new(self) end |
#verify ⇒ Sendly::VerifyResource
Access the Verify resource
71 72 73 |
# File 'lib/sendly/client.rb', line 71 def verify @verify ||= VerifyResource.new(self) end |
#webhooks ⇒ Sendly::WebhooksResource
Access the Webhooks resource
57 58 59 |
# File 'lib/sendly/client.rb', line 57 def webhooks @webhooks ||= WebhooksResource.new(self) end |