Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/Remove from do not mail list #7

Merged
merged 11 commits into from
Oct 6, 2017
Merged

Add/Remove from do not mail list #7

merged 11 commits into from
Oct 6, 2017

Conversation

serene
Copy link
Contributor

@serene serene commented Oct 6, 2017

No description provided.

@jessedoyle
Copy link
Contributor

📖

Copy link
Contributor

@jessedoyle jessedoyle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few small requests. Comments in review.


response['id'].present? ? true : false
rescue RestClient::ResourceNotFound, RestClient::BadRequest, RestClient::InternalServerError => e
contact.errors << 'Unexpected error occured'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of unepected error occured, what if we use e.message for the error message? It may help debug a little quicker.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we be rescuing from RestClient::InternalServerErrors for all of these API requests?

We use Rollbar to notify us of unhandled exceptions. If we're swallowing maropost 500 exceptions, we'll never get notifications when their API breaks (assuming 400/422 responses are common for invalid input).

class DoNotMailList
def self.exists?(contact)
response = request(:get,
"#{Maropost.configuration.api_url}/global_unsubscribes/email.json?contact[email]=#{contact.email}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we wrote a maropost_url(path, query) private helper method to help join the URL paths for us?

Also, could we use URI.join() here to generate the base url?

i.e.

private

def maropost_url(path, query = nil)
  URI.join(Maropost.configuration.api_url, path).tap { |u| query && u.query = query }.to_s
end

We could call the method above like this:

maropost_url('/global_unsubscribes/email.json', "contact[email]=#{contact.email}")

"#{Maropost.configuration.api_url}/global_unsubscribes/email.json?contact[email]=#{contact.email}")
JSON.parse response.body

response['id'].present? ? true : false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ternary is not necessary here. response['id'].present? should return a boolean

RestClient::Request.logged_request(
method: method,
timeout: 10,
open_timeout: 10,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make our open time 5 seconds. This would match our standards elsewhere (10 second read timeout, 5 second open timeout).

def self.request(method, url, payload = {})
RestClient::Request.logged_request(
method: method,
timeout: 10,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to read_timeout? RestClient overrides all timeouts otherwise. See: https://github.com/rest-client/rest-client#timeouts

spec/api_spec.rb Outdated
@@ -52,17 +52,39 @@
it 'calls update' do
expect(Maropost::Api).to receive(:find).with(contact.email).and_return(existing_contact)
expect(Maropost::Api).to receive(:update).with(contact)
allow(Maropost::DoNotMailList).to receive(:create).with(contact)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not stub methods in this file? Instead we should stub the request with WebMock.

subject { Maropost::DoNotMailList.exists?(contact) }

context 'email address is found on list' do
let(:found_response) { File.read File.join('spec', 'fixtures', 'do_not_mail_list', 'do_not_mail_found.json') }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we're reading fixtures often, can we create a read_fixture helper? We use this helper essentially in all of rspec suites.

We should be able to (mostly) copy the code here: https://github.com/amaabca/crm/blob/master/spec/support/helpers/fixtures.rb#L11

Then include the helper in our Rspec config: https://github.com/amaabca/crm/blob/master/spec/spec_helper.rb#L24

@@ -1,3 +1,3 @@
module Maropost
VERSION = '0.2.2'
VERSION = '0.2.3'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably bump this to 0.3.0 as it adds new functionality.

@jessedoyle
Copy link
Contributor

💩

@serene
Copy link
Contributor Author

serene commented Oct 6, 2017

👋

@jessedoyle
Copy link
Contributor

The changes look good. Thanks! 🐘🐘🐘

contact.errors << 'Unexpected error occured'
response['id'] ? true : false
rescue RestClient::ResourceNotFound, RestClient::BadRequest => e
contact.errors << "Unexpected error occurred. Error: #{e.message}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - works for me thanks!

@@ -39,6 +40,10 @@ def self.update(contact)

private

def self.maropost_url(path, query = nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

@serene serene merged commit fac1051 into master Oct 6, 2017
@serene serene deleted the do_not_mail_list branch October 6, 2017 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants