commapi 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: 205e055b80825657ad0cab27d87d2ae5ee080190737f37c925e7f1a47d6f5a2b
4
+ data.tar.gz: 34018eb08b401b4461670bc17e471ca6a247dc51e31c0067828c6978b9d34adf
5
+ SHA512:
6
+ metadata.gz: 0e747abb84f973b19d7e2490b5dff2c8c82a594d51a1eff94c6e7eb0038d72af591ed7e1e0feabcc158142054f1cbc81035542bd70a844b1f407a802c33ddc56
7
+ data.tar.gz: 190c021e7138f471787688ee0f44c8c0c71597033fae3ae0e022c57be8993596dbc0f8f896e845cd1cb49cfeb22aba587b908f91c18fe7c0efe79b961f64ca81
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httparty"
4
+ require "json"
5
+
6
+ module CommApi
7
+ class Client
8
+ include HTTParty
9
+
10
+ read_timeout Integer(ENV["COMMAPI_TIMEOUT"] || 5)
11
+
12
+ def data(http_method, url, body = {})
13
+ case http_method
14
+ when :get
15
+ get(api_url_for(url), headers: api_headers)
16
+ when :post
17
+ post(api_url_for(url), body:, headers: api_headers)
18
+ end
19
+ end
20
+
21
+ def self.data(http_method, url, body = {})
22
+ self.new.data(http_method, url, body)
23
+ end
24
+
25
+ private
26
+
27
+ def api_headers
28
+ {
29
+ "Content-Type" => "application/json",
30
+ "Authorization" => "Bearer #{CommApi.configuration.token}"
31
+ }
32
+ end
33
+
34
+ def api_url_for(service_url)
35
+ File.join(CommApi.configuration.url, service_url)
36
+ end
37
+
38
+ def default_timeout
39
+ self.class.default_options[:timeout] || self.class.default_options[:read_timeout]
40
+ end
41
+
42
+ def get(url, headers: api_headers, timeout: default_timeout)
43
+ request(:get, url, headers: headers, timeout: timeout)
44
+ end
45
+
46
+ def post(url, body: {}, headers: api_headers, timeout: default_timeout)
47
+ request(:post, url, body: body.to_json, headers: headers, timeout: timeout)
48
+ end
49
+
50
+ def patch(url, body: {}, headers: api_headers, timeout: default_timeout)
51
+ request(:patch, url, body: body.to_json, headers: headers, timeout: timeout)
52
+ end
53
+
54
+ def request(http_method, url, options = {})
55
+ response = self.class.send(http_method, url, options) rescue OpenStruct.new(code: 599, parsed_response: {})
56
+ return {} unless response.code == 200 || response.code == 201
57
+ response.parsed_response
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/configurable"
4
+
5
+ module CommApi
6
+ class Configuration
7
+ include ActiveSupport::Configurable
8
+
9
+ config_accessor(:url)
10
+ config_accessor(:token)
11
+ config_accessor(:rabbitmq)
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommApi
4
+ class DeliveryMethod
5
+ def initialize(mail) end
6
+
7
+ def deliver!(mail)
8
+ Notification.email(
9
+ from: mail[:from].formatted.first,
10
+ to: mail.to,
11
+ cc: mail.cc,
12
+ bcc: mail.bcc,
13
+ subject: mail.subject,
14
+ text: mail.text_part ? mail.text_part.body&.raw_source : mail.body.raw_source,
15
+ html: mail.html_part ? mail.html_part.body&.raw_source : mail.body.raw_source,
16
+ attachments: mail.attachments.map do |a|
17
+ {
18
+ filename: a.filename,
19
+ content: a.body.raw_source,
20
+ content_type: a.mime_type,
21
+ contentType: a.mime_type, # TODO: Remove when the API is updated
22
+ encoding: a.body.encoding
23
+ }
24
+ end
25
+ )
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bunny"
4
+
5
+ module CommApi
6
+ module Messaging
7
+ class AMQPClient
8
+ include Singleton
9
+
10
+ attr :config, :connection, :channel
11
+
12
+ def self.instance
13
+ @@instance ||= new
14
+ end
15
+
16
+ def initialize
17
+ rabbitmq = CommApi.configuration.rabbitmq
18
+ return unless rabbitmq&.has_key?(:url)
19
+
20
+ @config = rabbitmq.with_indifferent_access
21
+ @connection = Bunny.new(config[:url])
22
+ @connection.start
23
+
24
+ @channel = @connection.create_channel
25
+
26
+ puts "Connected to #{amqp_url}"
27
+ rescue Bunny::TCPConnectionFailed => e
28
+ puts "Connection to #{amqp_url} failed: #{e.message}"
29
+ end
30
+
31
+ def self.classes
32
+ Dir["app/exchanges/*.rb", "engines/**/app/exchanges/*.rb"].
33
+ map { |file| File.basename(file, ".rb") }.
34
+ map(&:camelize).
35
+ map(&:constantize)
36
+ end
37
+
38
+ def self.connect
39
+ classes.each { |k| k.new } if instance.connection&.connected?
40
+ end
41
+
42
+ def self.disconnect!
43
+ instance.connection.close
44
+ @@instance = nil
45
+ puts "Disconnected from #{instance.amqp_url}"
46
+ end
47
+
48
+ private
49
+
50
+ def amqp_url
51
+ @config[:url].sub(/\/\/.*@/, "//")
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommApi
4
+ module Messaging
5
+ class Exchange
6
+ def self.exchange(exchange_name, type:, routing_key:, queue: nil, exchange_options: {}, queue_options: {})
7
+ return unless AMQPClient.instance.connection&.connected?
8
+ channel = AMQPClient.instance.channel
9
+ @exchange_name = exchange_name
10
+
11
+ # TODO: implement direct and fanout
12
+ @exchange, @queue = case type
13
+ when :topic
14
+ x = channel.topic(exchange_name, { durable: true }.merge(exchange_options))
15
+ q = channel.queue(queue, queue_options).bind(x, routing_key:) if queue
16
+ [x, q]
17
+ end
18
+ end
19
+
20
+ def self.subscribe(options = { manual_ack: false })
21
+ return unless AMQPClient.instance.connection&.connected?
22
+ # TODO: show usage?
23
+ raise "Exchange is not defined" unless @exchange
24
+ raise "Queue is not defined" unless @queue
25
+
26
+ @queue.subscribe({ block: false }.merge(options)) do |delivery_info, properties, payload|
27
+ r = { delivery_info:, properties:, payload: }
28
+ yield r
29
+ AMQPClient.instance.channel.acknowledge(delivery_info.delivery_tag) if options[:manual_ack]
30
+ end
31
+ end
32
+
33
+ def self.publish(payload, options = {})
34
+ return unless AMQPClient.instance.connection&.connected?
35
+ # TODO: show usage?
36
+ raise "Exchange is not defined" unless @exchange
37
+ raise "Routing key is not defined" unless options.has_key?(:routing_key)
38
+
39
+ @exchange.publish(payload,
40
+ {
41
+ routing_key: options[:routing_key],
42
+ message_id: SecureRandom.uuid,
43
+ persistent: true,
44
+ content_type: "application/json",
45
+ content_encoding: "UTF-8"
46
+ }.merge(options))
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommApi
4
+ module Messaging
5
+ autoload :AMQPClient, "commapi/messaging/amqp_client"
6
+ autoload :Exchange, "commapi/messaging/exchange"
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommApi
4
+ class Notification
5
+ def self.email(options)
6
+ Client.data(:post, "/api/v1/email", options)
7
+ end
8
+
9
+ def self.sms(options)
10
+ Client.data(:post, "/api/v1/sms", options)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommApi
4
+ class Railtie < Rails::Railtie
5
+ config.after_initialize do
6
+ ActionMailer::Base.add_delivery_method(:commapi, CommApi::DeliveryMethod)
7
+
8
+ CommApi::Messaging::AMQPClient.connect
9
+ end
10
+ end
11
+ end
data/lib/commapi.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommApi
4
+ autoload :Configuration, "commapi/configuration"
5
+ autoload :Client, "commapi/client"
6
+ autoload :Notification, "commapi/notification"
7
+ autoload :DeliveryMethod, "commapi/delivery_method"
8
+ autoload :Messaging, "commapi/messaging"
9
+
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def self.configure
15
+ yield configuration
16
+ end
17
+ end
18
+
19
+ require "commapi/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nomadix
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actioncable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionmailer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '6.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bunny
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.20'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.20'
69
+ - !ruby/object:Gem::Dependency
70
+ name: httparty
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.18.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.18.0
83
+ description: Nomadix CommAPI Ruby gem
84
+ email:
85
+ - rubygems@nomadix.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/commapi.rb
91
+ - lib/commapi/client.rb
92
+ - lib/commapi/configuration.rb
93
+ - lib/commapi/delivery_method.rb
94
+ - lib/commapi/messaging.rb
95
+ - lib/commapi/messaging/amqp_client.rb
96
+ - lib/commapi/messaging/exchange.rb
97
+ - lib/commapi/notification.rb
98
+ - lib/commapi/railtie.rb
99
+ homepage: https://nomadix.com
100
+ licenses: []
101
+ metadata:
102
+ homepage_uri: https://nomadix.com
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 2.3.0
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.5.16
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Nomadix CommAPI Ruby gem
122
+ test_files: []