loops_email 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: 10a833456d991fba24c9c68791fb263245e0ca826ee100046f181895fccf3d6b
4
+ data.tar.gz: 3e6a1e223a2fed620a47430bf913e3d15091568325c9f7ec5a3f0de26861bd45
5
+ SHA512:
6
+ metadata.gz: 4a368d0e58f371592e9b56e77dd51d54cb6df95d18fd9c99acd98cb4a6b1805ff5ad701eb361a2f949356f77a883c2eaebfe99b6fea4229db1a3cf90808deb09
7
+ data.tar.gz: 8ce818668344081046bbea6aad1e466f063caa7a1a32c746786e38253816006b3ed425f4c1c59db0f4829a7b6ab1449ae83430fe9419b8e1e8c9e810c21f4d25
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # LoopsEmail
2
+ Unofficial loops email sdk. Official docs: https://loops.so/docs/start-here
3
+
4
+ ## Installation
5
+
6
+ Install the gem and add to the application's Gemfile by executing:
7
+
8
+ $ bundle add loops_email
9
+
10
+ If bundler is not being used to manage dependencies, install the gem by executing:
11
+
12
+ $ gem install loops_email
13
+
14
+ ## Usage
15
+
16
+ Create initializer `config/initializers/loops_email.rb`:
17
+ ```ruby
18
+ LoopsEmail.configure do |config|
19
+ config.api_key = "xxxxx" # if you're using rails, it's better to use built-in Credential
20
+ # config.stub = Rails.env.development? && ENV["LOOPS_EMAIL_ENABLE"]&.empty?
21
+ # config.log_file_path = "log/loops_email.log"
22
+ end
23
+ ```
24
+
25
+ ### Send transaction email
26
+ ```ruby
27
+ result = LoopsEmail::Sdk::TransactionalEmail.new(
28
+ receiver: "your_receiver_email",
29
+ transaction_id: "your_transaction_id",
30
+ variables: {
31
+ your_variable_name: "your_variable_value"
32
+ }
33
+ ).call
34
+
35
+ if result.success?
36
+ # handle success
37
+ else
38
+ puts result.error
39
+ end
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/loops_email.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,59 @@
1
+
2
+ class LoopsEmail::Client
3
+ def initialize; end
4
+
5
+ def post(endpoint, body: {}, other_headers: {})
6
+ return if LoopsEmail.stub?
7
+
8
+ response = Net::HTTP.post(
9
+ endpoint_uri(endpoint),
10
+ body.to_json,
11
+ headers.merge(other_headers)
12
+ )
13
+ log_response(response)
14
+ LoopsEmail::Result.new(response)
15
+ end
16
+
17
+ def get(endpoint, other_headers: {})
18
+ return if LoopsEmail.stub?
19
+
20
+ response = Net::HTTP.get(
21
+ endpoint_uri(endpoint),
22
+ headers.merge(other_headers)
23
+ )
24
+ log_response(response)
25
+ LoopsEmail::Result.new(response)
26
+ end
27
+
28
+ private
29
+
30
+ def success?(response)
31
+ response.is_a? Net::HTTPSuccess
32
+ end
33
+
34
+ def log_response(response)
35
+ if response.is_a? Net::HTTPSuccess
36
+ LoopsEmail.logger.info "[LoopsEmail] send email success: #{response.body}"
37
+ else
38
+ LoopsEmail.logger.error "[LoopsEmail] send email failed: #{response.body}"
39
+ end
40
+ end
41
+
42
+ def endpoint_uri(endpoint)
43
+ slash = endpoint.start_with?('/') ? '' : '/'
44
+ URI("#{LoopsEmail::Configuration::API_HOST}#{slash}#{endpoint}")
45
+ end
46
+
47
+ def headers
48
+ {
49
+ "Authorization": "Bearer #{api_key}",
50
+ "Content-Type": 'application/json'
51
+ }
52
+ end
53
+
54
+ def api_key
55
+ LoopsEmail.api_key ||
56
+ ENV["LOOPS_EMAIL_API_KEY"] ||
57
+ raise(LoopsEmail::Error.new("api_key is not set"))
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+
2
+ class LoopsEmail::Configuration
3
+ attr_writer :api_key, :stub, :log_file_path
4
+
5
+ API_HOST = "https://app.loops.so"
6
+
7
+ class Error < LoopsEmail::Error; end
8
+
9
+ def initialize; end
10
+
11
+ def api_key
12
+ @api_key || ENV["LOOPS_EMAIL_API_KEY"] || raise(LoopsEmail::Configuration::Error.new("api_key is not set"))
13
+ end
14
+
15
+ def log_file_path
16
+ @log_file_path || "log/loops_email.log"
17
+ end
18
+
19
+ def stub
20
+ false
21
+ end
22
+ alias stub? stub
23
+ end
@@ -0,0 +1,3 @@
1
+
2
+ class LoopsEmail::Error < StandardError
3
+ end
@@ -0,0 +1,51 @@
1
+
2
+ class LoopsEmail::Result
3
+ attr_accessor :response
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ end
8
+
9
+ def success?
10
+ response_success? && operation_success?
11
+ end
12
+
13
+ def failed?
14
+ !success?
15
+ end
16
+
17
+ def response_success?
18
+ response.is_a? Net::HTTPSuccess
19
+ end
20
+
21
+ def operation_success?
22
+ body.dig(:body, :success)
23
+ end
24
+
25
+ def http_code
26
+ response.code
27
+ end
28
+
29
+ def message
30
+ body[:content] || body.dig(:error, :message) || body
31
+ end
32
+
33
+ def error
34
+ body[:error]
35
+ end
36
+
37
+ def body
38
+ JSON.parse(response.body, symbolize_names: true)
39
+ rescue JSON::ParserError
40
+ { content: response.body }
41
+ end
42
+
43
+ def as_json
44
+ {
45
+ success: success?,
46
+ http_code: http_code,
47
+ body: body
48
+ }
49
+ end
50
+ alias to_h as_json
51
+ end
@@ -0,0 +1,25 @@
1
+
2
+ class LoopsEmail::Sdk::TransactionalEmail
3
+ attr_accessor :receiver,
4
+ :transaction_id,
5
+ :variables
6
+
7
+
8
+ def initialize(receiver:, transaction_id:, variables: {})
9
+ @receiver = receiver
10
+ @transaction_id = transaction_id
11
+ @variables = variables
12
+ end
13
+
14
+ def call
15
+ LoopsEmail::Client.new.post("/api/v1/transactional", body: payload)
16
+ end
17
+
18
+ def payload
19
+ {
20
+ transactionalId: transaction_id,
21
+ email: receiver,
22
+ dataVariables: variables
23
+ }
24
+ end
25
+ end
@@ -0,0 +1,2 @@
1
+
2
+ module LoopsEmail::Sdk; end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LoopsEmail
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "loops_email/version"
4
+ require_relative "loops_email/error"
5
+ require_relative "loops_email/configuration"
6
+ require_relative "loops_email/result"
7
+ require_relative "loops_email/client"
8
+ require_relative "loops_email/sdk"
9
+ require_relative "loops_email/sdk/transactional_email"
10
+
11
+ module LoopsEmail
12
+ class << self
13
+ def configure
14
+ yield configuration if block_given?
15
+ end
16
+
17
+ def configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ def api_key
22
+ configuration.api_key
23
+ end
24
+
25
+ def stub?
26
+ configuration.stub?
27
+ end
28
+
29
+ def logger
30
+ @looger ||= if ENV["RAILS_LOG_TO_STDOUT"] || ENV["LOOPS_EMAIL_LOG_TO_STDOUT"]
31
+ Logger.new(STDOUT)
32
+ else
33
+ Logger.new(configuration.log_file_path)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ module LoopsEmail
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loops_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - ianlynxk@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - README.md
22
+ - Rakefile
23
+ - lib/loops_email.rb
24
+ - lib/loops_email/client.rb
25
+ - lib/loops_email/configuration.rb
26
+ - lib/loops_email/error.rb
27
+ - lib/loops_email/result.rb
28
+ - lib/loops_email/sdk.rb
29
+ - lib/loops_email/sdk/transactional_email.rb
30
+ - lib/loops_email/version.rb
31
+ - sig/loops_email.rbs
32
+ homepage: https://github.com/getfrl/loops_email
33
+ licenses: []
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.7
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: 'unofficial loops email sdk. Official docs: https://loops.so/docs/start-here'
54
+ test_files: []