alertus-ruby-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 30effb2e64c3fed5a76343906b72aab694c3e9b9a056245be6263c0821a56fd5
4
+ data.tar.gz: d24218de91683fcb5e2e2281f6dce08d67f57de17de5f35b6c111a254376687b
5
+ SHA512:
6
+ metadata.gz: 0a1ea77ac9fa9b1cb92b097c726fe43af916ced4c2b1bb46ee4450b2fa522ae7ce1cda14fe2b3e57fc3b2e8864680f0fc572f9bb8ed82cc35afdaffbc31a9bb0
7
+ data.tar.gz: c37f9c71a249db1412ba2f574c8510e9f9da498ee5b6519a97c6047d5bbf9aff35a32552b16bd56b2c77e2ae22cc1b615fac085947adf85da1e6c94abd75e00c
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
20
+
data/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # Alertus Ruby SDK
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/alertus-ruby-sdk.svg)](https://badge.fury.io/rb/alertus-ruby-sdk)
4
+ [![Build Status](https://github.com/alertus/alertus-ruby-sdk/actions/workflows/ci.yml/badge.svg)](https://github.com/alertus/alertus-ruby-sdk/actions)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/.../maintainability)](https://codeclimate.com/github/alertus/alertus-ruby-sdk/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/.../coverage)](https://codeclimate.com/github/alertus/alertus-ruby-sdk/test_coverage)
7
+
8
+ Official Ruby client for the Alertus notification platform. Send push notifications to Android and iOS devices with Firebase Cloud Messaging (FCM) through a simple, idiomatic Ruby API.
9
+
10
+ ## Features
11
+
12
+ - ๐Ÿš€ Simple and intuitive Ruby API
13
+ - ๐Ÿ”’ Secure API key authentication
14
+ - ๐Ÿ“ฑ Multi-platform push notifications (Android, iOS)
15
+ - ๐Ÿ“Š Detailed delivery reports and error handling
16
+ - ๐Ÿ› ๏ธ Customizable notification payloads
17
+ - โšก Thread-safe with connection pooling
18
+ - ๐Ÿงช Comprehensive test coverage with RSpec
19
+ - ๐Ÿ“ฆ Minimal dependencies
20
+
21
+ ## Requirements
22
+
23
+ - Ruby 2.7 or higher
24
+ - Valid Alertus API key from the [Alertus Console](https://console.alertus.cloud)
25
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'alertus-ruby-sdk'
32
+ ```
33
+
34
+ And then execute:
35
+
36
+ ```bash
37
+ $ bundle install
38
+ ```
39
+
40
+ Or install it yourself as:
41
+
42
+ ```bash
43
+ $ gem install alertus-ruby-sdk
44
+ ```
45
+
46
+ ## Quick Start
47
+
48
+ 1. Get your API key from the [Alertus Console](https://console.alertus.cloud)
49
+ 2. Initialize the client with your API key
50
+ 3. Create and send notifications
51
+
52
+ ```ruby
53
+ require 'alertus'
54
+
55
+ # Initialize client with default options
56
+ client = Alertus::Client.new(ENV['ALERTUS_API_KEY'])
57
+
58
+ # Or configure with custom options
59
+ client = Alertus::Client.new(ENV['ALERTUS_API_KEY'],
60
+ base_url: 'https://api.alertus.cloud',
61
+ timeout: 30,
62
+ logger: Logger.new(STDOUT),
63
+ user_agent: 'my-app/1.0'
64
+ )
65
+
66
+ # Create a notification message
67
+ message = Alertus::Message.new(
68
+ title: 'Hello from Alertus!',
69
+ body: 'This is a test notification',
70
+ tokens: ['device-token-1', 'device-token-2'],
71
+ data: { key1: 'value1', key2: 42 },
72
+ priority: 'high',
73
+ time_to_live: 86_400, # 1 day in seconds
74
+ collapse_key: 'test-notification'
75
+ )
76
+
77
+ # Send the notification
78
+ begin
79
+ response = client.notifications.send(message)
80
+
81
+ if response.success?
82
+ puts "Notification sent! ID: #{response.id}"
83
+ else
84
+ puts "Failed to send notification: #{response.errors}"
85
+ end
86
+ rescue Alertus::Error => e
87
+ puts "Error: #{e.message}"
88
+ end
89
+ ```
90
+
91
+ ## Advanced Usage
92
+
93
+ ### Sending to Topics
94
+
95
+ ```ruby
96
+ message = Alertus::Message.new(
97
+ title: 'Breaking News',
98
+ body: 'A new update is available!',
99
+ topic: 'news'
100
+ )
101
+ ```
102
+
103
+ ### Conditional Messaging
104
+
105
+ ```ruby
106
+ message = Alertus::Message.new(
107
+ title: 'Special Offer',
108
+ body: 'Check out our new features!',
109
+ condition: "'sports' in topics && 'breaking' in topics"
110
+ )
111
+ ```
112
+
113
+ ### Error Handling
114
+
115
+ The SDK raises specific exceptions for different error scenarios:
116
+
117
+ ```ruby
118
+ begin
119
+ response = client.notifications.send(message)
120
+ rescue Alertus::AuthenticationError => e
121
+ puts "Authentication failed: #{e.message}"
122
+ rescue Alertus::ValidationError => e
123
+ puts "Invalid request: #{e.message}"
124
+ puts "Validation errors: #{e.errors}"
125
+ rescue Alertus::ServerError => e
126
+ puts "Server error: #{e.message}"
127
+ puts "Retry after: #{e.retry_after} seconds" if e.retry_after
128
+ rescue Alertus::Error => e
129
+ puts "Unexpected error: #{e.message}"
130
+ end
131
+ ```
132
+
133
+ ## Configuration
134
+
135
+ ### Client Options
136
+
137
+ | Option | Type | Description | Default |
138
+ |--------|------|-------------|---------|
139
+ | `base_url` | String | Base URL for the API | `https://api.alertus.cloud` |
140
+ | `timeout` | Integer | Request timeout in seconds | `30` |
141
+ | `logger` | Logger | Logger instance for debugging | `nil` |
142
+ | `user_agent` | String | Custom User-Agent header | `alertus-ruby-sdk/{version}` |
143
+
144
+ ### Environment Variables
145
+
146
+ The SDK can be configured using environment variables:
147
+
148
+ ```bash
149
+ export ALERTUS_API_KEY='your-api-key'
150
+ export ALERTUS_API_URL='https://api.alertus.cloud'
151
+ ```
152
+
153
+ ## Development
154
+
155
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
156
+
157
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
158
+
159
+ ## Contributing
160
+
161
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alertus/alertus-ruby-sdk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alertus/alertus-ruby-sdk/blob/main/CODE_OF_CONDUCT.md).
162
+
163
+ 1. Fork it (https://github.com/alertus/alertus-ruby-sdk/fork)
164
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
165
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
166
+ 4. Push to the branch (`git push origin my-new-feature`)
167
+ 5. Create a new Pull Request
168
+
169
+ ## License
170
+
171
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
172
+
173
+ ## Code of Conduct
174
+
175
+ Everyone interacting in the Alertus Ruby SDK project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alertus/alertus-ruby-sdk/blob/main/CODE_OF_CONDUCT.md).
176
+
177
+ ## Support
178
+
179
+ For support, please contact support@alertus.cloud or open an issue in our [GitHub repository](https://github.com/alertus/alertus-ruby-sdk/issues).
@@ -0,0 +1,29 @@
1
+ module Alertus
2
+ class Client
3
+ DEFAULT_BASE_URL = 'https://api.alertus.cloud'
4
+
5
+ attr_reader :messaging
6
+
7
+ def initialize(access_token, logs: false, base_url: DEFAULT_BASE_URL, max_retries: 3)
8
+ raise AccessTokenError if access_token.nil? || access_token.strip.empty?
9
+
10
+ headers = {
11
+ 'X-API-Key' => access_token,
12
+ 'Accept' => 'application/json',
13
+ 'Content-Type' => 'application/json'
14
+ }
15
+
16
+ @http = HttpClient.new(base_url: base_url, headers: headers, max_retries: max_retries)
17
+ @messaging = Messaging.new(@http)
18
+ end
19
+
20
+ def self.make(access_token = ENV['ALERTUS_API_KEY'], base_url: DEFAULT_BASE_URL)
21
+ new(access_token, base_url: base_url)
22
+ end
23
+
24
+ def notifications
25
+ @messaging
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,7 @@
1
+ module Alertus
2
+ class Error < StandardError; end
3
+ class AccessTokenError < Error; end
4
+ class UnauthorizedError < Error; end
5
+ class ForbiddenError < Error; end
6
+ end
7
+
@@ -0,0 +1,53 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Alertus
6
+ class HttpClient
7
+ def initialize(base_url:, headers: {}, timeout: 30, max_retries: 3)
8
+ @base_url = base_url
9
+ @headers = headers
10
+ @timeout = timeout
11
+ @max_retries = max_retries
12
+ end
13
+
14
+ def post(path, body: {})
15
+ request(:post, path, body: body)
16
+ end
17
+
18
+ private
19
+
20
+ def request(method, path, body: nil)
21
+ uri = URI.join(@base_url, path)
22
+
23
+ attempt = 0
24
+ begin
25
+ attempt += 1
26
+ http = Net::HTTP.new(uri.host, uri.port)
27
+ http.use_ssl = uri.scheme == 'https'
28
+ http.read_timeout = @timeout
29
+ http.open_timeout = @timeout
30
+
31
+ req = case method
32
+ when :post then Net::HTTP::Post.new(uri)
33
+ when :get then Net::HTTP::Get.new(uri)
34
+ else raise ArgumentError, 'Unsupported method'
35
+ end
36
+
37
+ @headers.each { |k, v| req[k] = v }
38
+ if body
39
+ req['Content-Type'] ||= 'application/json'
40
+ req.body = JSON.generate(body)
41
+ end
42
+
43
+ res = http.request(req)
44
+ OpenStruct.new(code: res.code.to_i, body: res.body)
45
+ rescue IOError, SystemCallError, Timeout::Error => e
46
+ raise e if attempt > @max_retries
47
+ sleep(0.5 * attempt)
48
+ retry
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,22 @@
1
+ module Alertus
2
+ class Message
3
+ attr_accessor :title, :body, :tokens, :data
4
+
5
+ def initialize(title:, body: '', tokens: [], data: nil)
6
+ @title = title
7
+ @body = body
8
+ @tokens = tokens
9
+ @data = data
10
+ end
11
+
12
+ def to_h
13
+ {
14
+ title: @title,
15
+ body: @body,
16
+ tokens: @tokens,
17
+ data: @data
18
+ }
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,21 @@
1
+ module Alertus
2
+ class Messaging
3
+ def initialize(http)
4
+ @http = http
5
+ end
6
+
7
+ def send(message)
8
+ res = @http.post('/api/v1/notifications/send', body: message.to_h)
9
+
10
+ case res.code
11
+ when 401
12
+ raise UnauthorizedError
13
+ when 403
14
+ raise ForbiddenError
15
+ else
16
+ ValidationResponse.from_json(res.body)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,20 @@
1
+ require 'json'
2
+
3
+ module Alertus
4
+ class ValidationResponse
5
+ attr_reader :valid, :errors
6
+
7
+ def initialize(valid:, errors: {})
8
+ @valid = !!valid
9
+ @errors = errors || {}
10
+ end
11
+
12
+ def self.from_json(json_str)
13
+ data = JSON.parse(json_str)
14
+ new(valid: data['valid'], errors: data['errors'] || {})
15
+ rescue JSON::ParserError
16
+ new(valid: false, errors: { 'response' => ['invalid json'] })
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,4 @@
1
+ module Alertus
2
+ VERSION = '0.1.0'
3
+ end
4
+
data/lib/alertus.rb ADDED
@@ -0,0 +1,17 @@
1
+ require_relative 'alertus/version'
2
+ require_relative 'alertus/errors'
3
+ require_relative 'alertus/http_client'
4
+ require_relative 'alertus/message'
5
+ require_relative 'alertus/validation_response'
6
+ require_relative 'alertus/messaging'
7
+ require_relative 'alertus/client'
8
+
9
+ module Alertus
10
+ DEFAULT_BASE_URL = 'https://api.alertus.cloud'
11
+
12
+ def self.send_notification(message, api_key = ENV['ALERTUS_API_KEY'])
13
+ client = Client.make(api_key)
14
+ client.notifications.send(message)
15
+ end
16
+ end
17
+
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alertus-ruby-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alertus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.12'
27
+ description: Simple Ruby client for sending notifications via Alertus Cloud API.
28
+ email:
29
+ - opensource@alertus.cloud
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - lib/alertus.rb
37
+ - lib/alertus/client.rb
38
+ - lib/alertus/errors.rb
39
+ - lib/alertus/http_client.rb
40
+ - lib/alertus/message.rb
41
+ - lib/alertus/messaging.rb
42
+ - lib/alertus/validation_response.rb
43
+ - lib/alertus/version.rb
44
+ homepage: https://github.com/alertus/alertus-ruby-sdk
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ source_code_uri: https://github.com/alertus/alertus-ruby-sdk
49
+ changelog_uri: https://github.com/alertus/alertus-ruby-sdk/releases
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '2.7'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.1.6
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Alertus Cloud Ruby SDK
69
+ test_files: []