semaphore-sms-ruby 2.1.2

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: 818fc7db1d70da7136fa8bb4403d90c3a01ead401e87a8c542d295661d8bd9fc
4
+ data.tar.gz: da60d0de2ede2bff17c3965b9ac59de2080e8d66dd05120dde64215a3c447150
5
+ SHA512:
6
+ metadata.gz: aa511710399ca2936cd0e90ee78cd1e27e7d7dead6735f6c52ed34151d406bc5cbaa133c9fe57f9affa470a8f8d27f9492f510679b675c7280ddb2480fd261ac
7
+ data.tar.gz: fafab793c7f84f7cde78129b0e565a065447eab503e145b5a75d4c4d99d29944b6491cf07a8d848d72ca8d846004cbf91b133ab9cf32f82fb61bd556f63a250e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Prince Karlo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Semaphore
2
+
3
+ This gem provides a simple and intuitive Ruby API wrapper for interacting with the Semaphore API. With this gem, you can easily send messages, manage accounts, and perform other operations using the Semaphore API.
4
+
5
+ > **Note:** This gem has been renamed from `ruby-semaphore` to `semaphore-sms-ruby`, and the client namespace is now `SemaphoreSMS` (previously `Semaphore`). Update your Gemfile and code accordingly if you were using the previous names.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "semaphore-sms-ruby"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $ gem install semaphore-sms-ruby
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ - Get your API key from https://semaphore.co/account
30
+
31
+ ### Quickstart
32
+
33
+ For a quick test you can pass your token directly to a new client:
34
+
35
+ ```rb
36
+ client = SemaphoreSMS::Client.new(api_key: '[API KEY]', sender_name: '[SENDER NAME]')
37
+
38
+ # Sending a message
39
+ client.messages.send(
40
+ message: '[YOUR MESSAGE]',
41
+ number: '[NUMBER]'
42
+ )
43
+
44
+ # Sending a bulk message
45
+ client.messages.bulk_send(
46
+ message: '[YOUR MESSAGE]',
47
+ numbers: ['NUMBER', 'NUMBER', 'NUMBER', 'NUMBER']
48
+ )
49
+
50
+ # Sending a priority message
51
+ client.priority.send(
52
+ message: '[YOUR MESSAGE]',
53
+ number: '[NUMBER]'
54
+ )
55
+
56
+ # Sending an OTP
57
+ client.otp.send(
58
+ message: 'Thanks for registering. Your OTP Code is {otp}.',
59
+ number: '[NUMBER]',
60
+ code: 1234
61
+ )
62
+
63
+ # Retrieving a message
64
+ client.messages.retrieve(123)
65
+
66
+ # Listing messages
67
+ client.messages.list(limit: 100, page: 1)
68
+
69
+ # Account
70
+ client.account.retrieve
71
+ client.account.transactions(limit: 100, page: 1)
72
+ client.account.sender_names
73
+ client.account.users
74
+ ```
75
+
76
+ ### Using Config
77
+
78
+ To enhance your setup, you can configure the gem with your API keys in a more secure manner, such as in a `semaphore_sms.rb` initializer file. This approach prevents hardcoding secrets directly into your codebase. Instead, consider using a gem like [dotenv](https://github.com/motdotla/dotenv) to safely pass the keys into your environments.
79
+
80
+ ```rb
81
+ # config/initializers/semaphore_sms.rb
82
+
83
+ SemaphoreSMS.configure do |config|
84
+ config.api_key = ENV.fetch("SEMAPHORE_API_KEY")
85
+ config.sender_name = ENV.fetch("SEMAPHORE_SENDERNAME")
86
+ end
87
+ ```
88
+
89
+ After configuring the API key, you can simply create a new client:
90
+
91
+ ```rb
92
+ client = SemaphoreSMS::Client.new
93
+
94
+ client.messages.send(
95
+ message: '[YOUR MESSAGE]',
96
+ number: '[NUMBER]'
97
+ )
98
+ ```
99
+
100
+ ## Development
101
+
102
+ ```bash
103
+ bundle install
104
+ bundle exec rake # specs + rubocop
105
+ bundle exec rake spec
106
+ bundle exec rake rubocop
107
+ ```
108
+
109
+ ## Contributing
110
+
111
+ Contributions are welcome! Please follow the guidelines outlined in the [CONTRIBUTING.md](https://github.com/frinsdev/ruby-semaphore-sms/blob/main/CONTRIBUTING.md) file.
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+ require 'rubocop/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ RuboCop::RakeTask.new
8
+
9
+ task default: %i[spec rubocop]
@@ -0,0 +1,2 @@
1
+ require_relative '../semaphore_sms'
2
+ require_relative '../semaphore_sms/compatibility'
@@ -0,0 +1,37 @@
1
+ module SemaphoreSMS
2
+ class Client
3
+ include SemaphoreSMS::HTTP
4
+
5
+ CONFIG_KEYS = %i[
6
+ api_version
7
+ api_key
8
+ sender_name
9
+ uri_base
10
+ request_timeout
11
+ ].freeze
12
+
13
+ attr_reader(*CONFIG_KEYS)
14
+
15
+ def initialize(config = {})
16
+ CONFIG_KEYS.each do |key|
17
+ instance_variable_set("@#{key}", config[key] || SemaphoreSMS.configuration.send(key))
18
+ end
19
+ end
20
+
21
+ def messages
22
+ @messages ||= Resources::Messages.new(self)
23
+ end
24
+
25
+ def priority
26
+ @priority ||= Resources::Priority.new(self)
27
+ end
28
+
29
+ def otp
30
+ @otp ||= Resources::Otp.new(self)
31
+ end
32
+
33
+ def account
34
+ @account ||= Resources::Account.new(self)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module Semaphore
3
+ VERSION = ::SemaphoreSMS::VERSION
4
+
5
+ Error = ::SemaphoreSMS::Error
6
+ ConfigurationError = ::SemaphoreSMS::ConfigurationError
7
+ Configuration = ::SemaphoreSMS::Configuration
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ module SemaphoreSMS
2
+ module HTTP
3
+ def post_request(path:, parameters: {})
4
+ conn.post(uri(path:)) do |req|
5
+ configure_request_params(req, parameters)
6
+ end&.body
7
+ end
8
+
9
+ def get_request(path:, parameters: {})
10
+ conn.get(uri(path:)) do |req|
11
+ configure_request_params(req, parameters)
12
+ end&.body
13
+ end
14
+
15
+ private
16
+
17
+ def configure_request_params(req, parameters)
18
+ req_parameters = parameters.dup
19
+ req_parameters[:apikey] = @api_key
20
+ req_parameters[:sender_name] = @sender_name
21
+ req.params = req_parameters
22
+ end
23
+
24
+ def conn
25
+ Faraday.new do |f|
26
+ f.options[:timeout] = @request_timeout
27
+ f.response :raise_error
28
+ f.response :json
29
+ end
30
+ end
31
+
32
+ def uri(path:)
33
+ URI("#{@uri_base}#{@api_version}#{path}")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ module Ruby
2
+ module Semaphore
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,40 @@
1
+ module SemaphoreSMS
2
+ module Resources
3
+ class Account
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def retrieve
9
+ @client.get_request(path: '/account')
10
+ end
11
+
12
+ def transactions(limit: nil, page: nil)
13
+ @client.get_request(
14
+ path: '/account/transactions',
15
+ parameters: compact_params(limit:, page:)
16
+ )
17
+ end
18
+
19
+ def sender_names(limit: nil, page: nil)
20
+ @client.get_request(
21
+ path: '/account/sendernames',
22
+ parameters: compact_params(limit:, page:)
23
+ )
24
+ end
25
+
26
+ def users(limit: nil, page: nil)
27
+ @client.get_request(
28
+ path: '/account/users',
29
+ parameters: compact_params(limit:, page:)
30
+ )
31
+ end
32
+
33
+ private
34
+
35
+ def compact_params(**parameters)
36
+ parameters.compact
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,51 @@
1
+ module SemaphoreSMS
2
+ module Resources
3
+ class Messages
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def send(message:, number:, **parameters)
9
+ @client.post_request(
10
+ path: '/messages',
11
+ parameters: compact_params(message:, number:, **parameters)
12
+ )
13
+ end
14
+
15
+ def bulk_send(message:, numbers:, **parameters)
16
+ @client.post_request(
17
+ path: '/messages',
18
+ parameters: compact_params(
19
+ message:,
20
+ number: Array(numbers).join(','),
21
+ **parameters
22
+ )
23
+ )
24
+ end
25
+
26
+ def retrieve(id)
27
+ @client.get_request(path: "/messages/#{id}")
28
+ end
29
+
30
+ def list(limit: nil, page: nil, start_date: nil, end_date: nil, network: nil, status: nil)
31
+ @client.get_request(
32
+ path: '/messages',
33
+ parameters: compact_params(
34
+ limit:,
35
+ page:,
36
+ startDate: start_date,
37
+ endDate: end_date,
38
+ network:,
39
+ status:
40
+ )
41
+ )
42
+ end
43
+
44
+ private
45
+
46
+ def compact_params(**parameters)
47
+ parameters.compact
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ module SemaphoreSMS
2
+ module Resources
3
+ class Otp
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def send(message:, number:, code: nil, **parameters)
9
+ @client.post_request(
10
+ path: '/otp',
11
+ parameters: compact_params(message:, number:, code:, **parameters)
12
+ )
13
+ end
14
+
15
+ private
16
+
17
+ def compact_params(**parameters)
18
+ parameters.compact
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module SemaphoreSMS
2
+ module Resources
3
+ class Priority
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def send(message:, number:, **parameters)
9
+ @client.post_request(
10
+ path: '/priority',
11
+ parameters: compact_params(message:, number:, **parameters)
12
+ )
13
+ end
14
+
15
+ private
16
+
17
+ def compact_params(**parameters)
18
+ parameters.compact
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module SemaphoreSMS
2
+ VERSION = '2.1.2'.freeze
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'faraday'
2
+
3
+ require_relative 'semaphore_sms/http'
4
+ require_relative 'semaphore_sms/resources/messages'
5
+ require_relative 'semaphore_sms/resources/priority'
6
+ require_relative 'semaphore_sms/resources/otp'
7
+ require_relative 'semaphore_sms/resources/account'
8
+ require_relative 'semaphore_sms/client'
9
+ require_relative 'semaphore_sms/version'
10
+
11
+ module SemaphoreSMS
12
+ class Error < StandardError; end
13
+ class ConfigurationError < Error; end
14
+
15
+ class Configuration
16
+ attr_writer :api_key, :sender_name
17
+ attr_accessor :api_version, :uri_base, :request_timeout
18
+
19
+ DEFAULT_API_VERSION = 'v4'.freeze
20
+ DEFAULT_URI_BASE = 'https://api.semaphore.co/api/'.freeze
21
+ DEFAULT_REQUEST_TIMEOUT = 120
22
+
23
+ def initialize
24
+ @api_key = nil
25
+ @api_version = DEFAULT_API_VERSION
26
+ @sender_name = nil
27
+ @uri_base = DEFAULT_URI_BASE
28
+ @request_timeout = DEFAULT_REQUEST_TIMEOUT
29
+ end
30
+
31
+ def api_key
32
+ return @api_key if @api_key
33
+
34
+ error_text = 'Semaphore access token missing! See https://semaphore.co/docs'
35
+ raise ConfigurationError, error_text
36
+ end
37
+
38
+ def sender_name
39
+ return @sender_name if @sender_name
40
+
41
+ error_text = 'Semaphore sender name missing! See https://semaphore.co/docs'
42
+ raise ConfigurationError, error_text
43
+ end
44
+ end
45
+
46
+ class << self
47
+ attr_writer :configuration
48
+ end
49
+
50
+ def self.configuration
51
+ @configuration ||= SemaphoreSMS::Configuration.new
52
+ end
53
+
54
+ def self.configure
55
+ yield(configuration)
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semaphore-sms-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Frins (Prince Karlo)
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.12'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.12'
26
+ description: This gem provides a simple and intuitive Ruby API wrapper for interacting
27
+ with the Semaphore API.
28
+ email:
29
+ - frins.dev@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/ruby/semaphore.rb
38
+ - lib/semaphore_sms.rb
39
+ - lib/semaphore_sms/client.rb
40
+ - lib/semaphore_sms/compatibility.rb
41
+ - lib/semaphore_sms/http.rb
42
+ - lib/semaphore_sms/railtie.rb
43
+ - lib/semaphore_sms/resources/account.rb
44
+ - lib/semaphore_sms/resources/messages.rb
45
+ - lib/semaphore_sms/resources/otp.rb
46
+ - lib/semaphore_sms/resources/priority.rb
47
+ - lib/semaphore_sms/version.rb
48
+ homepage: https://bootyard.com
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://bootyard.com
53
+ source_code_uri: https://github.com/frinsdev/ruby-semaphore-sms
54
+ changelog_uri: https://github.com/frinsdev/ruby-semaphore-sms/releases
55
+ rubygems_mfa_required: 'true'
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.2.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 4.0.16
71
+ specification_version: 4
72
+ summary: Ruby API Wrapper for https://semaphore.co/
73
+ test_files: []