ruby-semaphore 1.0.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: 7751eab577eb3dfadaa8b2b937c7f7ea25384176989193fe7d740d878033665d
4
+ data.tar.gz: da1579637995fc2be78bdee3d4185b65f66e3fb3efba235429217ae7d0413071
5
+ SHA512:
6
+ metadata.gz: 9cbe7a81ac7474df3f174c2b4b9c64cb28c84913366d44a2d82a11c91d83b3c8c08dfb80cb90ad98440243e6675a383f160e765836d759637126323be31c4d78
7
+ data.tar.gz: b48c5c7453b6d660734b05db8a324713e1542a2e32e0f1c85abc1be623ca824ecbfb40575d44012b3009459b83cb6279fca9e7cae613bfdee1472f1fa764fb6f
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,89 @@
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
+ ## Usage
6
+
7
+ - Get your API key from https://semaphore.co/account
8
+
9
+ ### Quickstart
10
+
11
+ For a quick test you can pass your token directly to a new client:
12
+
13
+ ```rb
14
+ semaphore = Semaphore::Client.new(api_key: '[API KEY]', sender_name: '[SENDER NAME]')
15
+
16
+ # Sending a message
17
+ semaphore.messages(
18
+ parameters: {
19
+ message: '[YOUR MESSAGE]',
20
+ number: '[NUMBER]'
21
+ }
22
+ )
23
+
24
+ # Sending a Bulk Message
25
+ semaphore.messages(
26
+ parameters: {
27
+ message: '[YOUR MESSAGE]',
28
+ number: '[NUMBER], [NUMBER], [NUMBER], [NUMBER]' # Comma separated
29
+ }
30
+ )
31
+
32
+ # Sending an OTP
33
+ semaphore.otp(
34
+ parameters: {
35
+ message: 'Thanks for registering. Your OTP Code is {otp}.',
36
+ number: '[NUMBER]',
37
+ code: 1234
38
+ }
39
+ )
40
+ ```
41
+
42
+ ### Using Config
43
+
44
+ To enhance your setup, you can configure the gem with your API keys in a more secure manner, such as in an semaphore.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.
45
+
46
+ ```rb
47
+ # config/initializers/semaphore.rb
48
+
49
+ Semaphore.configure do |config|
50
+ config.api_key = ENV.fetch("SEMAPHORE_API_KEY")
51
+ config.sender_name = ENV.fetch("SEMAPHORE_SENDERNAME")
52
+ end
53
+ ```
54
+
55
+ After configuring the API key, you can simply create a new client:
56
+
57
+ ```rb
58
+ Semaphore::Client.new.messages(
59
+ parameters: {
60
+ message: '[YOUR MESSAGE]',
61
+ number: '[NUMBER]'
62
+ }
63
+ )
64
+ ```
65
+
66
+ ## Installation
67
+ Add this line to your application's Gemfile:
68
+
69
+ ```ruby
70
+ gem "ruby-semaphore"
71
+ ```
72
+
73
+ And then execute:
74
+ ```bash
75
+ $ bundle
76
+ ```
77
+
78
+ Or install it yourself as:
79
+ ```bash
80
+ $ gem install ruby-semaphore
81
+ ```
82
+
83
+ ## Contributing
84
+
85
+ Contributions are welcome! Please follow the guidelines outlined in the CONTRIBUTING.md file.
86
+
87
+ ## License
88
+
89
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require_relative '../semaphore'
2
+ require_relative '../semaphore/compatibility'
@@ -0,0 +1,53 @@
1
+ module Semaphore
2
+ class Client
3
+ include Semaphore::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] || Semaphore.configuration.send(key))
18
+ end
19
+ end
20
+
21
+ def messages(parameters: {})
22
+ post_request(path: '/messages', parameters:)
23
+ end
24
+
25
+ def priority(parameters: {})
26
+ post_request(path: '/priority', parameters:)
27
+ end
28
+
29
+ def otp(parameters: {})
30
+ post_request(path: '/otp', parameters:)
31
+ end
32
+
33
+ def get_message(id, parameters: {})
34
+ get_request(path: "/messages/#{id}", parameters:)
35
+ end
36
+
37
+ def account(parameters: {})
38
+ get_request(path: '/account', parameters:)
39
+ end
40
+
41
+ def account_transactions(parameters: {})
42
+ get_request(path: '/account/transactions', parameters:)
43
+ end
44
+
45
+ def account_sendernames(parameters: {})
46
+ get_request(path: '/account/sendernames', parameters:)
47
+ end
48
+
49
+ def account_users(parameters: {})
50
+ get_request(path: '/account/users', parameters:)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module Semaphore
3
+ VERSION = ::Semaphore::VERSION
4
+
5
+ Error = ::Semaphore::Error
6
+ ConfigurationError = ::Semaphore::ConfigurationError
7
+ Configuration = ::Semaphore::Configuration
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ module Semaphore
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
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,3 @@
1
+ module Semaphore
2
+ VERSION = '1.0.0'.freeze
3
+ end
data/lib/semaphore.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'faraday'
2
+
3
+ require_relative 'semaphore/http'
4
+ require_relative 'semaphore/client'
5
+ require_relative "semaphore/version"
6
+
7
+ module Semaphore
8
+ class Error < StandardError; end
9
+ class ConfigurationError < Error; end
10
+
11
+ class Configuration
12
+ attr_writer :api_key, :sender_name
13
+ attr_accessor :api_version, :uri_base, :request_timeout
14
+
15
+ DEFAULT_API_VERSION = 'v4'.freeze
16
+ DEFAULT_URI_BASE = 'https://api.semaphore.co/api/'.freeze
17
+ DEFAULT_REQUEST_TIMEOUT = 120
18
+
19
+ def initialize
20
+ @api_key = nil
21
+ @api_version = DEFAULT_API_VERSION
22
+ @sender_name = nil
23
+ @uri_base = DEFAULT_URI_BASE
24
+ @request_timeout = DEFAULT_REQUEST_TIMEOUT
25
+ end
26
+
27
+ def api_key
28
+ return @api_key if @api_key
29
+
30
+ error_text = 'Semaphore access token missing! See https://semaphore.co/docs'
31
+ raise ConfigurationError, error_text
32
+ end
33
+
34
+ def sender_name
35
+ return @sender_name if @sender_name
36
+
37
+ error_text = 'Semaphore sender name missing! See https://semaphore.co/docs'
38
+ raise ConfigurationError, error_text
39
+ end
40
+ end
41
+
42
+ class << self
43
+ attr_writer :configuration
44
+ end
45
+
46
+ def self.configuration
47
+ @configuration ||= Semaphore::Configuration.new
48
+ end
49
+
50
+ def self.configure
51
+ yield(configuration)
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-semaphore
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Prince Karlo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.12
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.12
27
+ description: This gem provides a simple and intuitive Ruby API wrapper for interacting
28
+ with the Semaphore API.
29
+ email:
30
+ - 77134068+princekarlo-bootyard@users.noreply.github.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - lib/ruby/semaphore.rb
39
+ - lib/semaphore.rb
40
+ - lib/semaphore/client.rb
41
+ - lib/semaphore/compatibility.rb
42
+ - lib/semaphore/http.rb
43
+ - lib/semaphore/railtie.rb
44
+ - lib/semaphore/version.rb
45
+ homepage: https://bootyard.com
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://bootyard.com
50
+ source_code_uri: https://github.com/princekarlo-bootyard/ruby-semaphore
51
+ changelog_uri: https://github.com/princekarlo-bootyard/ruby-semaphore/releases
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.4.15
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Ruby API Wrapper for https://semaphore.co/
71
+ test_files: []