diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..3311b8b --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,5 @@ +Layout/LineLength: + Max: 120 + +Style/StringLiterals: + EnforcedStyle: double_quotes \ No newline at end of file diff --git a/hubspot-mailer.gemspec b/hubspot-mailer.gemspec index dc781b0..d8dc3bb 100644 --- a/hubspot-mailer.gemspec +++ b/hubspot-mailer.gemspec @@ -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 = ["hello@codeart.us".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 = ["hello@codeart.us"] + 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.freeze, ["~> 0.4"]) - s.add_runtime_dependency(%q.freeze, ["~> 5.1"]) - s.add_development_dependency(%q.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.freeze, ["~> 0.4"]) - s.add_dependency(%q.freeze, ["~> 5.1"]) - s.add_dependency(%q.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.freeze, ["~> 0.4"]) - s.add_dependency(%q.freeze, ["~> 5.1"]) - s.add_dependency(%q.freeze, ["~> 1.0"]) + s.add_dependency("actionmailer", ["~> 5.1"]) + s.add_dependency("bundler", ["~> 1.0"]) + s.add_dependency("hubspot-ruby", ["~> 0.4"]) end end diff --git a/lib/hubspot-mailer.rb b/lib/hubspot-mailer.rb index b7034a4..b8ad860 100644 --- a/lib/hubspot-mailer.rb +++ b/lib/hubspot-mailer.rb @@ -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" diff --git a/lib/hubspot/mailer.rb b/lib/hubspot/mailer.rb index 3d9095c..1163e42 100644 --- a/lib/hubspot/mailer.rb +++ b/lib/hubspot/mailer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Hubspot class Mailer < ActionMailer::Base abstract! @@ -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 ActiveSupport::Notifications instrumentation. @@ -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 @@ -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 @@ -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 diff --git a/lib/hubspot/mailer/delivery.rb b/lib/hubspot/mailer/delivery.rb index 9bdb912..775fd95 100644 --- a/lib/hubspot/mailer/delivery.rb +++ b/lib/hubspot/mailer/delivery.rb @@ -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) diff --git a/lib/hubspot/mailer/exceptions.rb b/lib/hubspot/mailer/exceptions.rb index 3e80b48..68ca540 100644 --- a/lib/hubspot/mailer/exceptions.rb +++ b/lib/hubspot/mailer/exceptions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Hubspot class Mailer < ActionMailer::Base class MissingTemplateError < StandardError; end diff --git a/lib/hubspot/mailer/hubspot_preview_interceptor.rb b/lib/hubspot/mailer/hubspot_preview_interceptor.rb index c8f2aaa..5038425 100644 --- a/lib/hubspot/mailer/hubspot_preview_interceptor.rb +++ b/lib/hubspot/mailer/hubspot_preview_interceptor.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Hubspot class Mailer < ActionMailer::Base class HubspotPreviewInterceptor @@ -21,7 +23,8 @@ def transform! def build_preview if message.message.is_a?(Hubspot::Mailer::Message) - html_part = "Email ID (template): #{message.email_id}

" + html_part = String.new + html_part << "Email ID (template): #{message.email_id}

" 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) @@ -33,7 +36,7 @@ def build_preview end def list_properties(label, list) - buffer = "" + buffer = String.new return buffer unless list.present? buffer << "#{label}:
    " diff --git a/lib/hubspot/mailer/message.rb b/lib/hubspot/mailer/message.rb index cd8adfb..4fb1a66 100644 --- a/lib/hubspot/mailer/message.rb +++ b/lib/hubspot/mailer/message.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Hubspot class Mailer < ActionMailer::Base class Message < Mail::Message