Skip to content

Commit

Permalink
Remove dependency on rest-client. Add mime-types for file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lacostej committed Feb 27, 2024
1 parent 4fc25b2 commit 09cc075
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ gem "danger-junit", "~> 1.0"
# A fake filesystem.
# Version 1.9+ requires Ruby >=2.7, while fastlane uses a `required_ruby_version` of `>= 2.6`.
gem "fakefs", "1.8"
# for file uploads with Faraday
gem "mime-types", ['>= 1.16', '< 4.0']
# Fast XML parser and object marshaller.
gem "ox", "2.13.2"
# Provides an interactive debugging environment for Ruby.
Expand All @@ -31,8 +33,6 @@ gem "rake"
# A readline implementation in Ruby
# See: https://github.com/deivid-rodriguez/byebug/issues/289#issuecomment-251383465
gem "rb-readline"
# A simple and flexible REST client.
gem "rest-client", ">= 1.8.0"
# Behavior-driven testing tool for Ruby.
gem "rspec", "~> 3.10"
# Formatter for RSpec to generate JUnit compatible reports.
Expand Down
13 changes: 3 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ GEM
signet (>= 0.16, < 2.a)
hashdiff (1.0.1)
highline (2.0.3)
http-accept (1.7.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
httpclient (2.8.3)
Expand All @@ -198,9 +197,9 @@ GEM
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
method_source (1.0.0)
mime-types (3.3.1)
mime-types (3.5.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0225)
mime-types-data (3.2024.0206)
mini_magick (4.12.0)
mini_mime (1.1.5)
multi_json (1.15.0)
Expand All @@ -210,7 +209,6 @@ GEM
nanaimo (0.3.0)
nap (1.1.0)
naturally (2.2.1)
netrc (0.11.0)
no_proxy_fix (0.1.2)
octokit (4.25.1)
faraday (>= 1, < 3)
Expand Down Expand Up @@ -250,11 +248,6 @@ GEM
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
retriable (3.1.2)
rexml (3.2.6)
rouge (2.0.7)
Expand Down Expand Up @@ -377,14 +370,14 @@ DEPENDENCIES
fastlane-plugin-clubmate
fastlane-plugin-ruby
fastlane-plugin-slack_train (>= 0.2.0)
mime-types (>= 1.16, < 4.0)
ox (= 2.13.2)
pry
pry-byebug
pry-rescue
pry-stack_explorer
rake
rb-readline
rest-client (>= 1.8.0)
rspec (~> 3.10)
rspec_junit_formatter (~> 0.4.1)
rubocop (= 1.50.2)
Expand Down
26 changes: 22 additions & 4 deletions fastlane/lib/fastlane/actions/mailgun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ def self.is_supported?(platform)
end

def self.run(options)
Actions.verify_gem!('rest-client')
require 'rest-client'
Actions.verify_gem!('faraday')
Actions.verify_gem!('mime-types')
require 'faraday'
begin
# Use mime/types/columnar if available, for reduced memory usage
require 'mime/types/columnar'
rescue LoadError
require 'mime/types'
end
handle_params_transition(options)
mailgunit(options)
end
Expand Down Expand Up @@ -126,14 +133,25 @@ def self.mailgunit(options)

unless options[:attachment].nil?
attachment_filenames = [*options[:attachment]]
attachments = attachment_filenames.map { |filename| File.new(filename, 'rb') }
attachments = attachment_filenames.map { |filename| Faraday::UploadIO.new(filename, mime_for(filename), filename) }
params.store(:attachment, attachments)
end

RestClient.post("https://api:#{options[:apikey]}@api.mailgun.net/v3/#{sandbox_domain}/messages", params)
conn = Faraday.new(url: "https://api:#{options[:apikey]}@api.mailgun.net") do |f|
f.request(:multipart)
f.request(:url_encoded)
f.adapter(:net_http)
end
response = conn.post("/v3/#{sandbox_domain}/messages", params)
UI.user_error!("Failed to send message via Mailgun, response: #{response.status}: #{response.body}.") if response.status != 200
mail_template(options)
end

def self.mime_for(path)
mime = MIME::Types.type_for(path)
mime.empty? ? 'text/plain' : mime[0].content_type
end

def self.mail_template(options)
hash = {
author: Actions.git_author_email,
Expand Down

0 comments on commit 09cc075

Please sign in to comment.