{"action":"created","comment":{"url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/comments/38475256","id":38475256,"diff_hunk":"@@ -28,34 +33,53 @@ def add_attachment(path, name = nil)\n     end\n \n     def to_h\n-      payload = {\n-        :from        => @from,\n-        :fromname    => @from_name,\n-        :subject     => @subject,\n-        :to          => @to,\n-        :toname      => @to_name,\n-        :date        => @date,\n-        :replyto     => @reply_to,\n-        :cc          => @cc,\n-        :bcc         => @bcc,\n-        :text        => @text,\n-        :html        => @html,\n+      payload = {}\n+\n+      PAYLOAD_PARAMS.each do |payload_param|\n+        payload[payload_mapping(payload_param)] = self.send(payload_param)\n+      end\n+\n+      payload.merge!({\n         :'x-smtpapi' => smtpapi_json,\n-        :files       => ({} unless @attachments.empty?)\n-      }.reject {|k,v| v.nil?}\n+        :files       => extract_attachments(payload)\n+      })\n+\n+      payload.reject! {|k,v| v.nil?}\n+\n+      assign_missing_to(payload)\n \n+      payload\n+    end\n+\n+    # This mapping is to ensure that keys in the request payload\n+    #  match requirements. This enables the use of common idiomatic\n+    #  ruby variables (snake_cased) within class logic and easy translation\n+    #  for request preparation.\n+    def payload_mapping(key)\n+      {\n+        from_name: :fromname,\n+        to_name: :toname,\n+        reply_to: :replyto,\n+      }[key] || key\n+    end\n+\n+    def assign_missing_to(payload)\n       # smtpapi fixer\n       if @to.nil? and not @smtpapi.to.empty?\n         payload[:to] = payload[:from]\n       end\n+    end\n+\n+    def extract_attachments(payload)\n+      return if @attachments.empty?\n \n-      unless @attachments.empty?\n-        @attachments.each do |file|\n-          payload[:files][file[:name]] = file[:file]\n+      {}.tap do |files|\n+        unless @attachments.empty?","path":"lib/sendgrid/mail.rb","position":85,"original_position":85,"commit_id":"6fd9c7b1d1173aa571f01c12cf0a3ca36910f15c","original_commit_id":"93dfe8a619913f481ddd7fc4cfb6976568be1ec8","user":{"login":"wesdotcool","id":246963,"avatar_url":"https://avatars.githubusercontent.com/u/246963?v=3","gravatar_id":"","url":"https://api.github.com/users/wesdotcool","html_url":"https://github.com/wesdotcool","followers_url":"https://api.github.com/users/wesdotcool/followers","following_url":"https://api.github.com/users/wesdotcool/following{/other_user}","gists_url":"https://api.github.com/users/wesdotcool/gists{/gist_id}","starred_url":"https://api.github.com/users/wesdotcool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wesdotcool/subscriptions","organizations_url":"https://api.github.com/users/wesdotcool/orgs","repos_url":"https://api.github.com/users/wesdotcool/repos","events_url":"https://api.github.com/users/wesdotcool/events{/privacy}","received_events_url":"https://api.github.com/users/wesdotcool/received_events","type":"User","site_admin":false},"body":"You've already ensured `@attachments` is not empty.","created_at":"2015-09-01T21:35:05Z","updated_at":"2015-09-01T21:35:05Z","html_url":"https://github.com/apartmentlist/sendgrid-ruby/pull/8#discussion_r38475256","pull_request_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8","_links":{"self":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/comments/38475256"},"html":{"href":"https://github.com/apartmentlist/sendgrid-ruby/pull/8#discussion_r38475256"},"pull_request":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8"}}},"pull_request":{"url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8","id":43819774,"html_url":"https://github.com/apartmentlist/sendgrid-ruby/pull/8","diff_url":"https://github.com/apartmentlist/sendgrid-ruby/pull/8.diff","patch_url":"https://github.com/apartmentlist/sendgrid-ruby/pull/8.patch","issue_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/8","number":8,"state":"open","locked":false,"title":"Template Mailer","user":{"login":"yez","id":638533,"avatar_url":"https://avatars.githubusercontent.com/u/638533?v=3","gravatar_id":"","url":"https://api.github.com/users/yez","html_url":"https://github.com/yez","followers_url":"https://api.github.com/users/yez/followers","following_url":"https://api.github.com/users/yez/following{/other_user}","gists_url":"https://api.github.com/users/yez/gists{/gist_id}","starred_url":"https://api.github.com/users/yez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yez/subscriptions","organizations_url":"https://api.github.com/users/yez/orgs","repos_url":"https://api.github.com/users/yez/repos","events_url":"https://api.github.com/users/yez/events{/privacy}","received_events_url":"https://api.github.com/users/yez/received_events","type":"User","site_admin":false},"body":"@rpdillon \r\nThis is the final piece of abstracting recipients, templates and\r\n  the sending of mail.\r\n\r\nThe use of this would look like:\r\n\r\n```ruby\r\nusers = User.where(email: ['first@gmail.com', 'second@gmail.com'])\r\n\r\nrecipients = []\r\n\r\nusers.each do |user|\r\n  recipient = SendGrid::Recipient.new(user.email)\r\n  recipient.add_substitution('first_name', user.first_name)\r\n  recipient.add_substitution('city', user.city)\r\n\r\n  recipients << recipient\r\nend\r\n\r\ntemplate = SendGrid::Template.new('MY_TEMPLATE_ID')\r\n\r\nclient = SendGrid::Client.new(api_user: my_user, api_key: my_key)\r\n\r\nmail_defaults = {\r\n  from: 'admin@email.com',\r\n  html: '<h1>I like email</h1>',\r\n  text: 'I like email'\r\n  subject: 'Email is great',\r\n}\r\n\r\nmailer = TemplateMailer.new(client, template, recipients)\r\nmailer.mail(mail_defaults)\r\n```","created_at":"2015-08-31T22:51:06Z","updated_at":"2015-09-01T21:35:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"50b898a1677ab899278fabaaa35fea56f8ee091f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8/commits","review_comments_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8/comments","review_comment_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/8/comments","statuses_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/statuses/6fd9c7b1d1173aa571f01c12cf0a3ca36910f15c","head":{"label":"apartmentlist:feature/template-mailer","ref":"feature/template-mailer","sha":"6fd9c7b1d1173aa571f01c12cf0a3ca36910f15c","user":{"login":"apartmentlist","id":928486,"avatar_url":"https://avatars.githubusercontent.com/u/928486?v=3","gravatar_id":"","url":"https://api.github.com/users/apartmentlist","html_url":"https://github.com/apartmentlist","followers_url":"https://api.github.com/users/apartmentlist/followers","following_url":"https://api.github.com/users/apartmentlist/following{/other_user}","gists_url":"https://api.github.com/users/apartmentlist/gists{/gist_id}","starred_url":"https://api.github.com/users/apartmentlist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apartmentlist/subscriptions","organizations_url":"https://api.github.com/users/apartmentlist/orgs","repos_url":"https://api.github.com/users/apartmentlist/repos","events_url":"https://api.github.com/users/apartmentlist/events{/privacy}","received_events_url":"https://api.github.com/users/apartmentlist/received_events","type":"Organization","site_admin":false},"repo":{"id":41328616,"name":"sendgrid-ruby","full_name":"apartmentlist/sendgrid-ruby","owner":{"login":"apartmentlist","id":928486,"avatar_url":"https://avatars.githubusercontent.com/u/928486?v=3","gravatar_id":"","url":"https://api.github.com/users/apartmentlist","html_url":"https://github.com/apartmentlist","followers_url":"https://api.github.com/users/apartmentlist/followers","following_url":"https://api.github.com/users/apartmentlist/following{/other_user}","gists_url":"https://api.github.com/users/apartmentlist/gists{/gist_id}","starred_url":"https://api.github.com/users/apartmentlist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apartmentlist/subscriptions","organizations_url":"https://api.github.com/users/apartmentlist/orgs","repos_url":"https://api.github.com/users/apartmentlist/repos","events_url":"https://api.github.com/users/apartmentlist/events{/privacy}","received_events_url":"https://api.github.com/users/apartmentlist/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apartmentlist/sendgrid-ruby","description":"Official Ruby Gem for the SendGrid email Web API.","fork":true,"url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby","forks_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/forks","keys_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/teams","hooks_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/hooks","issue_events_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/events{/number}","events_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/events","assignees_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/assignees{/user}","branches_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/branches{/branch}","tags_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/tags","blobs_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/refs{/sha}","trees_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/statuses/{sha}","languages_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/languages","stargazers_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/stargazers","contributors_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/contributors","subscribers_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/subscribers","subscription_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/subscription","commits_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/commits{/sha}","git_commits_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/commits{/sha}","comments_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/comments{/number}","issue_comment_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/comments{/number}","contents_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/contents/{+path}","compare_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/merges","archive_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/downloads","issues_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues{/number}","pulls_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls{/number}","milestones_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/milestones{/number}","notifications_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/labels{/name}","releases_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/releases{/id}","created_at":"2015-08-24T21:46:29Z","updated_at":"2015-08-24T21:46:30Z","pushed_at":"2015-09-01T21:33:58Z","git_url":"git://github.com/apartmentlist/sendgrid-ruby.git","ssh_url":"git@github.com:apartmentlist/sendgrid-ruby.git","clone_url":"https://github.com/apartmentlist/sendgrid-ruby.git","svn_url":"https://github.com/apartmentlist/sendgrid-ruby","homepage":"http://sendgrid.com","size":177,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"apartmentlist:master","ref":"master","sha":"e555986095d6042bddb7243f6d334cd366f24cb4","user":{"login":"apartmentlist","id":928486,"avatar_url":"https://avatars.githubusercontent.com/u/928486?v=3","gravatar_id":"","url":"https://api.github.com/users/apartmentlist","html_url":"https://github.com/apartmentlist","followers_url":"https://api.github.com/users/apartmentlist/followers","following_url":"https://api.github.com/users/apartmentlist/following{/other_user}","gists_url":"https://api.github.com/users/apartmentlist/gists{/gist_id}","starred_url":"https://api.github.com/users/apartmentlist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apartmentlist/subscriptions","organizations_url":"https://api.github.com/users/apartmentlist/orgs","repos_url":"https://api.github.com/users/apartmentlist/repos","events_url":"https://api.github.com/users/apartmentlist/events{/privacy}","received_events_url":"https://api.github.com/users/apartmentlist/received_events","type":"Organization","site_admin":false},"repo":{"id":41328616,"name":"sendgrid-ruby","full_name":"apartmentlist/sendgrid-ruby","owner":{"login":"apartmentlist","id":928486,"avatar_url":"https://avatars.githubusercontent.com/u/928486?v=3","gravatar_id":"","url":"https://api.github.com/users/apartmentlist","html_url":"https://github.com/apartmentlist","followers_url":"https://api.github.com/users/apartmentlist/followers","following_url":"https://api.github.com/users/apartmentlist/following{/other_user}","gists_url":"https://api.github.com/users/apartmentlist/gists{/gist_id}","starred_url":"https://api.github.com/users/apartmentlist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apartmentlist/subscriptions","organizations_url":"https://api.github.com/users/apartmentlist/orgs","repos_url":"https://api.github.com/users/apartmentlist/repos","events_url":"https://api.github.com/users/apartmentlist/events{/privacy}","received_events_url":"https://api.github.com/users/apartmentlist/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apartmentlist/sendgrid-ruby","description":"Official Ruby Gem for the SendGrid email Web API.","fork":true,"url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby","forks_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/forks","keys_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/teams","hooks_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/hooks","issue_events_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/events{/number}","events_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/events","assignees_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/assignees{/user}","branches_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/branches{/branch}","tags_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/tags","blobs_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/refs{/sha}","trees_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/statuses/{sha}","languages_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/languages","stargazers_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/stargazers","contributors_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/contributors","subscribers_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/subscribers","subscription_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/subscription","commits_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/commits{/sha}","git_commits_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/git/commits{/sha}","comments_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/comments{/number}","issue_comment_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/comments{/number}","contents_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/contents/{+path}","compare_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/merges","archive_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/downloads","issues_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues{/number}","pulls_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls{/number}","milestones_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/milestones{/number}","notifications_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/labels{/name}","releases_url":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/releases{/id}","created_at":"2015-08-24T21:46:29Z","updated_at":"2015-08-24T21:46:30Z","pushed_at":"2015-09-01T21:33:58Z","git_url":"git://github.com/apartmentlist/sendgrid-ruby.git","ssh_url":"git@github.com:apartmentlist/sendgrid-ruby.git","clone_url":"https://github.com/apartmentlist/sendgrid-ruby.git","svn_url":"https://github.com/apartmentlist/sendgrid-ruby","homepage":"http://sendgrid.com","size":177,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8"},"html":{"href":"https://github.com/apartmentlist/sendgrid-ruby/pull/8"},"issue":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/8"},"comments":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/issues/8/comments"},"review_comments":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8/comments"},"review_comment":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/pulls/8/commits"},"statuses":{"href":"https://api.github.com/repos/apartmentlist/sendgrid-ruby/statuses/6fd9c7b1d1173aa571f01c12cf0a3ca36910f15c"}}}}