Skip to content

Commit

Permalink
Add RuboCop linting and use frozen_string_literal everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ur5us committed Feb 17, 2020
1 parent 10b5d8a commit 944890c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Layout/LineLength:
Max: 120

Style/StringLiterals:
EnforcedStyle: double_quotes
40 changes: 21 additions & 19 deletions hubspot-mailer.gemspec
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
# frozen_string_literal: true

Gem::Specification.new do |s|
s.name = "hubspot-mailer".freeze
s.name = "hubspot-mailer"
s.version = "0.0.2"
s.licenses = ['MIT']
s.date = "2018-12-09".freeze
s.description = "Create beautiful transactional emails right within HubSpot using the email editor with all the benefits of smart content, personalization and templates - just like regular HubSpot emails. Create beautiful transactional emails right within HubSpot using the email editor with all the benefits of smart content, personalization and templates - just like regular HubSpot emails. Create beautiful transactional emails right within HubSpot using the email editor with all the benefits of smart content, personalization and templates - just like regular HubSpot emails.".freeze
s.summary = "HubSpot Single Send API SDK for use with Ruby on Rails".freeze
s.authors = ["heaven".freeze]
s.email = ["[email protected]".freeze]
s.homepage = "https://github.com/heaven/hubspot-mailer".freeze
s.licenses = ["MIT"]
s.date = "2018-12-09"
s.description = "Create beautiful transactional emails right within HubSpot using the email editor with all the benefits of smart content, personalization and templates - just like regular HubSpot emails. Create beautiful transactional emails right within HubSpot using the email editor with all the benefits of smart content, personalization and templates - just like regular HubSpot emails. Create beautiful transactional emails right within HubSpot using the email editor with all the benefits of smart content, personalization and templates - just like regular HubSpot emails."
s.summary = "HubSpot Single Send API SDK for use with Ruby on Rails"
s.authors = ["heaven"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/heaven/hubspot-mailer"
s.files = [
"lib/hubspot-mailer.rb",
"lib/hubspot/mailer.rb",
"lib/hubspot/mailer/delivery.rb",
"lib/hubspot/mailer/exceptions.rb",
"lib/hubspot/mailer/hubspot_preview_interceptor.rb",
"lib/hubspot/mailer/message.rb",
"lib/hubspot/mailer/message.rb"
]
s.require_paths = ["lib"]

if s.respond_to? :specification_version
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0')
s.add_runtime_dependency(%q<hubspot-ruby>.freeze, ["~> 0.4"])
s.add_runtime_dependency(%q<actionmailer>.freeze, ["~> 5.1"])
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new("1.2.0")
s.add_runtime_dependency("actionmailer", ["~> 5.1"])
s.add_runtime_dependency("hubspot-ruby", ["~> 0.4"])
s.add_development_dependency("bundler", ["~> 1.0"])
else
s.add_dependency(%q<hubspot-ruby>.freeze, ["~> 0.4"])
s.add_dependency(%q<actionmailer>.freeze, ["~> 5.1"])
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
s.add_dependency("actionmailer", ["~> 5.1"])
s.add_dependency("bundler", ["~> 1.0"])
s.add_dependency("hubspot-ruby", ["~> 0.4"])
end
else
s.add_dependency(%q<hubspot-ruby>.freeze, ["~> 0.4"])
s.add_dependency(%q<actionmailer>.freeze, ["~> 5.1"])
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
s.add_dependency("actionmailer", ["~> 5.1"])
s.add_dependency("bundler", ["~> 1.0"])
s.add_dependency("hubspot-ruby", ["~> 0.4"])
end
end
15 changes: 8 additions & 7 deletions lib/hubspot-mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'action_mailer'
require 'hubspot-ruby'
require 'hubspot/mailer/delivery'
require 'hubspot/mailer/exceptions'
require 'hubspot/mailer/hubspot_preview_interceptor'
require 'hubspot/mailer/message'
require 'hubspot/mailer'
# frozen_string_literal: true

require "action_mailer"
require "hubspot-ruby"
require "hubspot/mailer/delivery"
require "hubspot/mailer/exceptions"
require "hubspot/mailer/hubspot_preview_interceptor"
require "hubspot/mailer/message"
require "hubspot/mailer"
42 changes: 23 additions & 19 deletions lib/hubspot/mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Hubspot
class Mailer < ActionMailer::Base
abstract!
Expand All @@ -15,7 +17,7 @@ class Mailer < ActionMailer::Base

self.default_params = {}.freeze

SINGLE_SEND_PATH = '/email/public/v1/singleEmail/send'.freeze
SINGLE_SEND_PATH = "/email/public/v1/singleEmail/send"

class << self
# Wraps an email delivery inside of <tt>ActiveSupport::Notifications</tt> instrumentation.
Expand Down Expand Up @@ -82,19 +84,19 @@ def single_send_params(mail)
end

# Copy subject from header to custom property
if mail.subject.present? and not mail.custom_properties.try(:[], :subject)
if mail.subject.present? && !mail.custom_properties.try(:[], :subject)
mail.custom_properties ||= {}
mail.custom_properties[:subject] = mail.subject
end

if mail.contact_properties.present?
data[:contactProperties] =
Hubspot::Utils.hash_to_properties(mail.contact_properties, :key_name => :name)
Hubspot::Utils.hash_to_properties(mail.contact_properties, key_name: :name)
end

if mail.custom_properties.present?
data[:customProperties] =
Hubspot::Utils.hash_to_properties(mail.custom_properties, :key_name => :name)
Hubspot::Utils.hash_to_properties(mail.custom_properties, key_name: :name)
end

data
Expand All @@ -104,20 +106,21 @@ def parse_response(response)
status_code = response["sendResult"]

case status_code
when "SENT", "QUEUED"
response["eventId"]
when "INVALID_TO_ADDRESS"
raise RecipientAddressError.new(response), "The TO address is invalid: #{status_code}"
when "INVALID_FROM_ADDRESS"
raise SenderAddressError.new(response), "The FROM address is invalid: #{status_code}"
when "BLOCKED_DOMAIN", "PORTAL_SUSPENDED"
raise SendingError.new(response), "Message can't be sent: #{status_code}"
when "PREVIOUSLY_BOUNCED", "PREVIOUS_SPAM"
raise DeliveryError.new(response), "Message can't be delivered: #{status_code}"
when "MISSING_CONTENT"
raise InvalidTemplateError.new(response), "The emailId is invalid, or the emailId is an email that is not set up for Single Send: #{status_code}"
else
raise UnknownResponseError.new(response), "Unrecognized status code: #{status_code}"
when "SENT", "QUEUED"
response["eventId"]
when "INVALID_TO_ADDRESS"
raise RecipientAddressError.new(response), "The TO address is invalid: #{status_code}"
when "INVALID_FROM_ADDRESS"
raise SenderAddressError.new(response), "The FROM address is invalid: #{status_code}"
when "BLOCKED_DOMAIN", "PORTAL_SUSPENDED"
raise SendingError.new(response), "Message can't be sent: #{status_code}"
when "PREVIOUSLY_BOUNCED", "PREVIOUS_SPAM"
raise DeliveryError.new(response), "Message can't be delivered: #{status_code}"
when "MISSING_CONTENT"
raise InvalidTemplateError.new(response),
"The emailId is invalid, or the emailId is an email that is not set up for Single Send: #{status_code}"
else
raise UnknownResponseError.new(response), "Unrecognized status code: #{status_code}"
end
end
end
Expand Down Expand Up @@ -173,7 +176,8 @@ def assign_attributes_to_message(message, headers)

def assign_headers_to_message(message, headers)
headers.except(:parts_order, :content_type, :body, :template_name,
:template_path, :delivery_method, :delivery_method_options).each { |k, v| message[k] = v }
:template_path, :delivery_method, :delivery_method_options)
.each { |k, v| message[k] = v }
end
end
end
4 changes: 3 additions & 1 deletion lib/hubspot/mailer/delivery.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

module Hubspot
class Mailer < ActionMailer::Base
class Delivery
attr_accessor :settings

DEFAULTS = {}
DEFAULTS = {}.freeze

def initialize(values)
self.settings = DEFAULTS.merge(values)
Expand Down
2 changes: 2 additions & 0 deletions lib/hubspot/mailer/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Hubspot
class Mailer < ActionMailer::Base
class MissingTemplateError < StandardError; end
Expand Down
7 changes: 5 additions & 2 deletions lib/hubspot/mailer/hubspot_preview_interceptor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Hubspot
class Mailer < ActionMailer::Base
class HubspotPreviewInterceptor
Expand All @@ -21,7 +23,8 @@ def transform!

def build_preview
if message.message.is_a?(Hubspot::Mailer::Message)
html_part = "<b>Email ID (template)</b>: #{message.email_id}<br/><br/>"
html_part = String.new
html_part << "<b>Email ID (template)</b>: #{message.email_id}<br/><br/>"

html_part << list_properties("Contact Properties (use via {{contact.propertyname}})", message.contact_properties)
html_part << list_properties("Custom Properties (use via {{custom.property_name}})", message.custom_properties)
Expand All @@ -33,7 +36,7 @@ def build_preview
end

def list_properties(label, list)
buffer = ""
buffer = String.new
return buffer unless list.present?

buffer << "<b>#{label}</b>:<ul>"
Expand Down
2 changes: 2 additions & 0 deletions lib/hubspot/mailer/message.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Hubspot
class Mailer < ActionMailer::Base
class Message < Mail::Message
Expand Down

0 comments on commit 944890c

Please sign in to comment.